图片拼接 --全景图合成
开发环境
- python3
- opencv-contrib-python—3.4.2.16
- opencv-python—3.4.2.16
- PyQt5 — 5.15.6
基本思路
- SIFT特征提取
- FLANN 特征匹配
- 单应性矩阵
- 仿射变换
- 图片融合
- 最大内接矩形裁剪
- GUI界面显示
代码程序
完整工程:https://gitee.com/wangchaosun/image_merge
图片融合
merge_pic.py
import numpy as np
import cv2
LEFTDIR = 1
RIGHTDIR = 2
# get sift ,flann Machine
def getMachine():
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
sift = cv2.xfeatures2d_SIFT().create()
return sift,flann
def imgProcess(img,top,bot,left,right):
imgBord = cv2.copyMakeBorder(img,top,bot,left,right,cv2.BORDER_CONSTANT,value=(0,0,0))
imgGray = cv2.cvtColor(imgBord,cv2.COLOR_BGR2GRAY)
return imgBord,imgGray
def findEdgeDot(img,x1,x2,y1,y2):
dotsum = 0
for i in range(x1,x2+1):
for j in range(y1,y2+1):
if not img.item(j,i):
dotsum +=1
return dotsum
def getSmallOuterRect(img):
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
thresh,binary=cv2.threshold(gray,1,255,cv2.THRESH_BINARY)
image,contours,hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
areaList = []
for contour in contours:
area = cv2.contourArea(contour)
areaList.append(area)
return cv2.boundingRect(contours[np.argmax(areaList)])
def getMaxInnerRect(img,step): # 输入的图像是二进制的
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
thresh,binary=cv2.threshold(gray,1,255,cv2.THRESH_BINARY)
x = 0
y = 0
h,w = binary.shape
topdot = findEdgeDot(binary,x,x+w-1,y,y)
botdot = findEdgeDot(binary,x,x+w-1,y+h-1,y+h-1)
lefdot = findEdgeDot(binary,x,x,y,y+h-1)
rigdot = findEdgeDot(binary,x+w-1,x+w-1,y,y+h-1)
edgedot = [topdot,botdot,lefdot,rigdot]
while topdot or botdot or lefdot or rigdot :
maxedge = max(edgedot)
if maxedge == topdot:
y += step
h -= step
elif maxedge == botdot:
h -= step
elif maxedge == lefdot:
x += step
w -= step
else

最低0.47元/天 解锁文章
9558

被折叠的 条评论
为什么被折叠?



