第六章 图像检索以及基于图像描述符的搜索
通过提取特征进行图像的匹配与搜索
1 特征检测算法
常见的特征和提取算法:
Harris 检测角点
Sift 检测斑点(blob) 有专利保护
Surf 检测斑点 有专利保护
Fast 检测角点
Brief 检测斑点
Orb 带方向的fast算法和具有旋转不变性的brief算法
特征的定义
author = “retacn”
copyright = “property of mankind.”
license = “CN”
version = “0.0.1”
maintainer = “retacn”
email = “[email protected]”
status = “Development”
import cv2
import numpy as np
img = cv2.imread(‘../test1.jpg’)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,
2,
23, # sobel算子的中孔,3-31之间的奇数
0.04)
img[dst > 0.01 * dst.max()] = [0, 0, 255]
while (True):
cv2.imshow(“corners”, img)
if cv2.waitKey(33) & 0xFF == ord(‘q’):
break
cv2.destroyAllWindows()
使用dog和sift进行特征提取和描述
示例代码如下:
import cv2
import sys
import numpy as py
imgpath = ‘../test1.jpg’
img = cv2.imread(imgpath)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
sift = cv2.xfeatures2d.SIFT_create()
keypoints, descriptor = sift.detectAndCompute(gray, None)
img = cv2.drawKeypoints(image=img,
outImage=img,
keypoints=keypoints,
color=(51, 163, 236),
flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow(‘sift_keypoints’, img)
while (True):
if cv2.waitKey(int(1000 / 12)) & 0xFF == ord(‘q’):
break
cv2.destroyAllWindows()
使用心有快速hessian算法和SURF来提取特征
示例代码发如下:
author = “retacn”
copyright = “property of mankind.”
license = “CN”
version = “0.0.1”
maintainer = “retacn”
email = “[email protected]”
status = “Development”
import cv2
import sys
import numpy as py
imgpath = ‘../test1.jpg’
img = cv2.imread(imgpath)
alg = ‘SIFT’
threshold = ‘4000’
def fd(algorithm):
if algorithm == ‘SIFT’:
return cv2.xfeatures2d.SIFT_create()
if algorithm == ‘SURF’:
# return cv2.xfeatures2d.SURF_create(float(threshold) if len(sys.argv) == 4 else 4000)
return cv2.xfeatures2d.SURF_create(float(threshold))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
fd_alg = fd(alg)
keypoints, descriptor = fd_alg.detectAndCompute(gray, None)
img = cv2.drawKeypoints(image=img,
outImage=img,
keypoints=keypoints,
color=(51, 163, 236),
flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow(‘keypoints’, img)
while (True):
if cv2.waitKey(int(1000 / 12)) & 0xFF == ord(‘q’):
break
cv2.destroyAllWindows()
基于ORB的特征检测和特征匹配
ORB是基于
FAST(featuresfrom accelerated segment test)关键点检测技术
在像素周围绘制一个圆,包含16个像素
BRIEF(binaryrobust independent elementary features) 描述符
暴力(brute-force)匹配法
比较两个描述符,并产生匹配结果
ORB特征匹配
示例代码如下:
import numpy as np
import cv2
from matplotlib import pyplot as plt
cv2.ocl.setUseOpenCL(False)
img1 = cv2.imread(‘../test2_part.jpg’, cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread(‘../test2.jpg’, cv2.IMREAD_GRAYSCALE)
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
matches = sorted(matches, key=lambda x: x.distance)
img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:40], img2, flags=2)
plt.imshow(img3), plt.show()
报如下错误:
cv2.error: D:\Build\OpenCV\opencv-3.1.0\modules\python\src2\cv2.cpp:163:error: (-215) The data should normally be NULL! in functionNumpyAllocator::allocate
解决办法,添加如下代码 :
cv2.ocl.setUseOpenCL(False)
k最邻近配匹
import numpy as np
import cv2
from matplotlib import pyplot as plt
cv2.ocl.setUseOpenCL(False)
img1 = cv2.imread(‘../test2_part.jpg’, cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread(‘../test2.jpg’, cv2.IMREAD_GRAYSCALE)
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
bf = cv2.BFMatcher(cv2.NORM_L1, crossCheck=False)
matches = bf.knnMatch(des1, des2, k=2)
img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, matches, img2, flags=2)
plt.imshow(img3), plt.show()
Flann匹配法
Fast library for approximate nearestneighbors 近似最近邻的快速库
import numpy as np
import cv2
from matplotlib import pyplot as plt
queryImage = cv2.imread(‘../test2_part.jpg’, cv2.IMREAD_GRAYSCALE)
trainingImage = cv2.imread(‘../test2.jpg’, cv2.IMREAD_GRAYSCALE)
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(queryImage, None)
kp2, des2 = sift.detectAndCompute(trainingImage, None)
FLANN_INDEX_KDTREE = 0
indexParams = dict(algorithm=FLANN_INDEX_KDTREE, trees=5) # 处理索引
searchParams = dict(checks=50) # 创建对象,用来指定索引树的遍历次数
flann = cv2.FlannBasedMatcher(indexParams, searchParams)
matches = flann.knnMatch(des1, des2, k=2)
matchesMask = [[0, 0] for i in range(len(matches))]
for i, (m, n) in enumerate(matches):
if m.distance < 0.7 * n.distance:
matchesMask[i] = [1, 0]
drawParams = dict(matchColor=(0, 255, 0),
singlePointColor=(255, 0, 0),
matchesMask=matchesMask,
flags=0)
resultImage = cv2.drawMatchesKnn(queryImage, kp1, trainingImage, kp2, matches, None, **drawParams)
plt.imshow(resultImage), plt.show()
运行结果如下:
Flann单应性匹配
示例代码如下:
author = “retacn”
copyright = “property of mankind.”
license = “CN”
version = “0.0.1”
maintainer = “retacn”
email = “[email protected]”
status = “Development”
import numpy as np
import cv2
from matplotlib import pyplot as plt
MIN_MATCH_COUNT = 10
img1 = cv2.imread(‘../test3_part.jpg’, cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread(‘../test3.jpg’, cv2.IMREAD_GRAYSCALE)
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)
FLANN_INDEX_KDTREE = 0
indexParams = dict(algorithm=FLANN_INDEX_KDTREE, trees=5) # 处理索引
searchParams = dict(checks=50) # 创建对象,用来指定索引树的遍历次数
flann = cv2.FlannBasedMatcher(indexParams, searchParams)
matches = flann.knnMatch(des1, des2, k=2)
good = []
for m, n in matches:
if m.distance < 0.7 * n.distance:
good.append(m)
if len(good) > MIN_MATCH_COUNT:
# 在原始图像和训练图像中查询特征点
src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
# 单应性
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
matchesMask = mask.ravel().tolist()
# 对第二张图片计算相对于原始图像的投影畸变,并绘制边框
h, w = img1.shape
pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)
dst = cv2.perspectiveTransform(pts, M)
img2 = cv2.polylines(img2, [np.int32(dst)], True, 255, 3, cv2.LINE_AA)
else:
print(“Not enough matches are found -%d/%d” % (len(good), MIN_MATCH_COUNT))
matchesMask = None
draw_params = dict(matchColor=(0, 255, 0), # 绿线
singlePointColor=None,
matchesMask=matchesMask,
flags=2)
img3 = cv2.drawMatches(img1, kp1, img2, kp2, good, None, **draw_params)
plt.imshow(img3, ‘gray’), plt.show()
运行结果如下:
基于纹身取证的应用程序示例
A 将图像描述符保存到文件中
author = “retacn”
copyright = “property of mankind.”
license = “CN”
version = “0.0.1”
maintainer = “retacn”
email = “[email protected]”
status = “Development”
import cv2
import numpy as np
from os import walk
from os.path import join
import sys
def create_descriptors(folder):
files = []
for (dirpath, dirnames, filenames) in walk(folder):
files.extend(filenames)
for f in files:
save_descriptor(folder, f, cv2.xfeatures2d.SIFT_create())
def save_descriptor(folder, image_path, feature_detector):
print(“reading %s” % image_path)
if image_path.endswith(“npy”) or image_path.endswith(“avi”):
return
img = cv2.imread(join(folder, image_path), cv2.IMREAD_GRAYSCALE)
keypoints, descriptors = feature_detector.detectAndCompute(img, None)
descriptor_file = image_path.replace(“jpg”, “npy”)
np.save(join(folder, descriptor_file), descriptors)
dir = sys.argv[1]
create_descriptors(dir)
B 扫描匹配
author = “retacn”
copyright = “property of mankind.”
license = “CN”
version = “0.0.1”
maintainer = “retacn”
email = “[email protected]”
status = “Development”
from os.path import join
from os import walk
import numpy as np
import cv2
from sys import argv
from matplotlib import pyplot as plt
folder = argv[1]
query = cv2.imread(join(folder, ‘part.jpg’), cv2.IMREAD_GRAYSCALE)
files = []
images = []
descriptors = []
for (dirpath, dirnames, filenames) in walk(folder):
files.extend(filenames)
for f in files:
if f.endswith(‘npy’) and f != ‘part.npy’:
descriptors.append(f)
print(descriptors)
sift = cv2.xfeatures2d.SIFT_create()
query_kp, query_ds = sift.detectAndCompute(query, None)
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)
MIN_MATCH_COUNT = 10
potential_culprits = {}
print(“>> Initiating picture scan…”)
for d in descriptors:
print(“——— analyzing %s for matches ————” % d)
matches = flann.knnMatch(query_ds, np.load(join(folder, d)), k=2)
good = []
for m, n in matches:
if m.distance < 0.7 * n.distance:
good.append(m)
if len(good) > MIN_MATCH_COUNT:
print(‘%s is a match! (%d)’ % (d, len(good)))
else:
print(‘%s is not a match ’ % d)
potential_culprits[d] = len(good)
max_matches = None
potential_suspect = None
for culprit, matches in potential_culprits.items():
if max_matches == None or matches > max_matches:
max_matches = matches
potential_suspect = culprit
print(“potential suspect is %s” % potential_suspect.replace(“npy”, “”).upper())