Python implements image stitching

The examples in this article share the specific code of python to realize image stitching for your reference.

  1. Image to be stitched

  1. Image feature point matching results based on SIFT feature points and RANSAC method

  1. Image transformation result

  1. Code and precautions
import cv2
import numpy as np
 
 
def cv_show(name, image):
 cv2.imshow(name, image)
 cv2.waitKey(0)
 cv2.destroyAllWindows()
 
 
def detectAndCompute(image):
 image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 sift = cv2.xfeatures2d.SIFT_create()(kps, features)= sift.detectAndCompute(image, None)
 kps = np.float32([kp.pt for kp in kps]) #The points obtained need to be further converted before they can be used
 return(kps, features)
 
 
def matchKeyPoints(kpsA, kpsB, featuresA, featuresB, ratio =0.75, reprojThresh =4.0):
 # ratio is the recommended threshold for nearest neighbor matching
 # reprojThresh is the recommended threshold for random sampling consistency
 matcher = cv2.BFMatcher()
 rawMatches = matcher.knnMatch(featuresA, featuresB,2)
 matches =[]for m in rawMatches:iflen(m)==2 and m[0].distance < ratio * m[1].distance:
 matches.append((m[0].queryIdx, m[0].trainIdx))
 kpsA = np.float32([kpsA[m[0]]for m in matches]) #Use np.float32 conversion list
 kpsB = np.float32([kpsB[m[1]]for m in matches])(M, status)= cv2.findHomography(kpsA, kpsB, cv2.RANSAC, reprojThresh)return(M, matches, status) #Not all points have matching solutions, their status is stored in the status
 
 
def stich(imgA, imgB, M):
 result = cv2.warpPerspective(imgA, M,(imgA.shape[1]+ imgB.shape[1], imgA.shape[0]))
 result[0:imageA.shape[0],0:imageB.shape[1]]= imageB
 cv_show('result', result)
 
 
def drawMatches(imgA, imgB, kpsA, kpsB, matches, status):(hA, wA)= imgA.shape[0:2](hB, wB)= imgB.shape[0:2]
 # Note the 3 channels and uint8 type here
 drawImg = np.zeros((max(hA, hB), wA + wB,3),'uint8')
 drawImg[0:hB,0:wB]= imageB
 drawImg[0:hA, wB:]= imageA
 for((queryIdx, trainIdx),s)inzip(matches, status):if s ==1:
 # Note that float32--  int
 pt1 =(int(kpsB[trainIdx][0]),int(kpsB[trainIdx][1]))
 pt2 =(int(kpsA[trainIdx][0])+ wB,int(kpsA[trainIdx][1]))
 cv2.line(drawImg, pt1, pt2,(0,0,255))cv_show("drawImg", drawImg)
 
 
# Read image
imageA = cv2.imread('./right_01.png')cv_show("imageA", imageA)
imageB = cv2.imread('./left_01.png')cv_show("imageB", imageB)
# Calculate SIFT feature points and feature vectors(kpsA, featuresA)=detectAndCompute(imageA)(kpsB, featuresB)=detectAndCompute(imageB)
# Obtain a homography matrix based on nearest neighbor and random sampling consistency(M, matches, status)=matchKeyPoints(kpsA, kpsB, featuresA, featuresB)
# Draw matching results
drawMatches(imageA, imageB, kpsA, kpsB, matches, status)
# Splicing
stich(imageA, imageB, M)

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python implements image stitching
Python implements panoramic image stitching
Python implements image stitching function
python opencv for image stitching
Python implements horizontal stitching of pictures
Python dry goods | remote sensing image stitching
Python dry goods | remote sensing image stitching
Python implements image outer boundary tracking operation
Python implements Super Mario
Python implements tic-tac-toe game
Python implements tic-tac-toe game
Python implements man-machine gobang
Python implements Tetris game
python PIL open\display\save image
Python implements minesweeper game
Python realizes stitching pictures
Python implements scanning tools
python+OpenCV realizes image stitching
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements guessing game
Python implements simple tank battle
Python implements udp chat window
Python implements WeChat airplane game
Python implements word guessing game
Python implements a guessing game
Python on image processing PIL
Python implements digital bomb game
Python implements TCP file transfer
Python numpy implements rolling case
Python implements simple tic-tac-toe game
Python implements password strength verification
Python implements code block folding
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
How Python implements FTP function
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements gradient descent method
Python implements text version minesweeper
Python implements the brick-and-mortar game
Python reads .nii format image examples
Python implements student performance evaluation system
How Python implements the mail function
Python simply implements the snake game
Python3 implements the singleton design pattern
Python implements exchange rate conversion operations
Python implements string and number splicing
Python implements ten classic sorting algorithms
Python implements a universal web framework
Python implements 126 mailboxes to send mail
Python PIL library image graying processing
Python implements AI face change function
Python implements the steepest descent method
Python implements the actual banking system
Python implements digital bomb game program
Python realizes image recognition car function
Python implements ftp file transfer function
Python implements username and password verification
How Python implements the timer function