opencv 如何找到图像上特定点的坐标?

zpgglvta  于 2023-11-22  发布在  其他
关注(0)|答案(1)|浏览(165)

我试图在图像中获得特定的坐标。我在图像上手动标记了点和矩形(图2)在几个位置,向您展示我想要获得的坐标(白色点表示墙壁,窗口为粉红色圆点,列为矩形).我使用python和opencv来做这件事,但我不确定我是否在正确的轨道上。(图1). figure 1figure 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]

字符串
First attemp
编辑:- 我要恢复除门窗区域外的墙中心线。Here is an image for that-对于门窗,我要考虑以下事项:Doors/windows

qq24tv8q

qq24tv8q1#

代码需要一些调整添加了一个步骤,在原始图像上绘制绿色圆圈,以直观地标记检测到的角点,并根据其响应强度对角点进行排序,这使得更容易识别最重要的角点,并调整了Harris角点检测的参数,以更好地适应您的特定图像和应用程序。

更新示例:

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()

字符串

相关问题