Load a image in OpenCV

In order to load a image in opencv, place the image in a folder/in the same folder where you are executing the code.

import cv2
import matplotlib.pyplot as plt

img=cv2.imread(‘img1.png’,-1)
cv2.imshow(‘image’,img)
cv2.waitKey()
cv2.destroyAllWindows()

Details:

cv2.imread(filename[, flags])

Parameters:
  • filename – Name of file to be loaded.
  • flags –Flags specifying the color type of a loaded image:
    • CV_LOAD_IMAGE_ANYDEPTH – If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
    • CV_LOAD_IMAGE_COLOR – If set, always convert image to the color one
    • CV_LOAD_IMAGE_GRAYSCALE – If set, always convert image to the grayscale one
    • >0 Return a 3-channel color image.

      Note

      In the current implementation the alpha channel, if any, is stripped from the output image. Use negative value if you need the alpha channel.

    • =0 Return a grayscale image.
    • <0 Return the loaded image as is (with alpha channel).

Leave a Reply