我正在使用Python和OpenCV。我試圖找到電池的中心和角度:如何查找圖像中物體的中心和角度?
Image of batteries with random angles:
比我的代碼是這樣的:
import cv2
import numpy as np
img = cv2.imread('image/baterias2.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img2 = cv2.imread('image/baterias4.png',0)
minLineLength = 300
maxLineGap = 5
edges = cv2.Canny(img2,50,200)
cv2.imshow('Canny',edges)
lines = cv2.HoughLinesP(edges,1,np.pi/180,80,minLineLength,maxLineGap)
print lines
salida = np.zeros((img.shape[0],img.shape[1]))
for x in range(0, len(lines)):
for x1,y1,x2,y2 in lines[x]:
cv2.line(salida,(x1,y1),(x2,y2),(125,125,125),0)# rgb
cv2.imshow('final',salida)
cv2.imwrite('result/hough.jpg',img)
cv2.waitKey(0)
任何想法去解決它?
你試過了什麼? – saul
你好。我正在嘗試這個步驟:1.門檻,2. Canny,3.線條,但顯示我不完整的線條。 –
請分享您使用過的代碼以及當前的輸出結果嗎? – ZdaR