Python opencv imwrite ... can't find parameters

I am using opencv with python. I wanted to do cv2.imwrte:

cv2.imwrite('myimage.png', my_im) 

The only problem is that opencv does not recognize params constants:

 cv2.imwrite('myimage.png', my_im, cv2.CV_IMWRITE_PNG_COMPRESSION, 0) 

It cannot find CV_IMWRITE_PNG_COMPRESSION. Any ideas?

+7
python opencv
source share
4 answers

I can not find the CV_XXXXX key in the cv2 module:

  • Try cv2.XXXXX
  • Otherwise use cv2.cv.CV_XXXXX

In your case, cv2.cv.CV_IMWRITE_PNG_COMPRESSION .


Additional Information.

Documents for OpenCV (cv2 interface) are a bit confusing.

Usually parameters that look like CV_XXXX are actually cv2.XXXX .

I use the following to find the relevant cv2 constant cv2 . Say I was looking for CV_MORPH_DILATE . I will look for any constant with MORPH in it:

 import cv2 nms = dir(cv2) # list of everything in the cv2 module [m for m in nms if 'MORPH' in m] # ['MORPH_BLACKHAT', 'MORPH_CLOSE', 'MORPH_CROSS', 'MORPH_DILATE', # 'MORPH_ELLIPSE', 'MORPH_ERODE', 'MORPH_GRADIENT', 'MORPH_OPEN', # 'MORPH_RECT', 'MORPH_TOPHAT'] 

From this, I see that MORPH_DILATE is what I am looking for.

However , sometimes constants do not move from the cv interface to the cv2 interface.

In this case, they can be found in the cv2.cv.CV_XXXX section.

So, I was looking for IMWRITE_PNG_COMPRESSION for you and could not find it (under cv2.... ), and so I looked under cv2.cv.CV_IMWRITE_PNG_COMPRESSION , and hey presto! It is there:

 >>> cv2.cv.CV_IMWRITE_PNG_COMPRESSION 16 
+24
source share

Math deployment .coffee to ignore case and look in both namespaces:

 import cv2 import cv2.cv as cv nms = [(n.lower(), n) for n in dir(cv)] # list of everything in the cv module nms2 = [(n.lower(), n) for n in dir(cv2)] # list of everything in the cv2 module search = 'imwrite' print "in cv2\n ",[m[1] for m in nms2 if m[0].find(search.lower())>-1] print "in cv\n ",[m[1] for m in nms if m[0].find(search.lower())>-1] >>> in cv2 ['imwrite'] in cv ['CV_IMWRITE_JPEG_QUALITY', 'CV_IMWRITE_PNG_COMPRESSION', 'CV_IMWRITE_PXM_BINARY'] >>> 

Hope this problem goes into some later release of cv2 ...

+3
source share

compression style is automatically selected from the file extension. see cv2.imwrite help here .

however, you may still be interested to know all the possible flags used by all the possible functions in the cv2 and cv modules.

find cv2.txt and cv.txt on your computer. they will be where opencv modules are installed. at the bottom of these text files is a list of flags used by the respective modules.

just in case, if you do not find them, you can download the ones that I have, although they have been since August 2011:

+1
source share

in fact, with the cv2 API, this constant is replaced by cv2.IMWRITE_PNG_COMPRESSION .

0
source share

All Articles