opencv 项目--图像匹配

本文按照如下设计

ImageStitching_ExcessThree.py

python 复制代码
from Stitcher import Stitcher
import cv2
import my_utils
# 只拼接两张图片

# 读取需要拼接的图片
# imageA_original = cv2.imread("left_01.png")
# imageB_original = cv2.imread("right_01.png")
imageA_original = cv2.imread("left_01.jpg")
imageB_original = cv2.imread("right_01.jpg")
imageC_original = cv2.imread("right_02.jpg")

# 图像预处理-改变图像大小
imageA = my_utils.resize(imageA_original,width=500)
imageB = my_utils.resize(imageB_original,width=500)
imageC = my_utils.resize(imageC_original,width=500)

# 把图片拼接成全景图
stitcher = Stitcher()
(result, vis) = stitcher.stitch([imageA, imageB], showMatches=True)
(result, vis) = stitcher.stitch([result, imageC], showMatches=True)

# 显示所有图片
stitcher.cv_show("Image A", imageA)
stitcher.cv_show("Image B", imageB)
stitcher.cv_show("Image C", imageC)
stitcher.cv_show("Keypoint Matches", vis)
stitcher.cv_show("Result", result)

ImageStitching_JustTwo.py

python 复制代码
from Stitcher import Stitcher
import cv2
import my_utils
# 只拼接两张图片

# 读取需要拼接的图片
# imageA_original = cv2.imread("left_01.png")
# imageB_original = cv2.imread("right_01.png")
imageA_original = cv2.imread("left_01.jpg")
imageB_original = cv2.imread("right_01.jpg")

# 图像预处理-改变图像大小
imageA = my_utils.resize(imageA_original,width=500)
imageB = my_utils.resize(imageB_original,width=500)


# 把图片拼接成全景图
stitcher = Stitcher()
(result, vis) = stitcher.stitch([imageA, imageB], showMatches=True)
print(vis)
# 显示所有图片
stitcher.cv_show("Image A", imageA)
stitcher.cv_show("Image B", imageB)
stitcher.cv_show("Keypoint Matches", vis)
stitcher.cv_show("Result", result)

my_utils.py

python 复制代码
import cv2

def resize(image, width=None, height=None, inter=cv2.INTER_AREA):
    dim = None
    (h, w) = image.shape[:2]
    if width is None and height is None:
        return image
    if width is None:
        r = height / float(h)
        dim = (int(w * r), height)
        print('width is None', dim)

    else:
        r = width / float(w)
        dim = (width, int(h * r))
        print('height is None', dim)
    resized = cv2.resize(image, dim, interpolation=inter)
    return resized

Stitcher.py

python 复制代码
import numpy as np
import cv2


