OpenCV 전경 이미지 조립의 실현 예시
left_01.jpg
right_01.jpg
Stitcher.py
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, featuresA) = self.detectAndDescribe(imageA)
(kpsB, featuresB) = self.detectAndDescribe(imageB)
# ,
M = self.matchKeypoints(kpsA, kpsB, featuresA, featuresB, 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 cv_show(self,name,img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def detectAndDescribe(self, image):
#
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# SIFT
descriptor = cv2.xfeatures2d.SIFT_create()
# SIFT ,
(kps, features) = descriptor.detectAndCompute(image, None)
# NumPy
kps = np.float32([kp.pt for kp in kps])
# ,
return (kps, features)
def matchKeypoints(self, kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh):
#
matcher = cv2.BFMatcher()
# KNN A、B SIFT ,K=2
rawMatches = matcher.knnMatch(featuresA, featuresB, 2)
matches = []
for m in rawMatches:
# ratio ,
if len(m) == 2 and m[0].distance < m[1].distance * ratio:
# featuresA, featuresB
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)
#
return (matches, H, status)
# 4 , None
return None
def drawMatches(self, imageA, imageB, kpsA, kpsB, matches, status):
# , A、B
(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
ImageStiching.py
from Stitcher import Stitcher
import cv2
#
imageA = cv2.imread("left_01.jpg")
imageB = cv2.imread("right_01.jpg")
#
stitcher = Stitcher()
(result, vis) = stitcher.stitch([imageA, imageB], showMatches=True)
#
cv2.imshow("Image A", imageA)
cv2.imshow("Image B", imageB)
cv2.imshow("Keypoint Matches", vis)
cv2.imshow("Result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
실행 결과:다음 오류 가 발생 하면:
cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv_contrib\modules\xfeatures2d\src\sift.cpp:1207: error: (-213:The function/feature is not implemented) This algorithm is patented and is excluded in this configuration; Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function ‘cv::xfeatures2d::SIFT::create'
OpenCV 프로그램 알림 알고리즘 을 실행 하면 저작권 문 제 는 낮은 버 전의 opencv-contrib-python 을 설치 하여 해결 할 수 있 습 니 다.
pip install --user opencv-contrib-python==3.3.0.10
여기 서 OpenCV 전경 이미지 조합 에 관 한 예제 에 관 한 글 은 여기까지 소개 되 었 습 니 다.더 많은 관련 OpenCV 이미지 조합 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Visual Studio 2017에서 OpenCV 템플릿 프로젝트 만들기・Windows 7 Professional 64bit ・Visual Studio 2017 Version 15.9.14 · OpenCV 3.4.1 OpenCV의 도입 방법 등은 아래를 참조하십시오. Visual Stu...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.