import cv2 import numpy as np def cv_show(name,img): cv2.imshow(name,img) cv2.waitKey() cv2.destroyAllWindows() def get_img(path1,path2): img1 = cv2.imread(path1) img2 = cv2.imread(path2) #原图像变换为灰度图 img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) return img1,img2,img1_gray,img2_gray def get_info(img1_gray,img2_gray): # 尺度不变特征变换 sift = cv2.SIFT_create() # 关键点以及特征向量计算 kp1, des1 = sift.detectAndCompute(img1_gray, None) kp2, des2 = sift.detectAndCompute(img2_gray, None) kp1 = np.float32([kp.pt for kp in kp1]) kp2 = np.float32([kp.pt for kp in kp2]) return kp1,des1,kp2,des2 def get_match(kp1,kp2,des1,des2): # 特征点交叉检验 bf = cv2.BFMatcher() matches = bf.knnMatch(des1, des2,k=2) idx_pair=[] for m,n in matches: if m.distance<n.distance*0.75: idx_pair.append((m.queryIdx,m.trainIdx)) if len(idx_pair)>4: pt_list1 = np.float32([kp1[i] for (i, _) in idx_pair]) pt_list2 = np.float32([kp1[i] for (_, i) in idx_pair]) H,_ = cv2.findHomography(pt_list2,pt_list1,cv2.RANSAC,4) result = cv2.warpPerspective(img2, H, (img1.shape[1] + img2.shape[1], img1.shape[0])) result[0:img1.shape[0], 0:img1.shape[1]] = img1 return result #main函数 path1='img1.jpg' path2='img2.jpg' img1,img2,img1_gray,img2_gray=get_img(path1,path2) kp1,des1,kp2,des2=get_info(img1_gray,img2_gray) result=get_match(kp1,des1,kp2,des2) cv_show('result',result)
时间: 2024-04-01 16:34:16 浏览: 13
这段代码是一个基于SIFT特征点匹配的图像拼接代码。代码中先读取了两张图片,然后将其转换为灰度图像,并利用SIFT算法提取各自的关键点和特征向量,之后利用BFMatcher进行特征点匹配,再进行交叉检验,得到匹配点对,进而利用findHomography计算出变换矩阵H,最后利用warpPerspective函数将图像2变换到图像1的位置,并将两幅图像拼接在一起。最后使用cv_show函数显示拼接后的图像。
需要注意的是,SIFT算法是一种有专利的算法,如果使用OpenCV 3.4.2及以后的版本,需要安装opencv-contrib-python才能使用SIFT算法。如果没有安装该库,可以使用SURF算法代替SIFT算法进行特征提取。同时,该代码还存在一些问题,比如在利用findHomography计算变换矩阵时,应该将pt_list2放在前面,pt_list1放在后面,否则会导致拼接的结果不正确。此外,在利用warpPerspective函数变换图像时,应该先将变换矩阵H取反,否则得到的结果也会不正确。
相关问题
import cv2 import numpy as np def cv_show(name,img): cv2.imshow(name,img) cv2.waitKey() cv2.destroyAllWindows() def get_img(path1,path2): img1 = cv2.imread(path1) img2 = cv2.imread(path2) img1 = cv2.resize(img1, (300, 400)) img2 = cv2.resize(img2, (300, 400)) #原图像变换为灰度图 img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) return img1,img2,img1_gray,img2_gray def get_info(img1_gray,img2_gray): # 尺度不变特征变换 sift = cv2.SIFT_create() # 关键点以及特征向量计算 kp1, des1 = sift.detectAndCompute(img1_gray, None) kp2, des2 = sift.detectAndCompute(img2_gray, None) kp1 = np.float32([kp.pt for kp in kp1]) kp2 = np.float32([kp.pt for kp in kp2]) return kp1,des1,kp2,des2 def get_match(kp1,kp2,des1,des2): # 特征点交叉检验 bf = cv2.BFMatcher() matches = bf.knnMatch(des1, des2,k=2) idx_pair=[] for m,n in matches: if m.distance<n.distance*0.75: idx_pair.append((m.queryIdx,m.trainIdx)) if len(idx_pair)>4: pt_list1 = np.float32([kp1[i] for (i, _) in idx_pair]) pt_list2 = np.float32([kp1[i] for (_, i) in idx_pair]) H,_ = cv2.findHomography(pt_list2,pt_list1,cv.RANSAC,4) result = cv2.warpPerspective(img2, H, (img1.shape[1] + img2.shape[1], img1.shape[0])) result[0:img1.shape[0], 0:img1.shape[1]] = img1 return result #main函数 path1='img1.jpg' path2='img2.jpg' img1,img2,img1_gray,img2_gray=get_img(path1,path2) kp1,des1,kp2,des2=get_info(img1_gray,img2_gray) result=get_match(kp1,des1,kp2,des2) cv_show('result',result)对以上代码debug
这段代码的功能是将两张图片进行拼接,其中使用了SIFT算法进行特征点提取和匹配,以及RANSAC算法计算单应性矩阵。对于代码本身,我看到以下几个问题:
1.第一行的import语句应该写在不同的行中,而不是在同一行中。
2.在函数get_match()中,第12行的cv.RANSAC应该改为cv2.RANSAC。
3.在函数get_match()中,第20行的kp1应该改为kp2。
4.在函数get_match()中,第22行的img2应该改为img1。
5.在主函数中,img1和img2的大小应该保持一致,否则无法进行拼接。
6.在主函数中,应该调用cv_show()函数来显示img1和img2,以便查看它们的内容。
7.在主函数中,应该将result保存为文件,而不是直接展示在屏幕上。
希望我的解答能够帮助到你!
修改此代码使其可重复运行import pygame import sys from pygame.locals import * from robomaster import * import cv2 import numpy as np focal_length = 750 # 焦距 known_radius = 2 # 已知球的半径 def calculate_distance(focal_length, known_radius, perceived_radius): distance = (known_radius * focal_length) / perceived_radius return distance def show_video(ep_robot, screen): 获取机器人第一视角图像帧 img = ep_robot.camera.read_cv2_image(strategy="newest") 转换图像格式,转换为pygame的surface对象 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.transpose(img) # 行列互换 img = pygame.surfarray.make_surface(img) screen.blit(img, (0, 0)) # 绘制图像 def detect_white_circle(ep_robot): 获取机器人第一视角图像帧 img = ep_robot.camera.read_cv2_image(strategy="newest") 转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 进行中值滤波处理 gray = cv2.medianBlur(gray, 5) 检测圆形轮廓 circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 50, param1=160, param2=40, minRadius=5, maxRadius=60) if circles is not None: circles = np.uint16(np.around(circles)) for circle in circles[0, :]: center = (circle[0], circle[1]) known_radius = circle 在图像上绘制圆形轮廓 cv2.circle(img, center, known_radius, (0, 255, 0), 2) 显示图像 distance = calculate_distance(focal_length, known_radius, known_radius) 在图像上绘制圆和距离 cv2.circle(img, center, known_radius, (0, 255, 0), 2) cv2.putText(img, f"Distance: {distance:.2f} cm", (center[0] - known_radius, center[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) cv2.imshow("White Circle Detection", img) cv2.waitKey(1) def main(): pygame.init() screen_size = width, height = 1280, 720 screen = pygame.display.set_mode(screen_size) ep_robot = robot.Robot() ep_robot.initialize(conn_type='ap') version = ep_robot.get_version() print("Robot version: {0}".format(version)) ep_robot.camera.start_video_stream(display=False) pygame.time.wait(100) clock = pygame.time.Clock() while True: clock.tick(5) # 将帧数设置为25帧 for event in pygame.event.get(): if event.type == QUIT: ep_robot.close() pygame.quit() sys.exit() show_video(ep_robot, screen) detect_white_circle(ep_robot) if name == 'main': main()
修改后的代码如下所示:
```python
import pygame
import sys
from pygame.locals import *
from robomaster import robot
import cv2
import numpy as np
focal_length = 750 # 焦距
known_radius = 2 # 已知球的半径
def calculate_distance(focal_length, known_radius, perceived_radius):
distance = (known_radius * focal_length) / perceived_radius
return distance
def show_video(ep_robot, screen):
# 获取机器人第一视角图像帧
img = ep_robot.camera.read_cv2_image(strategy="newest")
# 转换图像格式,转换为pygame的surface对象
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.transpose(img) # 行列互换
img = pygame.surfarray.make_surface(img)
screen.blit(img, (0, 0)) # 绘制图像
def detect_white_circle(ep_robot):
# 获取机器人第一视角图像帧
img = ep_robot.camera.read_cv2_image(strategy="newest")
# 转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 进行中值滤波处理
gray = cv2.medianBlur(gray, 5)
# 检测圆形轮廓
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 50, param1=160, param2=40, minRadius=5, maxRadius=60)
if circles is not None:
circles = np.uint16(np.around(circles))
for circle in circles[0, :]:
center = (circle[0], circle[1])
known_radius = circle[2]
# 在图像上绘制圆形轮廓
cv2.circle(img, center, known_radius, (0, 255, 0), 2)
# 计算距离
distance = calculate_distance(focal_length, known_radius, known_radius)
# 在图像上绘制圆和距离
cv2.circle(img, center, known_radius, (0, 255, 0), 2)
cv2.putText(img, f"Distance: {distance:.2f} cm", (center[0] - known_radius, center[1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv2.imshow("White Circle Detection", img)
cv2.waitKey(1)
def main():
pygame.init()
screen_size = width, height = 1280, 720
screen = pygame.display.set_mode(screen_size)
ep_robot = robot.Robot()
ep_robot.initialize(conn_type='ap')
version = ep_robot.get_version()
print("Robot version: {0}".format(version))
ep_robot.camera.start_video_stream(display=False)
pygame.time.wait(100)
clock = pygame.time.Clock()
while True:
clock.tick(5) # 将帧数设置为25帧
for event in pygame.event.get():
if event.type == QUIT:
ep_robot.close()
pygame.quit()
sys.exit()
show_video(ep_robot, screen)
detect_white_circle(ep_robot)
if __name__ == '__main__':
main()
```