class Stitcher:

    # 拼接函数
    def stitch(self, images, ratio=0.75, reprojThresh=4.0, showMatches=False):
        # 获取图片
        (imageB, imageA) = images

        # 检测A,B图片的SIFT关键特征点,并且计算其特征描述子(特征向量)
        (kpsA, featureA) = self.detectAndDescribe(imageA)
        (kpsB, featureB) = self.detectAndDescribe(imageB)

        # 匹配两张图片的所有特征点,返回匹配结果
        M = self.matchKeypoints(kpsA, kpsB, featureA, featureB, ratio, reprojThresh)
        # 如果返回结果为空,没有匹配成功的特征点,退出算法
        if M is None:
            return None

        # 否则,提取匹配结果
        # H是3x3视角变换矩阵
        (matches, H, status) = M
        # 将图片A进行视角变换,result是变换后图片
        result = cv2.warpPerspective(imageA, H, (imageA.shape[1] + imageB.shape[1], imageA.shape[0]))
        # self.cv_show('result', result)
        # 将图片B传入result图片最左端
        result[0:imageB.shape[0], 0:imageB.shape[1]] = imageB
        # self.cv_show('result', result)

        # 检测是否需要显示图片匹配
        if showMatches:
            # 生成匹配图片
            vis = self.drawMatches(imageA, imageB, kpsA, kpsB, matches, status)
            # 返回结果
            return (result, vis)

        # 返回匹配结果
        return result

    # 获取图片关键点和特征描述子
    def detectAndDescribe(self, img):
        # 转灰度图
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        # 建立SIFT实例对象
        descriptor = cv2.xfeatures2d.SIFT_create()
        # 检测SIFT特征点并且计算描述子
        (kps, feature) = descriptor.detectAndCompute(img, None)

        # 并且将结果转换为Numpy数组
        kps = np.float32([kp.pt for kp in kps])

        # 返回特征点集合描述子
        return (kps, feature)

    # 匹配两张图片的特征点
    def matchKeypoints(self, kpsA, kpsB, featureA, featureB, ratio, reprojThresh):
        # 使用BF匹配
        matcher = cv2.BFMatcher()

        # 使用KNN来对A,B图的SIFT特征进行匹配
        rawMatches = matcher.knnMatch(featureA, featureB, k=2)

        # 获得较好的相匹配的特征(筛选特征点)
        matches = []
        for m in rawMatches:
            # 当最近距离跟次近距离的比值小于ratio值时,保留此匹配对
            if len(m) == 2 and m[0].distance < m[1].distance * ratio:
                # 存储两个点在featuresA, featuresB中的索引值
                # DMatch.trainIdx - Index of the descriptor in train descriptors
                # DMatch.queryIdx - Index of the descriptor in query descriptors
                matches.append((m[0].trainIdx, m[0].queryIdx))

        # 当筛选后的匹配对大于4时,计算视角变换矩阵
        if len(matches) > 4:
            # 获取匹配对的点的坐标
            ptsA = np.float32([kpsA[i] for (_, i) in matches])
            ptsB = np.float32([kpsB[i] for (i, _) in matches])

            # 计算视角变换矩阵
            (H, status) = cv2.findHomography(ptsA, ptsB, cv2.RANSAC, reprojThresh)

            # 返回匹配结果和单应性矩阵和status
            return (matches, H, status)

        # 如果匹配对小于4时,返回None
        return None

    # 展示图片
    def cv_show(self, name, img):
        cv2.imshow(name, img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    def drawMatches(self, imageA, imageB, kpsA, kpsB, matches, status):
        # 初始化可视化图片,将A、B图左右连接到一起
        print("Start draw Matching...")
        (hA, wA) = imageA.shape[:2]
        (hB, wB) = imageB.shape[:2]
        vis = np.zeros((max(hA, hB), wA + wB, 3), dtype="uint8")
        vis[0:hA, 0:wA] = imageA
        vis[0:hB, wA:] = imageB

        # 联合遍历,画出匹配对
        for ((trainIdx, queryIdx), s) in zip(matches, status):
            # 当点对匹配成功时,画到可视化图上
            if s == 1:
                # 画出匹配对
                ptA = (int(kpsA[queryIdx][0]), int(kpsA[queryIdx][1]))
                ptB = (int(kpsB[trainIdx][0]) + wA, int(kpsB[trainIdx][1]))
                cv2.line(vis, ptA, ptB, (0, 255, 0), 1)

        # 返回可视化结果
        return vis
相关推荐
QYR-分析3 分钟前
深耕智慧物流赛道:交叉带分拣机器人行业全景解析
大数据·人工智能·机器人
君为先-bey4 分钟前
LeMiCa——基于扩散的高效视频生成的词典序最小最大路径缓存
人工智能·深度学习·计算机视觉·扩散模型
Days20504 分钟前
AI提示词管理器:解锁大模型高效应用的核心工具
大数据·人工智能
KaMeidebaby5 分钟前
卡梅德生物技术快报|抗体的制备与纯化:分子实验实操:番茄 sHSP 重组表达与抗体的制备与纯化工艺
前端·数据库·人工智能·其他·算法·百度·新浪微博
我爱cope6 分钟前
【Agent智能体8 | 反思设计模式-大语言模型反思机制的四个演进阶段】
人工智能·设计模式·语言模型
虹科网络安全6 分钟前
艾体宝洞察|“顶会”看安全(八):针对预训练大语言模型的仅标签成员推断攻击
人工智能·安全·语言模型
IT_陈寒7 分钟前
Vite热更新把我整不会了,原来还要这样配!
前端·人工智能·后端
skywalk81639 分钟前
使用llama.cpp运行模型unsloth/Qwen3.6-35B-A3B-UD-Q4_K_M.gguf 速度大约5.5 token/s
人工智能·llama
暴躁小师兄数据学院10 分钟前
【AI大模型应用开发工程师特训笔记】第04讲(第1章):Python基础与环境搭建
人工智能·笔记·python·ai
架构源启12 分钟前
Spring AI进阶系列(11) Spring AI Multi-Agent 协作系统:辩论、投票与共识机制实战
java·人工智能·spring