Image processing using OpenCV-Python

Laiba Nasir
5 min readSep 22, 2022

--

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

  1. import cv2 library
  2. 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
Read Image

The output will be

Fig 1

Display Image

steps to display image

  1. Use the method cv2.imshow(window_name ,image)
  2. where window_name is name of image for window and image is the image you want to display
  3. Here waitkey() is used to avoid python kernel from crash or to make image wait on screen until the press
  4. print() method will show pixels of the image
output of print()

the output of display will be same as in fig 1.

Save Image on Disk

To save image

  1. 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
save image

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

text in image
output of text on image

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

blurring effects using gaussian and median filters

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

  1. Sobel Edge Detection
  2. 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

  1. Read image as grey scale image.
  2. se cv2.threshold() function to obtain the threshold image.
  3. Use cv2.findContours() and pass the threshold image and necessary parameters.
  4. 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

contour an image

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 👏

--

--

No responses yet