opencv color image to greycode conversion error

时间:2017-08-13 17:46:56

标签: python opencv image-processing

我开始了一个项目,它输入2个图像并使用筛选检测关键点,然后检查2个图像的相似性

我实际完成了项目而没有将图像文件转换为greycode,但后来我明白将图像转换为灰色代码然后比较可以得到更准确的结果

所以我编写了将图像转换为greycode的代码 但我面临一个问题

import cv2
import easygui
import sys
from matplotlib import pyplot as plt

print "image 1 :",sys.argv[1]
print "image 2 :",sys.argv[2]
print "******** comparing images please wait *********"
file1=sys.argv[1]
file2=sys.argv[2]

img1 = cv2.imread(file1,0)#queryImage
img2 = cv2.imread(file2,0)#trainImage
gray_image1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray_image2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

我收到错误

image 1 : taj1.jpg
image 2 : taj2.jpg
******** comparing images please wait *********
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /root/opencv-3.3.0/opencv-3.3.0/modules/imgproc/src/color.cpp, line 10638
Traceback (most recent call last):
  File "image_similarity.py", line 14, in <module>
    gray_image1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
cv2.error: /root/opencv-3.3.0/opencv-3.3.0/modules/imgproc/src/color.cpp:10638: error: (-215) scn == 3 || scn == 4 in function cvtColor

我该如何解决它 提前谢谢

1 个答案:

答案 0 :(得分:1)

如果你像加载它一样,你不需要转换它。

img1 = cv2.imread(file1,0)#queryImage
img2 = cv2.imread(file2,0)#trainImage

第二个参数= 0表示您将其加载为灰度图像。