OpenCV-灰度模式与灰度颜色转换 [英] Opencv - Grayscale mode Vs gray color conversion

查看:240
本文介绍了OpenCV-灰度模式与灰度颜色转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在opencv(2.4.11)python(2.7)中工作,并且在玩灰色图像.在以灰度模式加载图像并将图像从BGR转换为灰度时,我发现了异常的行为.以下是我的实验代码:

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

当我查看差异图像时,我可以看到剩下的像素而不是黑色的图像.您能提出任何理由吗?什么是处理灰色图像的正确方法.

P.S.当我在SIFT中使用两个图像时,关键点是不同的,特别是在处理质量较差的图像时,可能会导致不同的结果.

解决方案

注意:这不是重复的,因为OP知道cv2.imread中的图像在BGR中格式(不同于建议的重复问题,假设它是RGB,因此提供的答案仅解决了该问题)

为说明起见,我打开了这张相同颜色的JPEG图像:

一次使用转换

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

以及另一种通过以灰度模式加载

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

就像您所记录的那样,两张图片之间的差异并不是完美的0,我可以看到向左和向底部的差异像素

我也总结了差异,以了解

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

我尝试了所有cv2.imread()模式

cv2.imread()的所有IMREAD_模式中,只有IMREAD_COLORIMREAD_ANYCOLOR可以使用COLOR_BGR2GRAY进行转换,并且它们对IMREAD_GRAYSCALE中打开的图像的区别都相同.

差异似乎并不大.我的猜测是两种方法在数值计算上的差异(加载灰度与转换为灰度)

自然,您要避免的是在特定版本的图像上微调代码,以发现它对于来自不同来源的图像不是最佳选择.

总之,我们不要在处理管道中混用版本和类型.

因此,我将使图像源保持均匀,例如如果您已使用BGR从摄像机捕获图像,那么我将使用BGR作为源,并进行BGR到灰度的转换cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

反之亦然,如果我的最终来源是灰度的,那么我将以灰度级打开文件和视频捕获cv2.imread(path, cv2.IMREAD_GRAYSCALE)

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 from BGR to GRAY. 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.

P.S. When I use both the images in SIFT, keypoints are different which may lead to different outcome specially when working with bad quality images.

解决方案

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)

这篇关于OpenCV-灰度模式与灰度颜色转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