【opencv】SIFT算法进行图像特征点匹配

参考https://www.cnblogs.com/cj695/p/4041478.html
目的:
输入:商品货架一层

输出:圈出这一层想找的商品

import cv2




def sift_kp(image):
    gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    sift = cv2.xfeatures2d_SIFT.create()
    kp,des = sift.detectAndCompute(image,None)
    kp_image = cv2.drawKeypoints(gray_image,kp,None)
    return kp,des



#根据特征向量,找匹配的特征点
def get_good_match(des1,des2):
    bf = cv2.BFMatcher()
    matches = bf.knnMatch(des1, des2, k=2)
    good = []
    for m, n in matches:
        if m.distance < 0.75 * n.distance:
            good.append(m)
    return good



#物品名称与(kp,des对应)
d={}
#用来存放所有img
imgs=[]


img = cv2.imread("C:\\Users\\51530\\Desktop\ObjectDetection\\643.png")
img001 = cv2.imread("C:\\Users\\51530\\Desktop\ObjectDetection\\001.png")
imgs.append(('001',img001))
img003 = cv2.imread("C:\\Users\\51530\\Desktop\\ObjectDetection\\003.png")
imgs.append(('003',img003))

for i in imgs:
    kp,des=sift_kp(i[1])
    d[i[0]]=(kp,des)


threshold=130
h,w=img.shape[:2]
print(w)
print(h)
z=0
while z24:
            # print(d.get(k)[0][0].pt)
            print("kp.len:",len(kp))
            x=[]
            y=[]
            for j in kp:
                x.append(j.pt[0])
                y.append(j.pt[1])
            xmin=int(min(x))
            xmax=int(max(x))
            ymin=int(min(y))
            ymax=int(max(y))
            print('ymin:',ymin)
            print('ymax:',ymax)
            cv2.rectangle(img, (z, ymin), (z+xmax, ymax), (55, 255, 155), 2)
            print((xmax, ymin))
            cv2.putText(img, k, (z, 200), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2)
            z+=xmax
            break
    z+=2

cv2.imshow('img',img)
cv2.imwrite('C:\\Users\\51530\\Desktop\ObjectDetection\\0000001.png',img)
# cv2.imshow('1',kp_image1)
# cv2.imshow('2',kp_image2)
key=cv2.waitKey(0)

输入图:


训练的图是:


输出图:


存在问题:

1 阈值设置问题

2 识别不准确

3 同一物品,转换角度无法识别


你可能感兴趣的:(【opencv】SIFT算法进行图像特征点匹配)