OpenCV-Python 图像全景拼接stitch及黑边处理

        OpenCV版本:4.5.3.56

        算法实现思路:

  1. 图像拼接
  2. 全景轮廓提取
  3. 轮廓最小正矩形
  4. 腐蚀处理
  5. 裁剪

        代码实现:

import cv2
import numpy as np


def stitch(image):
    # 图像拼接
    # stitcher = cv2.createStitcher(False)  # OpenCV 3.X.X.X使用该方法
    stitcher = cv2.Stitcher_create(cv2.Stitcher_PANORAMA)  # OpenCV 4.X.X.X使用该方法,cv2.Stitcher_create()也可以
    status, pano = stitcher.stitch(image)

    # 黑边处理
    if status == cv2.Stitcher_OK:
        # 全景图轮廓提取
        stitched = cv2.copyMakeBorder(pano, 10, 10, 10, 10, cv2.BORDER_CONSTANT, (0, 0, 0))
        gray = cv2.cvtColor(stitched, cv2.COLOR_BGR2GRAY)
        thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1]
        cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]

        # 轮廓最小正矩形
        mask = np.zeros(thresh.shape, dtype="uint8")
        (x, y, w, h) = cv2.boundingRect(cnts[0])  # 取出list中的轮廓二值图,类型为numpy.ndarray
        cv2.rectangle(mask, (x, y), (x + w, y + h), 255, -1)

        # 腐蚀处理,直到minRect的像素值都为0
        minRect = mask.copy()
        sub = mask.copy()
        while cv2.countNonZero(sub) > 0:
            minRect = cv2.erode(minRect, None)
            sub = cv2.subtract(minRect, thresh)

        # 提取minRect轮廓并裁剪
        cnts = cv2.findContours(minRect, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
        (x, y, w, h) = cv2.boundingRect(cnts[0])
        stitched = stitched[y:y + h, x:x + w]

        cv2.imshow('stitched', stitched)
        cv2.imwrite('stitched.jpg', stitched)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    else:
        print('图像匹配的特征点不足')


if __name__ == "__main__":
    image1 = cv2.imread('data/space1.jpg')
    image2 = cv2.imread('data/space2.jpg')
    image3 = cv2.imread('data/space3.jpg')
    image = image1, image2, image3
    stitch(image)

        原图

图像拼接

# stitcher = cv2.createStitcher(False)  # OpenCV 3.X.X.X使用该方法
stitcher = cv2.Stitcher_create(cv2.Stitcher_PANORAMA)  # OpenCV 4.X.X.X使用该方法,cv2.Stitcher_create()也可以
status, pano = stitcher.stitch(image)

        OpenCV-Pythonstitch实现了图像拼接方法。在OpenCV 3.X.X.X系列版本中,使用cv2.createStitcher。在OpenCV 4.X.X.X系列版本中,使用cv2.Stitcher_create或者cv2.Stitcher_create,两者用法一致。

        stitch有两个返回值,一个是status,表示是否拼接成功;另一个是pano,当图像匹配的特征点足够时,拼接成功,返回全景图,当图像匹配的特征点不够时,拼接失败,返回None。效果如下:

黑边处理

        全景图拼接完成后,会出现图像边界外的黑色像素(0),使全景图不完美。可采取如下方法去除黑边:全景图轮廓提取轮廓最小正矩形腐蚀处理

全景图轮廓提取

# 全景图轮廓提取
stitched = cv2.copyMakeBorder(pano, 10, 10, 10, 10, cv2.BORDER_CONSTANT, (0, 0, 0))
gray = cv2.cvtColor(stitched, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]

轮廓最小正矩形

# 轮廓最小正矩形
mask = np.zeros(thresh.shape, dtype="uint8")
(x, y, w, h) = cv2.boundingRect(cnts[0])  # 取出list中的轮廓二值图,类型为numpy.ndarray
cv2.rectangle(mask, (x, y), (x + w, y + h), 255, -1)

腐蚀处理

# 腐蚀处理,直到minRect的像素值都为0
minRect = mask.copy()
sub = mask.copy()
while cv2.countNonZero(sub) > 0:
    minRect = cv2.erode(minRect, None)
    sub = cv2.subtract(minRect, thresh)

        创建了两个mask副本:

  • minRect,最小mask,将慢慢缩小尺寸,直到它可以放入全景图的内部
  • sub,判断minRect是否全黑(0)

         通过cv2.erode()腐蚀minRect,直到sub为全黑(0)。用类似动图演示,上面为sub,下面为minRect:

全景图

参考链接

OpenCV: samples/python/stitching.py

Image Stitching with OpenCV and Python - PyImageSearch

使用OpenCV和Python拼接图像_W_Tortoise的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/weixin_41611054/article/details/120340485