我試圖使用OpenCV從它的背景中分割一個彎曲杆,然後找到彎曲並計算每個彎曲之間的角度。如何分割彎曲杆進行角度計算?
第一部分幸運的是前景和背景之間足夠的對比度是微不足道的。 當分割時,一點腐蝕/膨脹照顧反射/高光。
第二部分是我不知道如何處理它的地方。
我可以很容易地檢索輪廓(頂部和底部非常相似,所以要麼) ,但我似乎無法弄清楚如何將輪廓分割成直線部分和彎曲杆來計算角度。
到目前爲止,我已經嘗試過簡單地繪製輪廓,但是要麼我得到太多或太少的點,並且難以指定正確的 設置以保持筆直部分筆直並且彎曲部分簡化。
這裏是我的輸入圖像(bend.png)
而且這裏是我試過到目前爲止:
#!/usr/bin/env python
import numpy as np
import cv2
threshold = 229
# erosion/dilation kernel
kernel = np.ones((5,5),np.uint8)
# contour simplification
epsilon = 0
# slider callbacks
def onThreshold(x):
global threshold
print "threshold = ",x
threshold = x
def onEpsilon(x):
global epsilon
epsilon = x * 0.01
print "epsilon = ",epsilon
# make a window to add sliders/preview to
cv2.namedWindow('processed')
#make some sliders
cv2.createTrackbar('threshold','processed',60,255,onThreshold)
cv2.createTrackbar('epsilon','processed',1,1000,onEpsilon)
# load image
img = cv2.imread('bend.png',0)
# continuously process for quick feedback
while 1:
# exit on ESC key
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
# Threshold
ret,processed = cv2.threshold(img,threshold,255,0)
# Invert
processed = (255-processed)
# Dilate
processed = cv2.dilate(processed,kernel)
processed = cv2.erode(processed,kernel)
# Canny
processed = cv2.Canny(processed,100,200)
contours, hierarchy = cv2.findContours(processed,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
if len(contours) > 0:
approx = cv2.approxPolyDP(contours[0],epsilon,True)
# print len(approx)
cv2.drawContours(processed, [approx], -1, (255,255,255), 3)
demo = img.copy()
cv2.drawContours(demo, [approx], -1, (192,0,0), 3)
# show result
cv2.imshow('processed ',processed)
cv2.imshow('demo ',demo)
# exit
cv2.destroyAllWindows()
這裏就是我這麼遠,但我不相信這是最好的辦法:
我試圖在視覺上弄清楚了這一點,我所針對的是沿着這些路線的東西:
因爲最終的目標是計算彎曲部分的東西之間的夾角像這種感覺簡單:
我的假設是對INT之間的擬合直線和計算的角度ersecting線可以工作:
我沒有使用HoughLines OpenCV Python tutorial快速測試,但無論是參數的傳遞,我沒有得到很大的成績:
#!/usr/bin/env python
import numpy as np
import cv2
threshold = 229
minLineLength = 30
maxLineGap = 10
houghThresh = 15
# erosion/dilation kernel
kernel = np.ones((5,5),np.uint8)
# slider callbacks
def onMinLineLength(x):
global minLineLength
minLineLength = x
print "minLineLength = ",x
def onMaxLineGap(x):
global maxLineGap
maxLineGap = x
print "maxLineGap = ",x
def onHoughThresh(x):
global houghThresh
houghThresh = x
print "houghThresh = ",x
# make a window to add sliders/preview to
cv2.namedWindow('processed')
#make some sliders
cv2.createTrackbar('minLineLength','processed',1,50,onMinLineLength)
cv2.createTrackbar('maxLineGap','processed',5,30,onMaxLineGap)
cv2.createTrackbar('houghThresh','processed',15,50,onHoughThresh)
# load image
img = cv2.imread('bend.png',0)
# continuously process for quick feedback
while 1:
# exit on ESC key
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
# Threshold
ret,processed = cv2.threshold(img,threshold,255,0)
# Invert
processed = (255-processed)
# Dilate
processed = cv2.dilate(processed,kernel)
processed = cv2.erode(processed,kernel)
# Canny
processed = cv2.Canny(processed,100,200)
lineBottom = np.zeros(img.shape,np.uint8)
contours, hierarchy = cv2.findContours(processed,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
if len(contours) > 0:
cv2.drawContours(lineBottom, contours, 0, (255,255,255), 1)
# HoughLinesP
houghResult = img.copy()
lines = cv2.HoughLinesP(lineBottom,1,np.pi/180,houghThresh,minLineLength,maxLineGap)
try:
for x in range(0, len(lines)):
for x1,y1,x2,y2 in lines[x]:
cv2.line(houghResult,(x1,y1),(x2,y2),(0,255,0),2)
except Exception as e:
print e
# show result
cv2.imshow('lineBottom',lineBottom)
cv2.imshow('houghResult ',houghResult)
# exit
cv2.destroyAllWindows()
是這是一個可行的方法?如果是這樣,在OpenCV Python中進行線擬合的正確方法是什麼?
否則,這是解決此問題的最佳方法?
更新繼三木勸我試過的OpenCV 3的LSD和比HoughLinesP
得到更好的結果,但它看起來還是有一些調整需要,雖然它不看以外cv2.createLineSegmentDetector
不會有太多的選擇,玩:
我做了與approxPolyDP非常類似的事情,最終合併了幾乎共線的後續段/短段。 – Miki
另一種方法是使用LSD(線段檢測器)...我從來沒有使用它,但值得一試 – Miki
@Miki聽起來很有趣。我從approxPolyDP開始,但接下來不知道該怎麼做。所以,你基本上通過線路循環,如果線對之間的角度差異低於閾值你合併成一行,對吧? –