目前我工作的一個項目,我嘗試使用OpenCV中找到矩形表面的照片的角落(的Python,Java或C++)OpenCV的:檢測有缺陷的矩形
我選擇所需表面通過過濾顏色,然後我有面具和它傳遞給cv2.findContours
:
cnts, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt = sorted(cnts, key = cv2.contourArea, reverse = True)[0]
peri = cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, 0.02*peri, True)
if len(approx) == 4:
cv2.drawContours(mask, [approx], -1, (255, 0, 0), 2)
使用cv2.HoughLines
我已經成功地得到4條直線準確地描述表面。它們的交叉點正是我需要的:
edged = cv2.Canny(mask, 10, 200)
hLines = cv2.HoughLines(edged, 2, np.pi/180, 200)
lines = []
for rho,theta in hLines[0]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv2.line(mask, (x1,y1), (x2,y2), (255, 0, 0), 2)
lines.append([[x1,y1],[x2,y2]])
的問題是:是否有可能以某種方式調整findContours
?
另一種解決辦法是找到交叉點的座標。這種方法的任何線索,歡迎:)
任何人可以給我一個提示如何解決這個問題呢?
「他們的交點正是我需要的」 - - 在這種情況下計算它們的交點。 –
你能給我一個線索我怎麼能做到這一點? – Aray
已經有很多線索可用,例如http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect或https://en.wikipedia.org/ wiki/Line%E2%80%93line_intersection或http://stackoverflow.com/a/7448287/3962537 –