提问人:user14874585 提问时间:11/1/2023 最后编辑:user14874585 更新时间:11/1/2023 访问量:51
如何在图像上找到特定点的坐标?
How to find coords of a specific point on an image?
问:
我正在尝试在图像中获取特定坐标。我在图像(图 2)的几个位置手动标记了点和矩形,以显示我想要获取的坐标(白点表示墙壁,粉红色圆点表示窗口,矩形表示列)。我正在使用 python 和 opencv 来做到这一点,但我不确定我是否走在正确的轨道上。这是我正在处理的图像示例(图 1)。图 1 图 2 这是我的第一次尝试:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
cv2_imshow(gray)
dst = cv2.cornerHarris(gray,5,3,0.04)
ret, dst = cv2.threshold(dst,0.1*dst.max(),255,0)
dst = np.uint8(dst)
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)
for i in range(1, len(corners)):
print(corners[i])
img[dst>0.1*dst.max()]=[255,255,255]
答:
0赞
ma9
11/1/2023
#1
代码需要一些调整 添加了在原始图像上绘制绿色圆圈的步骤,以直观地标记检测到的角点和 按响应强度对角点进行排序,这样可以更容易地识别最重要的角和 调整了哈里斯角点检测的参数,以更好地适应您的特定图像和应用。
更新的示例:
import cv2
import numpy as np
# Load the image
img = cv2.imread('jCIUR.jpg')
# Convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Calculate the Harris corner response function
gray = np.float32(gray)
dst = cv2.cornerHarris(gray, 2, 3, 0.04) # You can adjust the parameters
# Threshold the Harris response to find corner points
ret, dst = cv2.threshold(dst, 0.1 * dst.max(), 255, 0)
dst = np.uint8(dst)
# Find connected components and refine corner points
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
corners = cv2.cornerSubPix(gray, np.float32(centroids), (5, 5), (-1, -1), criteria)
# Print and draw the corner points on the original image
for corner in corners:
x, y = corner[0], corner[1]
print(f"Corner at ({x},{y})")
cv2.circle(img, (int(x), int(y)), 5, (0, 255, 0), -1) # Draw a green circle
# Display the image with marked corners
cv2.imshow('Corners', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
评论
0赞
user14874585
11/1/2023
积分很大,而我只需要几个。我编辑了我的帖子,请看一下。
评论