OpenCV for Image Processing
By: Faiza Anan Noor
Tags: Image Processing, Numpy, Matplotlib, Opencv, Python

In this story, I’m going to highlight some basic operations of OpenCV that can be useful for Image Processing.

Reading the image:

So lets start off with this image below:

Code to be executed for reading and showing this image:

Code for reading an image and plotting it using Matplotlib

OUTPUT

Here, the imread() function mainly performs the function of reading an image and it takes in two parameters. The first one is the path of our image, and the second one is a flag denoting how the image should be read.

In this example, flag = 0. This basically means that we are reading our image in grayscale mode.

For flag = 1 the image will be read in coloured mode. Then the result will be :

Displaying the Image with our default Image Viewer:

cv.imshow(‘kawaii’,image1) cv.waitKey(0) #Waits for an indefinite amount of time for a key press when the patrameter =0. cv.destroyAllWindows() #Destroys all windows using the value specified in the function waitKey(),after the image has been displayed.

Finding Image Shape:

Shape of image is accessed by img.shape . It returns the number of rows, columns and channels(only when the image is colored):

image2=cv.imread(“doggo.jpg”,0)

plt.imshow(image2)

print(image2.shape) OUTPUT: (2485, 4268)

Here,2485 refers to the height, 4268 refers to the width.

If we read the image in colored mode, we

image1=cv.imread(“doggo.jpg”,1)

plt.imshow(image1)

print(image1.shape) OUTPUT: (2485, 4268,3)

Here in this case we get 3 as the third output since the image is being read as a coloured image.

Converting an Image from one color space to another:

The method used for converting an image from one color space to another is : cv2.cvtColor()

This normally takes in two parameters. The first one is the source/image to be converted, and the second one specifies the current mode and the mode to convert to.

This statement can be clarified using an example:

new_img3=cv.cvtColor(image1, cv.COLOR_BGR2RGB )

plt.imshow(new_img3)

Here we are converting our image in BGR mode to RGB. If we do so, we can finally obtain our actual image.

Result of cv.cvtColor(image1, cv.COLOR_BGR2RGB )

There are more than 150 color-space conversion methods available in OpenCV. We know that OpenCV reads an image not in the conventional way that is RGB, but rather in BGR. So in order to correctly read a picture and for different other reasons we might need to switch between color spaces.

Similar conversions include:

image = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY )

image = cv2.cvtColor(src, cv2.COLOR_BGR2HSV )

Resizing the image:

resized_img3=cv.resize(new_img3,(200,200))

plt.imshow(resized_img3)

print(resized_img3.shape)

OUTPUT:

Here our image has been resized to (200,200)

Splitting our Image:

Let’s split our newly converted image into 3 separate channels

red,green,blue=cv.split(new_img3) f, axarr = plt.subplots(2,2)

axarr[0,0].imshow(red)

axarr[0,1].imshow(green)

axarr[1,0].imshow(blue)

axarr[1,1].imshow(new_img3)

OUTPUT:

Here we the 4th one is our original image, and the 1st,2nd,3rd images are the results of splitting our images according to Red, Green, Blue Channels respectively.

By executing cv.split(new_img3), we separated our image into 3 separate channels.

Merging individual Color Channels:

We can also merge individual color channels in the following way:

mergedImg= cv.merge((red,green,blue))

plt.imshow(mergedImg)

cv.imshow(“MergedImg”,mergedImg)

cv.waitKey(0)

cv.destroyAllWindows()

Saving Image to a Location:

We can save our image to a specific location by using:

cv.imwrite(filename, image)

Example: cv.imwrite(“NewImg.jpg”,mergedImg)

Here the image “mergedImg” will be saved in the current directory under the name “NewImg.jpg”