我正在使用opencv(2.4.11) python(2.7)工作,并且在处理灰度图像时进行了一些尝试。当以灰度模式加载图像并将图像从BGR转换为GRAY时,我发现了一种不寻常的行为。以下是我的实验代码:
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中使用这两个图像时,关键点是不同的,这可能会导致在处理低质量图像时产生不同的结果。
cv2.imread
读入的图像格式是 BGR(不同于被认为是 RGB 的重复问题,因此提供的答案只针对了那个问题)。 - bakkal