Image processing using OpenCV-Python
There are lot of data and images are significant part of these data. To use these images ,need to be processed So there I am going to share some processes of image manipulation or processes to improve its quality or extracting some information. To process it, python becomes an apt choice So lets see few of them below So the image I have used for processing is cat
Read Image
Steps to read the image
- import cv2 library
- To read image, use the function of cv2.Imread(image_path, flag) where path is the image path and flags represent image types like color, greyscale or unchanged This can also be represent using flags like 1 for color,0 for greyscale and -1 for unchanged by passing parameter
The output will be
Display Image
steps to display image
- Use the method cv2.imshow(window_name ,image)
- where window_name is name of image for window and image is the image you want to display
- Here waitkey() is used to avoid python kernel from crash or to make image wait on screen until the press
- print() method will show pixels of the image
the output of display will be same as in fig 1.
Save Image on Disk
To save image
- cv2.imwrite(path, image) will be used where path is where to save and image_name will be by which name the new image will be saved like in our case as greyscale.png
Resize Image
Image resizing mean scaling of image. It helps to zoom in or zoom out.
For zoom in use parameter cv2.INTER_AREA and cv2.INTER_LINEAR for zoom out
the output is
Cropping Image
The output will be
Text On Image
Text method is used to add text on image where parameters are image name, text ,coordinates using org ,font font scale ,color of the text and thickness of the text
Convert RGB Image to Grayscale
for the conversion of RGB image into greyscale set the flag to 0 and then show it on the screen.
The output will be
Blurring an Image
When we blur an image, we make the color transition from one side of an edge in the image to another smooth rather than sudden. The effect is to average out rapid changes in pixel intensity. A blur is a very common operation we need to perform before other tasks such as thresholding. there are several blurring methods but three significant are Gaussian ,Median and Bilateral Blus
The output will be
Edge Detection of Image
Edge detection is an image processing technique for finding the boundaries of objects within images .It is used for image segmentation and data extraction.
There are two important edge-detection algorithms available in OpenCV
- Sobel Edge Detection
- Canny Edge Detection
Here canny Algorithm has applied For edging
Blurring of image is required that has been done by median blur method and then apply canny method for edge detection
the output will be
Sharpen Image
Prepare a filter and sharpen the image
output will be
Detect Contours in an image
Contours can be explained simply as a curve joining all the continuous points (along the boundary), having same color or intensity. The contours are a useful tool for shape analysis and object detection and recognition. For better accuracy, use binary images
So the steps to contours in image are
- Read image as grey scale image.
- se cv2.threshold() function to obtain the threshold image.
- Use cv2.findContours() and pass the threshold image and necessary parameters.
- findContours() returns contours. You can draw it on the original image or a blank image. Here original image is used.
and the output will be
Histogram of Image
A histogram is a very important tool in Image processing. It is a graphical representation of the distribution of data. An image histogram gives a graphical representation of the distribution of pixel intensities in a digital image. Here calcHist and plot is used for histogram
Following will be the histogram of the given image
So these are some operation of image processing ,So Hope you would like this ,if yes clap below 👏