【问题标题】:how to compare images using color histograms in opencv python and find the closest match如何在opencv python中使用颜色直方图比较图像并找到最接近的匹配
【发布时间】:2014-03-15 18:24:00
【问题描述】:

我在我的项目中这样做是为了使用 opencv python 识别相机中显示的标志。我已经尝试过使用 surf,但由于某些标志的特征较少,例如将红色和蓝色作为标志的特征,因此它并不总是能给出正确的识别。 关于我可以做些什么来根据颜色直方图识别标志的项目有什么建议吗?非常感谢您的帮助。

例如,我展示了越南国旗,程序会将显示的国旗与国旗图像数据库进行比较,并识别出它是越南国旗。

注意:这是我发布的这些链接https://stackoverflow.com/questions/22424745/compare-a-single-image-to-database-of-images-and-find-the-closest-match-using-th 中的相同问题,但由于连接速度慢,我无法编辑它

 import cv2
     import numpy as np
     import pyttsx
     import sys
     import os
     import operator

     hbins = 180
     sbins = 255
     hrange = [0,180]
     srange = [0,256]
     ranges = hrange+srange   

     flags=["Cambodia.jpg","Laos.jpg","Malaysia.jpg","Myanmar.jpg","Philippines.jpg","Singapore.jpg","Thailand.jpg","Vietnam.jpg","Indonesia.jpg","Brunei.jpg"] 
     list_of_pics=[]
     valueCompare=[]
     cam = cv2.VideoCapture(0) 
     while True:
        _, frame = cam.read(0)

        cv2.imshow('asdas',frame) 
        list_of_pics=[]
        valueCompare=[]           
        k=cv2.waitKey(10)
        if(k==32):
            cv2.imwrite("pic.jpg",frame)#the image from the camera
            img = "pic.jpg"
            for i in flags:

                base = cv2.imread(img)
                test1 = cv2.imread(i)#the flags to be compared with
                rows,cols = base.shape[:2]
                basehsv = cv2.cvtColor(base,cv2.COLOR_BGR2HSV)
                test1hsv = cv2.cvtColor(test1,cv2.COLOR_BGR2HSV)

                histbase = cv2.calcHist(basehsv,[0,1],None,[180,256],ranges)
                cv2.normalize(histbase,histbase,0,255,cv2.NORM_MINMAX)

                histtest1 = cv2.calcHist(test1hsv,[0,1],None,[180,256],ranges)
                cv2.normalize(histtest1,histtest1,0,255,cv2.NORM_MINMAX)    

                comHist=cv2.compareHist(histbase,histtest1,3)
                valueCompare.append(comHist)
                picDict={"comhist":comHist,"name":i}
                list_of_pics.append(picDict)

            newlist = sorted(list_of_pics, key=operator.itemgetter('comhist')) #get the max value of all the compared images
    #print newlist
           matched_image=newlist[0]['name']
           print matched_image
        elif k == 27:
            break
    cv2.destroyAllWindows()

用于模板匹配代码。

k=cv2.waitKey(10)
methods = 'cv2.TM_CCOEFF_NORMED'#only the one method to be used
list_of_pics=[]

if(k==32):
    for flag in flags:
        img = cv2.imread('Singapore.jpg',0)
        #img = img.copy()
        template = cv2.imread(flag,0)
        w, h = template.shape[::-1]

        # All the 6 methods for comparison in a list

        method = eval(methods)#
        #print method
            # Apply template Match
        res = cv2.matchTemplate(img,template,method)
        #print res

        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        #print min_val, max_val
        matchVal=res[0][0]
        picDict={"matchVal":matchVal,"name":flag}
        list_of_pics.append(picDict)
        #print res[0][0]
    newlist = sorted(list_of_pics, key=operator.itemgetter('matchVal'),reverse=True) 
    print newlist
    matched_image=newlist[0]['name']
    print matched_image
elif k == 27:
    break
cv2.destroyAllWindows()

【问题讨论】:

    标签: python opencv image-processing


    【解决方案1】:

    计算直方图后,可以使用直方图匹配功能。

    double result = compareHist( image, template, compare_method );
    

    结果的值取决于您使用的compare_method。例如,如果您使用 correlation 作为比较方法,则 result 的值将介于 0-1 之间,并且值越大则匹配。

    替代方案:

    如果你认为数据库中标志的大小和方向与当前图像几乎相似,那么你甚至不需要计算直方图。这种情况下,可以直接使用openCV的matchTemplate()

    【讨论】:

    • 谢谢,如果我能正确地做到这一点,我会尝试实现并发布更新。非常感谢
    • 我尝试使用 compareHist(),但它没有返回正确的标志名称。我正在使用宏基笔记本电脑的内置摄像头。我将发布代码。
    • 我也尝试过使用模板匹配..但它确实可以正确识别标志..我将发布我在模板中使用的代码
    猜你喜欢
    • 2017-08-27
    • 2014-08-27
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 2022-10-09
    • 2019-08-28
    • 2017-08-23
    • 1970-01-01
    相关资源
    最近更新 更多