3

this piece of code is used to plot an image inside a jupyter cell.

from matplotlib import pyplot as plt
import cv2
img = cv2.imread('img1.png',1)
a = plt.imshow(img)
plt.show()

other parts is easy to understand except opencv.imread.

the doc of opencv.imread says

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).

I've tried -1, 0 and 1, which works well.

what do other options (such as CV_LOAD_IMAGE_ANYDEPTH) do? how can I use these options in Python?

from matplotlib import pyplot as plt
import cv2
img = cv2.imread('img1.png',CV_LOAD_IMAGE_ANYDEPTH)
a = plt.imshow(img)
plt.show()

direct setting gets error

--------------------------------------------------------------------------
NameError                                Traceback (most recent call last)
<ipython-input-14-8c9e4e747e3f> in <module>()
      1 from matplotlib import pyplot as plt
      2 import cv2
----> 3 img = cv2.imread('img1.png',CV_LOAD_IMAGE_ANYDEPTH)
      4 a = plt.imshow(img)
      5 plt.show()

NameError: name 'CV_LOAD_IMAGE_ANYDEPTH' is not defined

using cv2.CV_LOAD_IMAGE_ANYDEPTH got another error

--------------------------------------------------------------------------
AttributeError                           Traceback (most recent call last)
<ipython-input-29-f6ea92814b06> in <module>()
      1 from matplotlib import pyplot as plt
      2 import cv2
----> 3 img = cv2.imread('img1.png',cv2.CV_LOAD_IMAGE_ANYDEPTH)
      4 a = plt.imshow(img)
      5 plt.show()

AttributeError: module 'cv2.cv2' has no attribute 'CV_LOAD_IMAGE_ANYDEPTH'

can any one provide a runnable python code to demonstrate how to use CV_LOAD_IMAGE_ANYDEPTH?

Jay
  • 400
  • 1
  • 4
  • 13

2 Answers2

1

imread flags indicate how the image should be read. They are integer flags, coming in powers of 2 (except for -1).

The flags are described in the documentation here, and here you can find their values. Technically, you could simply use the value, but that is a bad coding style.

There is a bit of confusion regarding these flags since with the release of OpenCV 3, some non backwards compatible changes to the naming and scoping of some flags have been made. For OpenCV 3 and higher, use the names in linked documentation.

Paul92
  • 8,827
  • 1
  • 23
  • 37
  • Thanks for your answer. Would you please provide a runnable python code to demonstrate how to use CV_LOAD_IMAGE_ANYDEPTH in python? – Jay Jun 09 '19 at 11:52
0

I noticed that they have renamed some of the parameters in the python version when compared to the official openCV documentation cv2.CV_LOAD_IMAGE_ANYDEPTH becomes cv2.IMREAD_ANYDEPTH in python for example so your code should look like this:

img = cv2.imread('img1.png',cv2.IMREAD_ANYDEPTH)

you can find all the flags for cv2.imread under this new denomination cv2.IMREAD_***