OpenCV Python – Reading Image


OpenCV Python – Reading an image



”;


The CV2 package (name of OpenCV-Python library) provides the imread() function to read an image.

The command to read an image is as follows −


img=cv2.imread(filename, flags)

The flags parameters are the enumeration of following constants −

  • cv2.IMREAD_COLOR (1) − Loads a color image.
  • cv2.IMREAD_GRAYSCALE (0) − Loads image in grayscale mode
  • cv2.IMREAD_UNCHANGED (-1) − Loads image as such including alpha channel

The function will return an image object, which can be rendered using imshow() function. The command for using imshow() function is given below −


cv2.imshow(window-name, image)

The image is displayed in a named window. A new window is created with the AUTOSIZE flag set. The WaitKey() is a keyboard binding function. Its argument is the time in milliseconds.

The function waits for specified milliseconds and keeps the window on display till a key is pressed. Finally, we can destroy all the windows thus created.

The function waits for specified milliseconds and keeps the window on display till a key is pressed. Finally, we can destroy all the windows thus created.

The program to display the OpenCV logo is as follows −


import numpy as np
import cv2
# Load a color image in grayscale
img = cv2.imread(''OpenCV_Logo.png'',1)
cv2.imshow(''image'',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

The above program displays the OpenCV logo as follows −


OpenCV Logo

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *