当前位置:网站首页>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)




边栏推荐
- Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
- 微服务开发步骤(nacos)
- Problem note - Oracle 11g uninstall
- solidty-基础篇-基础语法和定义函数
- It's suitable for people who don't have eloquence. The benefits of joining the China Video partner program are really delicious. One video gets 3 benefits
- idea中新建的XML文件变成普通文件的解决方法.
- 2022-2-15 learning xiangniuke project - Section 1 filtering sensitive words
- Solid basic basic grammar and definition function
- Buuctf reinforcement question ezsql
- 深度分析数据在内存中的存储形式
猜你喜欢

对于编程思想和能力有重大提升的书有哪些?

The first word of JVM -- detailed introduction to JVM and analysis of runtime data area

C learning notes (5) class and inheritance

Word2vec yyds dry goods inventory
![[leetcode 324] 摆动排序 II 思维+排序](/img/cb/26d89e1a1f548b75a5ef9f29eebeee.png)
[leetcode 324] 摆动排序 II 思维+排序

JVM第二话 -- JVM内存模型以及垃圾回收

建立自己的网站(14)

竣达技术丨多台精密空调微信云监控方案

Chapter 4 of getting started with MySQL: creation, modification and deletion of data tables

微服务追踪SQL(支持Isto管控下的gorm查询追踪)
随机推荐
DirectX repair tool v4.1 public beta! [easy to understand]
leetcode:329. 矩阵中的最长递增路径
One of the first steps to redis
Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
Day-02 database
微信网页订阅消息实现
TypeScript:var
购物商城6.27待完成
IDEA全局搜索快捷键(ctrl+shift+F)失效修复
The first word of JVM -- detailed introduction to JVM and analysis of runtime data area
After twists and turns, I finally found the method of SRC vulnerability mining [recommended collection]
2022-2-15 learning xiangniuke project - Section 4 business management
JVM第一话 -- JVM入门详解以及运行时数据区分析
音乐播放器开发实例(可毕设)
Hidden rules of the workplace that must be understood before 30
微信公众号订阅消息 wx-open-subscribe 的实现及闭坑指南
[stage life summary] I gave up the postgraduate entrance examination and participated in the work. I have successfully graduated and just received my graduation certificate yesterday
Tensorflow 2. X realizes iris classification
Internet hospital system source code hospital applet source code smart hospital source code online consultation system source code
NPDP能给产品经理带来什么价值?你都知道了吗?