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

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

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

  1. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  2. gray = np.float32(gray)
  3. cv2_imshow(gray)
  4. dst = cv2.cornerHarris(gray,5,3,0.04)
  5. ret, dst = cv2.threshold(dst,0.1*dst.max(),255,0)
  6. dst = np.uint8(dst)
  7. ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
  8. criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
  9. corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)
  10. for i in range(1, len(corners)):
  11. print(corners[i])
  12. img[dst>0.1*dst.max()]=[255,255,255]

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

qq24tv8q

qq24tv8q1#

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

更新示例:

  1. import cv2
  2. import numpy as np
  3. # Load the image
  4. img = cv2.imread('jCIUR.jpg')
  5. # Convert the image to grayscale
  6. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  7. # Calculate the Harris corner response function
  8. gray = np.float32(gray)
  9. dst = cv2.cornerHarris(gray, 2, 3, 0.04) # You can adjust the parameters
  10. # Threshold the Harris response to find corner points
  11. ret, dst = cv2.threshold(dst, 0.1 * dst.max(), 255, 0)
  12. dst = np.uint8(dst)
  13. # Find connected components and refine corner points
  14. ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
  15. criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
  16. corners = cv2.cornerSubPix(gray, np.float32(centroids), (5, 5), (-1, -1), criteria)
  17. # Print and draw the corner points on the original image
  18. for corner in corners:
  19. x, y = corner[0], corner[1]
  20. print(f"Corner at ({x},{y})")
  21. cv2.circle(img, (int(x), int(y)), 5, (0, 255, 0), -1) # Draw a green circle
  22. # Display the image with marked corners
  23. cv2.imshow('Corners', img)
  24. cv2.waitKey(0)
  25. cv2.destroyAllWindows()

字符串

展开查看全部

相关问题