Opencv - Grayscale mode Vs gray color conversion

后端 未结 1 1446
予麋鹿
予麋鹿 2020-12-05 03:59

I am working in opencv(2.4.11) python(2.7) and was playing around with gray images. I found an unusual behavior when loading image in gray scale mode and converting image fr

相关标签:
1条回答
  • 2020-12-05 04:56

    Note: This is not a duplicate, because the OP is aware that the image from cv2.imread is in BGR format (unlike the suggested duplicate question that assumed it was RGB hence the provided answers only address that issue)

    To illustrate, I've opened up this same color JPEG image:

    once using the conversion

    img = cv2.imread(path)
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    

    and another by loading it in gray scale mode

    img_gray_mode = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    

    Like you've documented, the diff between the two images is not perfectly 0, I can see diff pixels in towards the left and the bottom

    I've summed up the diff too to see

    import numpy as np
    np.sum(diff)
    # I got 6143, on a 494 x 750 image
    

    I tried all cv2.imread() modes

    Among all the IMREAD_ modes for cv2.imread(), only IMREAD_COLOR and IMREAD_ANYCOLOR can be converted using COLOR_BGR2GRAY, and both of them gave me the same diff against the image opened in IMREAD_GRAYSCALE

    The difference doesn't seem that big. My guess is comes from the differences in the numeric calculations in the two methods (loading grayscale vs conversion to grayscale)

    Naturally what you want to avoid is fine tuning your code on a particular version of the image just to find out it was suboptimal for images coming from a different source.

    In brief, let's not mix the versions and types in the processing pipeline.

    So I'd keep the image sources homogenous, e.g. if you have capturing the image from a video camera in BGR, then I'd use BGR as the source, and do the BGR to grayscale conversion cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    Vice versa if my ultimate source is grayscale then I'd open the files and the video capture in gray scale cv2.imread(path, cv2.IMREAD_GRAYSCALE)

    0 讨论(0)
提交回复
热议问题