简体   繁体   English

opencv彩色图像到greycode转换错误

[英]opencv color image to greycode conversion error

i started on a project which inputs 2 images and detect the keypoints using sift and then checking the similarity of the 2 images 我开始了一个项目,该项目输入2张图像并使用筛选检测关键点,然后检查2张图像的相似性

i actually completed the project without converting the image files to greycode but later i understood that converting images to grey code and then comparing gives more accurate results 我实际上是在没有将图像文件转换为灰度代码的情况下完成项目的,但后来我知道将图像转换为灰度代码,然后进行比较可以得出更准确的结果

so i wrote the code to convert the image to greycode but i am facing a problem 所以我写了代码将图像转换为格雷码,但是我遇到了一个问题

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)

i am getting an error 我遇到错误

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

how can i resolve it thanks in advance 我该如何解决它,谢谢

You don't need to convert it, if you load it as you do. 如果按需加载,则无需转换。

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

Second parameter=0 means you load it as gray scale image. 第二个参数= 0表示您将其加载为灰度图像。

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

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