Using OpenCV library, you can −
- Read and write images
- Capture and save videos
- Process images (filter, transform)
- Perform feature detection
- Detect specific objects such as faces, eyes, cars, in the videos or images.
- Analyze the video, i.e., estimate the motion in it, subtract the background, and track objects in it.
Loading an image using OpenCV:
import cv2
# colored Image
Img = cv2.imread (“Penguins.jpg”,1)
# Black and White (gray scale)
Img_1 = cv2.imread (“Penguins.jpg”,0)
Image Shape/Resolution :
import cv2
# Black and White (gray scale)
Img = cv2.imread (“Penguins.jpg”,0)
Print(img.shape)
Displaying the Image :
import cv2
# Black and White (gray scale)
Img = cv2.imread (“Penguins.jpg”,0)
cv2.imshow(“Penguins”, img)
cv2.waitKey(0)
# cv2.waitKey(2000)
cv2.destroyAllWindows()
Resizing the Image :
import cv2
# Black and White (gray scale)
img = cv2.imread (“Penguins.jpg”,0)
resized_image = cv2.resize(img, (650,500))
cv2.imshow(“Penguins”, resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
There are so many fucntions present in OpenCV for image processing. now we will learn various operations we can perform on the image for image processing one by one.
1.Grayscaling
2.Image Translations
3.Image Rotation
4.Scaling and Croping
5.Edge detection, Sharpning, Thresholding, Erosion and Dilation
6.Arithmetic and bitwise Operations
Greyscaling is the process of converting an image from format RGB, CMYK, HSV, etc. to shades of grey. the greyscale image is varies between complete white and complete black.
- There are so many algorithms that can customized to work only with the greyscaled images for e.g. Canny edge detection function pre-implemented in OpenCV library works on Grayscaled images only.
- There is dimensional reduction in greyscale image as RGB image has 3 channels and greyscaled image has only 1 channel.
Translation is the process of shifiting image from one location to another location.
this can be done using transformation matrix
the tx and ty elements of the matrix shows the shifting of image to x-direction and y-direction respectively.
we can use the cv2.wrapAffine() function to implement these translations. This function requires a 2×3 array. The numpy array should be of float type.
- Shifting of image .
- Hiding part of the image.
- Animation to the image by using loop to translation.
- To crop the image.
Roation opearation is used to rotate the image in multiplies of 90 degree.
- To display the reflection of image.
This opeartion is used to change the size of the image.
To fit one image into another image.
This opeartion is used to crop the image.In this all we are doing is slicing arrays. we first supply the startY and endY coordinates, followed by the startX and endX coordinates to the slice.
for e.g cropped = image[70:170, 440:540]
To remove the unwanted part of the image.
It involves the detecting the sharp edges in the image.
- Object detection.
- Image recognition.
This opeartion is used to sharpen the image
cv2.filter2D() this fucntion is used to perform the sharpen/blur opeeration
It has 3 parameters in which one is kernal. kernal is nothing but the numpy array for sharpning image.
for e.g kernel_3x3 = np.ones((3, 3), np.float32) / 9
Here, the matter is straight forward. If pixel value is greater than a threshold value, it is assigned one value (may be white), else it is assigned another value (may be black). The function used is cv2.threshold. First argument is the source image, which should be a grayscale image. Second argument is the threshold value which is used to classify the pixel values. Third argument is the maxVal which represents the value to be given if pixel value is more than (sometimes less than) the threshold value. OpenCV provides different styles of thresholding and it is decided by the fourth parameter of the function. Different types are:
- cv2.THRESH_BINARY
- cv2.THRESH_BINARY_INV
- cv2.THRESH_TRUNC
- cv2.THRESH_TOZERO
- cv2.THRESH_TOZERO_INV
Erosion and Dilation are the two morphological operations which are a set of operations that process images based on shapes.
Erosion:
- Erodes away the boundaries of foreground object
- Used to diminish the features of an image.
Dilution:
- Increases the object area
- Used to accentuate features
Erosion:
It is useful for removing small white noises.
Used to detach two connected objects etc.
Dilation:
In cases like noise removal, erosion is followed by dilation. Because, erosion removes white noises, but it also shrinks our object. So we dilate it. Since noise is gone, they won’t come back, but our object area increases.
It is also useful in joining broken parts of an object.