python opencv imwrite … can't find params

前端 未结 4 1397
梦谈多话
梦谈多话 2021-01-02 16:39

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

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

The only problem is that opencv does not r

4条回答
  •  情深已故
    2021-01-02 17:31

    I can't find key CV_XXXXX in the cv2 module:

    1. Try cv2.XXXXX
    2. Failing that, use cv2.cv.CV_XXXXX

    In your case, cv2.cv.CV_IMWRITE_PNG_COMPRESSION.


    More info.

    The docs for OpenCV (cv2 interface) are a bit confusing.

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

    I use the following to search for the relevant cv2 constant name. Say I was looking for CV_MORPH_DILATE. I'll search 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'm looking for.

    However, sometimes the constants have not been moved from the cv interface to the cv2 interface yet.

    In that case, you can find them under cv2.cv.CV_XXXX.

    So, I looked for IMWRITE_PNG_COMPRESSION for you and couldn't find it (under cv2....), and so I looked under cv2.cv.CV_IMWRITE_PNG_COMPRESSION, and hey presto! It's there:

    >>> cv2.cv.CV_IMWRITE_PNG_COMPRESSION
    16
    

提交回复
热议问题