首页 > 解决方案 > 如何通过 Python OpenCV 单击检测到的边缘?

问题描述

使用以下代码,我可以检测矩形的边缘,但现在我想点击矩形的每个边缘?我怎么能做到这一点?

import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib
import win32api

matplotlib.rcParams["savefig.dpi"] = 400 # to get high resolution

img = cv2.imread('1.png');
plt.imshow( img )
plt.title('Original Image')
plt.show()

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply Canny edge detection method on the image 
edges = cv2.Canny(gray,75,150,apertureSize = 3) 

plt.imshow(edges )
plt.title('My Image edges')
plt.show()

#lines = cv2.HoughLines(edges,1,np.pi/180, 200) 
lines = cv2.HoughLinesP(edges,1,np.pi/180, 150) 
print(lines)

标签: pythonopencvimage-processingedge-detection

解决方案


You can use the following:

if (lines.any() == True): 
    for line in lines: 
        x1,y1, _, _ = line[0]
        cv2.Rectangle(img,(x1,y1),(x1+3,y1+3),(0,255,0),3)
        cv2.imshow('image', img)
        cv2.waitKey(0)
        cv2.destroyAllWindow()

this code will go throw all the edges one by one


推荐阅读