# 光立法匹配
import numpy as np
import cv2
from matplotlib import pyplot as plt
import copy
import os
import time
start_time = time.time()
MIN_MATCH_COUNT = 7
# 第一步,打开usb摄像头,或者读取视频文件,一样
cap = cv2.VideoCapture('my_match_video_data/video/src18.mp4') # 每一帧都是模板
bgr_all = cv2.imread('my_match_video_data/bgr/bgr_report.jpg') # 大的背景
bgr_all_tmp = copy.deepcopy(bgr_all)
frames_all=cap.get(7)
name1 = "my_match_video2pic/20_KLT/src"
name2 = "my_match_video2pic/20_KLT/bgr"
def mkdir(path):
folder = os.path.exists(path)
if not folder: # 判断是否存在文件夹如果不存在则创建为文件夹
os.makedirs(path) # makedirs 创建文件时如果路径不存在会创建这个路径
mkdir(name1)
mkdir(name2)
# 第二步:构建角点检测所需参数:角点个数、质量阈值、角点之间最小距离
feature_params = dict(maxCorners=40,
qualityLevel=0.3,
minDistance=50)
lk_params = dict(winSize=(15, 15), # lucas kanade参数:搜索窗口、金字塔层数
maxLevel=3)
color = np.random.randint(0, 255, (100, 3)) # 随机颜色条
# 第三步:拿到第一帧图像并灰度化作为前一帧图片
ret, old_frame = cap.read()
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) # 每一帧都是模板
bgr_gray = cv2.cvtColor(bgr_all_tmp, cv2.COLOR_BGR2GRAY) # 大的背景
sift = cv2.SIFT_create()
kp1, des1 = sift.detectAndCompute(bgr_gray, None) # 大的背景
kp2, des2 = sift.detectAndCompute(old_gray, None) # 每一帧的
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm=FLANN_INDEX_KDTREE,
trees=5) # 0为线性暴力搜索(应该可以尝试换一下看速度)https://blog.csdn.net/qq_36584673/article/details/121997887
search_params = dict(checks=50) # 遍历的次数
flann = cv2.FlannBasedMatcher(index_params, search_params) # 快速最近邻搜索库,
matches = flann.knnMatch(des1, des2,
k=2) # 大的背景,每一帧的,最匹配的K个点 https://blog.csdn.net/qq_45769063/article/details/108773998
# store all the good matches as per Lowe's ratio test.
good = []
index_p0 = []
for m, n in matches:
if m.distance < 0.6 * n.distance:
# print("m")
good.append(m) # 索引下标,可以根据这个找到相应的坐标
index_p0.append(m.queryIdx)
if len(good) > MIN_MATCH_COUNT:
# src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2) # m.queryIdx描述符的下标 pt为关键点坐标 kp1是大的背景的
p0 = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2) # 待匹配的 dst_pts(每一帧的)
p0_src = copy.deepcopy(p0)
print(len(p0))
# 第四步:返回所有检测特征点,需要输入图片,角点的最大数量,品质因子,minDistance
for frames in range(int(frames_all)-1):
# 第六步:读取图片灰度化作为后一张图片的输入
bgr_all_tmp = copy.deepcopy(bgr_all)
ret, frame = cap.read()
frame_tmp = copy.deepcopy(frame)
frame_gray = cv2.cvtColor(frame_tmp, cv2.COLOR_BGR2GRAY)
h,w = frame_gray.shape
# 第七步:进行金字塔LK光流检测需要输入前一帧和当前图像及前一帧检测到的角点
pl, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)
# print(len(pl[st == 1]))
print(len(pl[st == 1]))
if len(pl[st == 1]) <= 10: #270
old_gray = frame_gray.copy()
kp2, des2 = sift.detectAndCompute(old_gray, None)
matches = flann.knnMatch(des1, des2, # des1是大的背景, des2是每一帧的
k=2) # https://blog.csdn.net/qq_45769063/article/details/108773998
good = []
index_p0 = []
for m, n in matches:
if m.distance < 0.6 * n.distance:
good.append(m)
index_p0.append(m.queryIdx)
if len(good) > MIN_MATCH_COUNT:
p0 = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2) # 待匹配的 dst_pts
continue
# 第八步:读取运动了的角点st == 1表示检测到的运动物体,即v和u表示为0
index_tmp = []
for i in range(len(st)):
if st[i]==1:
index_tmp.append(index_p0[i])
dst_pts = np.float32([kp1[i].pt for i in index_tmp]).reshape(-1, 1, 2) # 原图的索引坐标,src(大的背景模板)
index_p0 = index_tmp
good_new = pl[st == 1] # 现在帧的
good_old = p0[st == 1] # 前一帧的
src_pts = []
for i, (new, old) in enumerate(zip(good_new, good_old)): # 取每一帧的特征点坐标
a, b = new.ravel()
src_pts.append([[a, b]])
src_pts = np.float32(src_pts) # 每一帧的
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) # 5.0原图像的点经过变换后点与目标图像上对应点的误差,src是每一帧的,dst是大的背景
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(bgr_all_tmp, [np.int32(dst)], True, (255, 0, 0), 10, cv2.LINE_AA) # 用于绘制任何图像上的多边形 # 能运行的最后删除的东西
srcc = [] # 每一帧的
for i in dst:
srcc.append(i[0])
srcc = np.float32(srcc)
dstt = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]])
# # 通过运算得出M矩阵
M_inver = cv2.getPerspectiveTransform(srcc, dstt)
# # 提取特征图片
bg_img = cv2.warpPerspective(bgr_all, M_inver, (w, h))
cv2.imwrite(name1+"/src"+str(frames)+".png", frame)
cv2.imwrite(name2+"/bgr"+str(frames)+".png", bg_img)
print(frames, frames_all)
cv2.namedWindow('result', cv2.WINDOW_NORMAL) # 窗口大小可以改变
cv2.imshow('result', img2)
cv2.waitKey(0)
# 第十步:更新前一帧图片和角点的位置
old_gray = frame_gray.copy()
p0 = good_new.reshape(-1, 1, 2)
end_time = time.time()
print("运行花费了"+str(end_time-start_time)+"秒")
cv2.destroyAllWindows()
cap.release()
程序意思为有一个待匹配的图,然后在一个更大的图里找到与之匹配的,再通过透视变换,转变为同一视角的。
效果图: