Read Image Using OpenCV Framework

kamal_DS
3 min readJan 19, 2023

--

OpenCV is one of the popular frameworks used for Image Processing which means if we want to change pixel intensities or the pattern of the image we can take the help of OpenCV because there are a lot of methods available in the OpenCV framework which is used to develop popular computer vision models.

In The first step, we need to install a few packages,

  • NumPy
  • OpenCV
  • matplotlib

for windows OS, You can have a look at the below websites for installation purposes.

First, we need to import NumPy and OpenCV and in OpenCV Framework we can use imread method to read an image ex: gray and color images.

for grayscale images:

image_gray = cv2.imread('./images/mandril_gray.tif',cv2.IMREAD_GRAYSCALE)  # reading an image 
print(image_gray.shape)
cv2.imshow('first_image' , image_gray) # showing an image
cv2.waitKey() # waiting
cv2.destroyAllWindows() # till pressing anything from keyboard

In the first line, we are reading a grayscale image by giving the path of the image and cv2.IMREAD_GRAYSCALE tells us to read the image in a grayscale pattern only. If we are using color image also cv2.IMREAD_GRAYSCALE converts color images to grayscale images only.

shape method will tell us the number of rows and columns in an image ex: (512, 512).

cv2.waitkey() is used to hold the window for a specific time, If we didn’t give any value inside the wait key() it waits for the user to click a key from the keyboard once the user clicks any key from the keyboard window gets destroyed. If we specify any values it takes the value as seconds in time.

once clicking any button from the keyboard window gets destroyed for this purpose we are using cv2.destroyAllWindows()

For example, if we want to read a color image as the grayscale image we can use the same process as mentioned above only change is we need to specify the location of the color image which is reading.

this color image using the same code we can convert into black and white,

image_gray = cv2.imread('./images/tulips.jpg',0)  # reading an image 
print(image_gray.shape)
cv2.imshow('first_image' , image_gray) # showing an image
cv2.waitKey() # waiting
cv2.destroyAllWindows() # till pressing anything from keyboard

the conversions are done because the first parameter is the path of the image in the imread method and the second parameter is how to read an image,

0- For a greyscale image we can use 0 instead of cv2.IMREAD_GRAYSCALE

1- color image

so here we are reading color images but asking to show them in the form of grayscale that’s why we have given 0 as a second parameter.

the color image will be converted into grayscale which you can see below.

you can get the complete code from my GitHub Repo = https://github.com/saikamal3344/OpenCV-Image-Processing-

--

--

kamal_DS
kamal_DS

Written by kamal_DS

Interested to work in the field of Artificial Intelligence, Machine Learning, Deep Learning, NLP and Computer Vision.

Responses (1)