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




边栏推荐
- Storage form of in-depth analysis data in memory
- En utilisant le paquet npoi de net Core 6 c #, lisez Excel.. Image dans la cellule xlsx et stockée sur le serveur spécifié
- Word2vec yyds dry goods inventory
- What problems should be considered for outdoor LED display?
- [zero basic IOT pwn] reproduce Netgear wnap320 rce
- [dynamic programming] p1004 grid access (four-dimensional DP template question)
- 241. 为运算表达式设计优先级
- 对于编程思想和能力有重大提升的书有哪些?
- The first technology podcast month will be broadcast soon
- Quelle valeur le pdnp peut - il apporter aux gestionnaires de produits? Vous savez tout?
猜你喜欢
![After twists and turns, I finally found the method of SRC vulnerability mining [recommended collection]](/img/ac/ab6053e6ea449beedf434d4cf07dbb.png)
After twists and turns, I finally found the method of SRC vulnerability mining [recommended collection]

In hot summer, please put away this safe gas use guide!
![[15. Interval consolidation]](/img/6c/afc46a0e0d14127d2c234ed9a9d03b.png)
[15. Interval consolidation]

MIT team used graph neural network to accelerate the screening of amorphous polymer electrolytes and promote the development of next-generation lithium battery technology
![[zero basic IOT pwn] reproduce Netgear wnap320 rce](/img/f7/d683df1d4b1b032164a529d3d94615.png)
[zero basic IOT pwn] reproduce Netgear wnap320 rce

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

Cannot link redis when redis is enabled

One of the first steps to redis

One of the data Lake series | you must love to read the history of minimalist data platforms, from data warehouse, data lake to Lake warehouse

Don't want to knock the code? Here comes the chance
随机推荐
JVM second conversation -- JVM memory model and garbage collection
【锁】Redis锁 处理并发 原子性
111. Minimum depth of binary tree
适合没口才的人做,加入中视频伙伴计划收益是真香,一个视频拿3份收益
Configuration of ZABBIX API and PHP
Storage form of in-depth analysis data in memory
手把手带你入门 API 开发
首届技术播客月开播在即
Solid basic structure and array, private / public function, return value and modifier of function, event
关于软件测试的一些思考
一波三折,终于找到src漏洞挖掘的方法了【建议收藏】
Official announcement: Apache Doris graduated successfully and became the top project of ASF!
Written on the first day after Doris graduated
solidty-基础篇-基础语法和定义函数
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
Microservice development steps (Nacos)
Mongodb second talk - - mongodb High available Cluster Implementation
En utilisant le paquet npoi de net Core 6 c #, lisez Excel.. Image dans la cellule xlsx et stockée sur le serveur spécifié
qt捕获界面为图片或label显示
职场太老实,总被欺负怎么办?