Image Enhancement Techniques Using OpenCV
Image Enhancement Techniques Using OpenCV
Image enhancement is the process of improving the quality and appearance of an image. It
can be used to correct flaws or defects in an image, or to simply make an image more visually
appealing. Image enhancement techniques can be applied to a wide range of images,
including photographs, scans, and digital images. Some common goals of image
enhancement include increasing contrast, sharpness, and colorfulness; reducing noise and
blur; and correcting distortion and other defects. Image enhancement techniques can be
applied manually using image editing software, or automatically using algorithms and
computer programs such as OpenCV.
In this article, we will explore a variety of image enhancement techniques that can be
performed using OpenCV and Python. OpenCV is a powerful, open-source computer vision
library that provides a wide range of image processing and computer vision algorithms. By
combining the capabilities of OpenCV with the versatility of Python, we can easily
implement a variety of image enhancement techniques to improve the quality and appearance
of our images.
In the following sections, we will discuss the different image enhancement techniques and
how to implement them using OpenCV and Python. There are several image enhancement
techniques that you can use with OpenCV and Python to improve the quality and clarity of
images. Here are a few examples:
Method-1
There are several ways to adjust the brightness and contrast of an image using OpenCV and
Python. One common method is to use the cv2.addWeighted() function, which allows you to
adjust the brightness by adding a scalar value to each pixel in the image, and the contrast by
scaling the pixel values.
Here is an example of how to adjust the brightness and contrast of an image using the
cv2.addWeighted() function:
#Import the necessary libraries
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the image
image = cv2.imread('GFG.jpeg')
#Plot the original image
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Adjust the brightness and contrast
# Adjusts the brightness by adding 10 to each pixel value
brightness = 10
# Adjusts the contrast by scaling the pixel values by 2.3
contrast = 2.3
image2 = cv2.addWeighted(image, contrast, np.zeros(image.shape, image.dtype), 0,
brightness)
#Save the image
cv2.imwrite('modified_image.jpg', image2)
#Plot the contrast image
plt.subplot(1, 2, 2)
plt.title("Brightness & contrast")
plt.imshow(image2)
plt.show()
Output:
In this example, the brightness of the image is adjusted by adding 10 to each pixel value, and
the contrast is adjusted by scaling the pixel values by 2.3. You can adjust the values of
brightness and contrast to achieve the desired level of brightness and contrast.
Method-2
Another method for adjusting the brightness and contrast of an image is to use the
cv2.convertScaleAbs() function, which allows you to adjust the brightness and contrast using
a combination of scaling and shifting the pixel values.
#Import the necessary libraries
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the image
image = cv2.imread('GFG.jpeg')
#Plot the original image
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Adjust the brightness and contrast
# g(i,j)=α⋅f(i,j)+β
# control Contrast by 1.5
alpha = 1.5
# control brightness by 50
beta = 50
image2 = cv2.convertScaleAbs(image, alpha=alpha, beta=beta)
#Save the image
cv2.imwrite('Brightness & contrast.jpg', image2)
#Plot the contrast image
plt.subplot(1, 2, 2)
plt.title("Brightness & contrast")
plt.imshow(image2)
plt.show()
In this example, the brightness and contrast are adjusted using a combination of scaling and
shifting the pixel values. You can adjust the values of alpha and beta to achieve the desired
level of brightness and contrast.
Sharpening images
Sharpening is the process of enhancing the edges and fine details in an image to make it
appear sharper and more defined. It is important because it can help to bring out the details
and features in an image, making it more visually appealing and easier to understand.
Sharpening can be used to correct blur or softness in an image and can be applied using a
variety of techniques.
Method-1
One common method for sharpening images using OpenCV and Python is to use the
cv2.filter2D() function, which convolves the image with a kernel. The kernel can be designed
to enhance the edges in the image, resulting in a sharper image.
Here is an example of how to sharpen an image using the cv2.filter2D() function:
#Import the necessary libraries
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the image
image = cv2.imread('GFG.jpeg')
#Plot the original image
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Create the sharpening kernel
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
# Sharpen the image
sharpened_image = cv2.filter2D(image, -1, kernel)
#Save the image
cv2.imwrite('sharpened_image.jpg', sharpened_image)
#Plot the sharpened image
plt.subplot(1, 2, 2)
plt.title("Sharpening")
plt.imshow(sharpened_image)
plt.show()
Sharpening
In this example, a 3×3 sharpening kernel is used to enhance the edges in the image. You can
experiment with different kernels to achieve the desired level of sharpening. Numpy is used
to create the sharpening kernel is created as a NumPy array using the np.array() function.
This array is then passed as an argument to the cv2.filter2D() function, which convolves the
image with the kernel to sharpen it.
Method-2
Another method for sharpening images is to use the cv2.Laplacian() function, which
calculates the Laplacian of an image and returns the result as a sharpened image.
#Import the necessary libraries
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the image
image = cv2.imread('GFG.jpeg')
#Plot the original image
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Sharpen the image using the Laplacian operator
sharpened_image2 = cv2.Laplacian(image, cv2.CV_64F)
#Save the image
cv2.imwrite('Laplacian sharpened_image.jpg', sharpened_image2)
#Plot the sharpened image
plt.subplot(1, 2, 2)
plt.title("Laplacian Sharpening")
plt.imshow(sharpened_image2)
plt.show()
Laplacian Sharpening
In this example, the Laplacian operator calculates the sharpened image. You can adjust the
depth of the output image using the cv2.CV_64F parameter.
Median Method-1
Here is an example of how to remove noise from an image using the cv2.medianBlur()
function in OpenCV:
#Import the necessary libraries
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the image
image = cv2.imread('GFG.jpeg')
#Plot the original image
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Remove noise using a median filter
filtered_image = cv2.medianBlur(image, 11)
#Save the image
cv2.imwrite('Median Blur.jpg', filtered_image)
#Plot the blured image
plt.subplot(1, 2, 2)
plt.title("Median Blur")
plt.imshow(filtered_image)
plt.show()
Median Blur
In this example, the cv2.medianBlur() function is used to apply a median filter to the image.
The 5 parameter specifies the size of the kernel to use for the filter. You can adjust the kernel
size to achieve the desired level of noise reduction.
Method-2
Another method for removing noise from images is to use a Gaussian filter, which uses a
weighted average of neighboring pixels to smooth out noise and reduce artifacts. You can use
the cv2.GaussianBlur() function to apply a Gaussian filter to an image in OpenCV.
#Import the necessary libraries
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the image
image = cv2.imread('GFG.jpeg')
#Plot the original image
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Remove noise using a Gaussian filter
filtered_image2 = cv2.GaussianBlur(image, (7, 7), 0)
#Save the image
cv2.imwrite('Gaussian Blur.jpg', filtered_image2)
#Plot the blured image
plt.subplot(1, 2, 2)
plt.title("Gaussian Blur")
plt.imshow(filtered_image2)
plt.show()
Gaussian Blur
In this example, the cv2.GaussianBlur() function is used to apply a Gaussian filter to the
image. The (5, 5) parameter specifies the size of the kernel to use for the filter, and the 0
parameter specifies the standard deviation of the Gaussian function. You can adjust these
parameters to achieve the desired level of noise reduction.
Enhanced Coloured
This code first converts the image from the BGR color space to the HSV color space using
the cv2.cvtColor() function. It then adjusts the hue, saturation, and value (brightness) of the
image by multiplying the corresponding channels by a scalar value. Finally, it converts the
image back to the BGR color space and saves the modified image. You can adjust the scalar
values to achieve the desired level of color enhancement.
Scaled Image
In this example, the image is scaled by a factor of 2 along both axes, resulting in an image
that is twice the size of the original. The interpolation parameter allows you to specify the
interpolation method to use when resizing or scaling the image. The available options include
cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, and others.
This is just a basic example of how to resize and scale images using OpenCV and Python.
You can adjust the size and scaling factors to achieve the desired results, and you can also
specify the interpolation method to use when resizing or scaling the image.
Inverse Transform
We can also inverse the color by simply subtracting each value from 255
#Import the necessary libraries
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the image
image = cv2.imread('GFG.jpeg')
https://www.geeksforgeeks.org/image-enhancement-techniques-using-opencv-python/
https://www.analyticsvidhya.com/blog/2021/05/image-processing-using-opencv-with-
practical-examples/