当前位置:网站首页>Opencv Learning Notes 6 -- image mosaic
Opencv Learning Notes 6 -- image mosaic
2022-07-01 14:55:00 【Cloudy_ to_ sunny】
opencv Learning Notes 6 -- Image mosaic
import numpy as np
import cv2
import matplotlib.pyplot as plt#Matplotlib yes RGB
class Stitcher:
# Splicing function
def stitch(self, images, ratio=0.75, reprojThresh=4.0,showMatches=False):
# Get input picture
(imageB, imageA) = images
# testing A、B The image SIFT Key feature points , And calculate the feature descriptors
(kpsA, featuresA) = self.detectAndDescribe(imageA)
(kpsB, featuresB) = self.detectAndDescribe(imageB)
# Match all the feature points of the two pictures , Return matching results
M = self.matchKeypoints(kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh)
# If the returned result is empty , There is no feature point matching success , Exit algorithm
if M is None:
return None
# otherwise , Extract matching results
# H yes 3x3 Perspective transformation matrix
(matches, H, status) = M
# The picture A Change the angle of view ,result It's the transformed picture
result = cv2.warpPerspective(imageA, H, (imageA.shape[1] + imageB.shape[1], imageA.shape[0]))# The third parameter is the image size
self.cv_show('result', result)
# The picture B Pass in result At the far left of the picture
result[0:imageB.shape[0], 0:imageB.shape[1]] = imageB
self.cv_show('result', result)
# Check if you need to show a picture match
if showMatches:
# Generate matching pictures
vis = self.drawMatches(imageA, imageB, kpsA, kpsB, matches, status)
# Return results
return (result, vis)
# Return matching results
return result
# Show function
def cv_show(self,name,img):
b,g,r = cv2.split(img)
img_rgb = cv2.merge((r,g,b))
plt.imshow(img_rgb)
plt.show()
def cv_show1(self,name,img):
plt.imshow(img)
plt.show()
cv2.imshow(name,img)
cv2.waitKey()
cv2.destroyAllWindows()
# Calculate features and descriptions
def detectAndDescribe(self, image):
# Convert a color image to a grayscale image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# establish SIFT generator
descriptor = cv2.xfeatures2d.SIFT_create()
# testing SIFT Characteristic point , And calculate the descriptors
(kps, features) = descriptor.detectAndCompute(image, None)
# Convert the result to NumPy Array
kps = np.float32([kp.pt for kp in kps])
# Return to feature point set , And corresponding description features
return (kps, features)
# Matching key points
def matchKeypoints(self, kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh):
# Build violence matchers
matcher = cv2.BFMatcher()
# Use KNN The test comes from A、B Of Graphs SIFT Feature matching ,K=2
rawMatches = matcher.knnMatch(featuresA, featuresB, 2)#1 Yes 2 matching
matches = []
for m in rawMatches:
# When the ratio of the nearest distance to the next nearest distance is less than ratio When the value of , Keep this match
if len(m) == 2 and m[0].distance < m[1].distance * ratio:
# Store two points in featuresA, featuresB Index value in
matches.append((m[0].trainIdx, m[0].queryIdx))
# When the filtered match pair is greater than 4 when , Calculate the angle transformation matrix
if len(matches) > 4:
# Get the point coordinates of the matched pair
ptsA = np.float32([kpsA[i] for (_, i) in matches])
ptsB = np.float32([kpsB[i] for (i, _) in matches])
# Calculate the angle transformation matrix
(H, status) = cv2.findHomography(ptsA, ptsB, cv2.RANSAC, reprojThresh)
# Return results
return (matches, H, status)
# If the match pair is less than 4 when , return None
return None
# Draw matching points
def drawMatches(self, imageA, imageB, kpsA, kpsB, matches, status):
# Initialize visualization image , take A、B Connect left and right
(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
# Joint traversal , Draw a match
for ((trainIdx, queryIdx), s) in zip(matches, status):
# When the point pair match is successful , Draw on the visualization
if s == 1:
# Draw a match
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 visualization results
return vis
# Read the mosaic
imageA = cv2.imread("left_01.png")
imageB = cv2.imread("right_01.png")
# Make a panorama of the picture
stitcher = Stitcher()
(result, vis) = stitcher.stitch([imageA, imageB], showMatches=True)


# Show all pictures
stitcher.cv_show("Image A", imageA)
stitcher.cv_show("Image B", imageB)
stitcher.cv_show("Keypoint Matches", vis)
stitcher.cv_show("Result", result)




边栏推荐
- [14. Interval sum (discretization)]
- Reorganize the trivial knowledge points at the end of the term
- Basic operation of database
- 使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
- JVM第二话 -- JVM内存模型以及垃圾回收
- opencv学习笔记五--文件扫描+OCR文字识别
- Zabbix API与PHP的配置
- 定了!2022海南二级造价工程师考试时间确定!报名通道已开启!
- Minimum spanning tree and bipartite graph in graph theory (acwing template)
- solidty-基础篇-基础语法和定义函数
猜你喜欢

idea中新建的XML文件变成普通文件的解决方法.

2022-2-15 learning xiangniuke project - Section 4 business management

Minimum spanning tree and bipartite graph in graph theory (acwing template)

241. Design priorities for operational expressions

One of the first steps to redis

Sqlachemy common operations

Written on the first day after Doris graduated

Problem note - Oracle 11g uninstall

openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题

The first technology podcast month will be broadcast soon
随机推荐
241. Design priorities for operational expressions
Hidden rules of the workplace that must be understood before 30
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
[leetcode 324] 摆动排序 II 思维+排序
Apk signature principle
leetcode:329. 矩阵中的最长递增路径
It's settled! 2022 Hainan secondary cost engineer examination time is determined! The registration channel has been opened!
Word2vec yyds dry goods inventory
Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
炎炎夏日,这份安全用气指南请街坊们收好!
Vnctf2022 open web gocalc0
Solid basic basic grammar and definition function
JVM第二话 -- JVM内存模型以及垃圾回收
Chapter 4 of getting started with MySQL: creation, modification and deletion of data tables
Some thoughts on software testing
生成随机数(4位、6位)
【15. 区间合并】
【阶段人生总结】放弃考研,参与到工作中,已经顺利毕业了,昨天刚领毕业证
[14. Interval sum (discretization)]
Buuctf reinforcement question ezsql