OpenCV开发笔记(七十八):在ubuntu上搭建opencv+python开发环境以及匹配识别Demo
若该文为原创文章,转载请注明原文出处
本文章博客地址:https://hpzwl.blog.csdn.net/article/details/140435870
长沙红胖子Qt(长沙创微智科)博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中…
OpenCV开发专栏(点击传送门)
上一篇:《OpenCV开发笔记(七十七):相机标定(二):通过棋盘标定计算相机内参矩阵矫正畸变摄像头图像》
下一篇:持续补充中…
前言
Python上的OpenCv开发,在linux上的基本环境搭建流程。
安装python
以python2.7为开发版本。
sudo apt-get install python2.7 sudo apt-get install python2.7-dev
安装OpenCV
多种方式,先选择最简单的方式。
sudo apt-get install python-opencv
打开摄像头
测试Demo
import cv2
import numpy
cap = cv2.VideoCapture(0)
while 1:
ret, frame = cap.read()
cv2.imshow("capture", frame)
if cv2.waitKey(100) & 0xff == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
测试结果
模板匹配
测试Demo
import cv2
import numpy
# read template image
template = cv2.imread("src.png")
#cv2.imshow("template", template);
# read target image
target = cv2.imread("dst.png")
#cv2.imshow("target", target)
# get tempalte's width and height
tHeight, tWidth = template.shape[:2]
print tHeight, tWidth
# matches
result = cv2.matchTemplate(target, template, cv2.TM_SQDIFF_NORMED)
# normalize
cv2.normalize(result, result, 0, 1, cv2.NORM_MINMAX, -1)
minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result)
strminVal = str(minVal)
print strminVal
cv2.rectangle(target, minLoc, (minLoc[0] + tWidth, minLoc[1] + tHeight), (0,0,255), 2)
cv2.imshow("result", target)
cv2.waitKey()
cv2.destroyAllWindows()
测试结果
Flann特征点匹配
版本回退
在opencv3.4.x大版本后,4.x系列的sift被申请了专利,无法使用了,flann需要使用到
sift = cv2.xfeatures2d.SIFT_create()
所以需要回退版本。
sudo apt-get remove python-opencv sudo pip install opencv-python==3.4.2.16
安装模块库matplotlib
python -m pip install matplotlib sudo apt-get install python-tk pip install opencv-contrib-python==3.4.2.16
测试Demo
# FLANN based Matcher
import numpy as np
import cv2
from matplotlib import pyplot as plt
#min match count is 10
MIN_MATCH_COUNT = 10
# queryImage
template = cv2.imread('src.png',0)
# trainImage
target = cv2.imread('dst.png',0)
# initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(template,None)
kp2, des2 = sift.detectAndCompute(target,None)
# create FLANN match
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)
matches = flann.knnMatch(des1,des2,k=2)
# store all the good matches as per Lowe's ratio test.
good = []
# lose MIN_MATCH_COUNT:
# get key
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)
# cal mat and mask
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
matchesMask = mask.ravel().tolist()
h,w = template.shape
# convert 4 corner
pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
dst = cv2.perspectiveTransform(pts,M)
cv2.polylines(target,[np.int32(dst)],True,0,2, 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)
result = cv2.drawMatches(template, kp1, target, kp2, good, None, **draw_params)
cv2.imshow("dst", result)
cv2.imshow("dst2", target)
cv2.waitKey()
测试结果
上一篇:《OpenCV开发笔记(七十七):相机标定(二):通过棋盘标定计算相机内参矩阵矫正畸变摄像头图像》
下一篇:持续补充中…
本文章博客地址:https://hpzwl.blog.csdn.net/article/details/140435870
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!





