本文主要是介绍【纹理学习】基于BFmatcher/FlannBasedMatcher的SIFT/ORB/SURF在Re-ID的简单实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本笔记图片源于网络,仅用于学习用途,联系侵删。
对纹理学习不甚了解,可前往【纹理学习】初探纹理学习
目录
- 基于BFmatcher的SIFT实现1
- 基于BFmatcher的SIFT实现2
- 基于FlannBasedMatcher的SURF实现
- 基于FlannBasedMatcher的SIFT实现
- 基于BFMatcher的ORB实现
- 高清图片实验
- 一些想法
- 贴一下“年久失修”的代码(还是可以用的!)
- 两个不同形式的基于BFmatcher的SIFT
- 基于FlannBasedMatcher的SURF
- 基于FlannBasedMatcher的SIFT
- 基于BFmatcher的ORB
- 给大家放一下实验原图片
基于BFmatcher的SIFT实现1
ratio=0.8
ratio=0.9
基于BFmatcher的SIFT实现2
ratio=0.2
ratio=0.5
ratio=0.8
ratio=whatever
基于FlannBasedMatcher的SURF实现
基于FlannBasedMatcher的SIFT实现
ratio=0.8
基于BFMatcher的ORB实现
ratio=0.8
ratio=0.9
ratio=0.99
高清图片实验
一些想法
当时进行这个实验时间比较紧凑,代码研究不到位,总感觉一些参数是可以优化的。
但毋庸置疑的结论就是这些方法在高清图片上表现优异,面对十分模糊的Re-ID图片显得仓皇失措。
结合一篇去模糊的论文:
【论文笔记】Unsupervised Domain-Specific Deblurring via Disentangled Representations
或许可以通过将去模糊方法应用到整个人体上以实现图片转清晰化,进而运行纹理学习的一些方法进行实验以提高匹配精度,有兴趣的朋友不放尝试一下。
贴一下“年久失修”的代码(还是可以用的!)
两个不同形式的基于BFmatcher的SIFT
One
import cv2
import numpy as npdef 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_image, kp, desdef get_good_match(des1, des2):bf = cv2.BFMatcher()matches = bf.knnMatch(des1, des2, k=2) # des1为模板图,des2为匹配图matches = sorted(matches, key=lambda x: x[0].distance / x[1].distance)good = []for m, n in matches:if m.distance < 0.9 * n.distance:good.append(m)return goodimg1 = cv2.imread(r'1.png')
img2 = cv2.imread(r'2.png')kpimg1, kp1, des1 = sift_kp(img1)
kpimg2, kp2, des2 = sift_kp(img2)cv2.namedWindow("img1",0)
cv2.resizeWindow("img1", 640, 480)
cv2.imshow('img1',np.hstack((img1,kpimg1)))
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.namedWindow("img2",0)
cv2.resizeWindow("img2", 640, 480)
cv2.imshow('img2',np.hstack((img2,kpimg2)))
cv2.waitKey(0)
cv2.destroyAllWindows()goodMatch = get_good_match(des1, des2)
all_goodmatch_img= cv2.drawMatches(img1, kp1, img2, kp2, goodMatch, None, flags=2)
# goodmatch_img自己设置前多少个goodMatch[:10]
goodmatch_img = cv2.drawMatches(img1, kp1, img2, kp2, goodMatch[:100], None, flags=2)cv2.namedWindow("all_goodmatch_img",0)
cv2.resizeWindow("all_goodmatch_img", 640, 480)
cv2.imshow('all_goodmatch_img', all_goodmatch_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.namedWindow("goodmatch_img",0)
cv2.resizeWindow("goodmatch_img", 640, 480)
cv2.imshow('goodmatch_img', goodmatch_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Two
import numpy as np
import cv2# 匹配的图片
imgname1 = '1.png'
imgname2 = '2.png'# SIFT特征描述子
sift = cv2.xfeatures2d.SIFT_create()# 读取第一张图像,并做灰度处理
# kp1、des1分别为第一张图像的 keypoints and descriptors
img1 = cv2.imread(imgname1)
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
kp1, des1 = sift.detectAndCompute(img1,None)# 读取第二张图像,并做灰度处理
# kp2、des2分别为第二张图像的 keypoints and descriptors
img2 = cv2.imread(imgname2)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
kp2, des2 = sift.detectAndCompute(img2,None)# 水平拼接两张灰度图像,窗口处理
hmerge = np.hstack((gray1, gray2))
cv2.namedWindow("gray",0)
cv2.resizeWindow("gray", 640, 480)
cv2.imshow("gray", hmerge)
cv2.waitKey(0)
cv2.destroyAllWindows()# 画出特征点,并显示为红色圆圈
img3 = cv2.drawKeypoints(img1,kp1,img1,color=(255,0,255))
img4 = cv2.drawKeypoints(img2,kp2,img2,color=(255,0,255))
hmerge = np.hstack((img3, img4))
cv2.namedWindow("point",0)
cv2.resizeWindow("point", 640, 480)
cv2.imshow("point", hmerge)
cv2.waitKey(0)
cv2.destroyAllWindows()# BFMatcher解决匹配
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)
# 调整ratio
good = []
for m,n in matches:if m.distance < 0.8*n.distance:good.append([m])# 绘制匹配结果
img5 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,flags=2)
cv2.namedWindow("BFmatch",0)
cv2.resizeWindow("BFmatch", 640, 480)
cv2.imshow("BFmatch", img5)
cv2.waitKey(0)
cv2.destroyAllWindows()
基于FlannBasedMatcher的SURF
import numpy as np
import cv2
from matplotlib import pyplot as pltimgname1 = 't1.jpeg'
imgname2 = 't2.jpeg'surf = cv2.xfeatures2d.SURF_create()FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params,search_params)img1 = cv2.imread(imgname1)
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) #灰度处理图像
kp1, des1 = surf.detectAndCompute(img1,None)#des是描述子img2 = cv2.imread(imgname2)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
kp2, des2 = surf.detectAndCompute(img2,None)hmerge = np.hstack((gray1, gray2)) #水平拼接
cv2.namedWindow("gray",0)
cv2.resizeWindow("gray", 640, 480)
cv2.imshow("gray", hmerge) #拼接显示为gray
cv2.waitKey(0)
cv2.destroyAllWindows()img3 = cv2.drawKeypoints(img1,kp1,img1,color=(255,0,255))
img4 = cv2.drawKeypoints(img2,kp2,img2,color=(255,0,255))hmerge = np.hstack((img3, img4)) #水平拼接
cv2.namedWindow("point",0)
cv2.resizeWindow("point", 640, 480)
cv2.imshow("point", hmerge) #拼接显示为gray
cv2.waitKey(0)
cv2.destroyAllWindows()matches = flann.knnMatch(des1,des2,k=2)good = []
for m,n in matches:if m.distance < 0.8*n.distance:good.append([m])
img5 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)
cv2.namedWindow("SURF",0)
cv2.resizeWindow("SURF", 640, 480)
cv2.imshow("SURF", img5)
cv2.waitKey(0)
cv2.destroyAllWindows()
基于FlannBasedMatcher的SIFT
import numpy as np
import cv2
from matplotlib import pyplot as pltimgname1 = '3.png'
imgname2 = '4.png'sift = cv2.xfeatures2d.SIFT_create()# FLANN 参数设计
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params,search_params)img1 = cv2.imread(imgname1)
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) #灰度处理图像
kp1, des1 = sift.detectAndCompute(img1,None)#des是描述子img2 = cv2.imread(imgname2)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
kp2, des2 = sift.detectAndCompute(img2,None)hmerge = np.hstack((gray1, gray2)) #水平拼接
cv2.namedWindow("gray",0)
cv2.resizeWindow("gray", 640, 480)
cv2.imshow("gray", hmerge) #拼接显示为gray
cv2.waitKey(0)
cv2.destroyAllWindows()img3 = cv2.drawKeypoints(img1,kp1,img1,color=(255,0,255))
img4 = cv2.drawKeypoints(img2,kp2,img2,color=(255,0,255))hmerge = np.hstack((img3, img4)) #水平拼接
cv2.namedWindow("point",0)
cv2.resizeWindow("point", 640, 480)
cv2.imshow("point", hmerge) #拼接显示为gray
cv2.waitKey(0)
cv2.destroyAllWindows()
matches = flann.knnMatch(des1,des2,k=2)
matchesMask = [[0,0] for i in range(len(matches))]good = []
for m,n in matches:if m.distance < 0.8*n.distance:good.append([m])img5 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)
cv2.namedWindow("FLANN",0)
cv2.resizeWindow("FLANN", 640, 480)
cv2.imshow("FLANN", img5)
cv2.waitKey(0)
cv2.destroyAllWindows()
基于BFmatcher的ORB
import numpy as np
import cv2
from matplotlib import pyplot as pltimgname1 = 'b.png'
imgname2 = 'f.png'orb = cv2.ORB_create()img1 = cv2.imread(imgname1)
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) #灰度处理图像
kp1, des1 = orb.detectAndCompute(img1,None)#des是描述子img2 = cv2.imread(imgname2)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
kp2, des2 = orb.detectAndCompute(img2,None)hmerge = np.hstack((gray1, gray2)) #水平拼接
cv2.namedWindow("gray",0)
cv2.resizeWindow("gray", 640, 480)
cv2.imshow("gray", hmerge) #拼接显示为gray
cv2.waitKey(0)
cv2.destroyAllWindows()img3 = cv2.drawKeypoints(img1,kp1,img1,color=(255,0,255))
img4 = cv2.drawKeypoints(img2,kp2,img2,color=(255,0,255))hmerge = np.hstack((img3, img4)) #水平拼接
cv2.namedWindow("point",0)
cv2.resizeWindow("point", 640, 480)
cv2.imshow("point", hmerge) #拼接显示为gray
cv2.waitKey(0)
cv2.destroyAllWindows()# BFMatcher解决匹配
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)
# 调整ratio
good = []
for m,n in matches:if m.distance < 0.99*n.distance:good.append([m])img5 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)
cv2.namedWindow("ORB",0)
cv2.resizeWindow("ORB", 640, 480)
cv2.imshow("ORB", img5)
cv2.waitKey(0)
cv2.destroyAllWindows()
给大家放一下实验原图片
这篇关于【纹理学习】基于BFmatcher/FlannBasedMatcher的SIFT/ORB/SURF在Re-ID的简单实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!