图片拼接 --全景图合成

图片拼接 --全景图合成

开发环境
  • 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
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暴风雨中的白杨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值