简体   繁体   English

Opencv - 灰度模式与灰度颜色转换

[英]Opencv - Grayscale mode Vs gray color conversion

I am working in opencv(2.4.11) python(2.7) and was playing around with gray images.我在 opencv(2.4.11) python(2.7) 工作,并在玩灰色图像。 I found an unusual behavior when loading image in gray scale mode and converting image from BGR to GRAY.在灰度模式下加载图像并将图像从 BGR 转换为灰色时,我发现了一个不寻常的行为。 Following is my experimental code:以下是我的实验代码:

import cv2

path = 'some/path/to/color/image.jpg'

# Load color image (BGR) and convert to gray
img = cv2.imread(path)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Load in grayscale mode
img_gray_mode = cv2.imread(path, 0)

# diff = img_gray_mode - img_gray
diff = cv2.bitwise_xor(img_gray,img_gray_mode)

cv2.imshow('diff', diff)
cv2.waitKey()

When I viewed the difference image, I can see the left out pixels instead of jet black image.当我查看差异图像时,我可以看到遗漏的像素而不是黑色图像。 Can you suggest any reason?你能提出任何理由吗? What is the correct way of working with gray images.处理灰度图像的正确方法是什么。

PS When I use both the images in SIFT, keypoints are different which may lead to different outcome specially when working with bad quality images. PS 当我在 SIFT 中使用这两个图像时,关键点是不同的,这可能会导致不同的结果,特别是在处理质量差的图像时。

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)注意:这不是重复的,因为 OP 知道来自cv2.imread的图像采用 BGR 格式(与建议的重复问题假设它是 RGB 不同,因此提供的答案仅解决该问题)

To illustrate, I've opened up this same color JPEG image:为了说明这一点,我打开了这张相同颜色的 JPEG 图像:

在此处输入图片说明

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就像您记录的那样,两个图像之间的差异不是完美的 0,我可以看到左侧和底部的差异像素

在此处输入图片说明

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我尝试了所有cv2.imread()模式

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_GRAYSCALEcv2.imread()所有IMREAD_模式中,只有IMREAD_COLORIMREAD_ANYCOLOR可以使用COLOR_BGR2GRAY进行转换,并且它们都给了我与在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, eg 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)所以我会保持图像源的同质性,例如,如果您从 BGR 中的摄像机捕获图像,那么我会使用 BGR 作为源,并将 BGR 进行灰度转换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)反之亦然,如果我的最终来源是灰度,那么我会以灰度cv2.imread(path, cv2.IMREAD_GRAYSCALE)打开文件和视频捕获

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM