2013-02-28 299 views
0

有沒有辦法在opencv中計算粗糙度輪廓?如何計算opencv中的粗糙度輪廓?

圖像樣本:https://docs.google.com/file/d/0ByS6Z5WRz-h2NDgySmJ6NnpId0U/edit?usp=sharing

UPDATE

我的用於計算粗糙度代碼:

周緣輪廓/凸包周長

nomeimg = 'Riscalate2/JPEG/e (5).jpg' 

img = cv2.imread(nomeimg) 

gray = cv2.imread(nomeimg,0)#convert grayscale adn binarize 

element = cv2.getStructuringElement(cv2.MORPH_CROSS,(6,6)) 
graydilate = cv2.erode(gray, element) #imgbnbin 

cv2.imshow('image',graydilate) 
cv2.waitKey(0) 

ret,thresh = cv2.threshold(graydilate,127,255,cv2.THRESH_BINARY_INV) # binarize 

imgbnbin = thresh 
cv2.imshow('bn',thresh) 
cv2.waitKey() 

#element = cv2.getStructuringElement(cv2.MORPH_CROSS,(2,2)) 
#element = np.ones((11,11),'uint8') 


contours, hierarchy = cv2.findContours(imgbnbin, cv2.RETR_TREE ,cv2.CHAIN_APPROX_SIMPLE) 
print(len(contours)) 


# Take only biggest contour basing on area 
Areacontours = list() 
calcarea = 0.0 
unicocnt = 0.0 
for i in range (0, len(contours)): 
    area = cv2.contourArea(contours[i]) 
    #print("area") 
    #print(area) 
    if (area > 90): #con 90 trova i segni e togli puntini 
     if (calcarea<area): 
      calcarea = area 
      unicocnt = contours[i] 

#ROUGHNESS 
perimeter = cv2.arcLength(unicocnt,True) 
hull = cv2.convexHull(unicocnt,returnPoints = False) 
hullperimeter = cv2.arcLength(hull,True) 

print("perimeter") 
print(perimeter) 
print("hullperimeter") 
print(hullperimeter) 


roughness = perimeter/hullperimeter 
print("roughness") 
print(roughness) 

錯誤:

Traceback (most recent call last): 
    File "C:\Python27\nuovefeature.py", line 417, in <module> 
    hullperimeter = cv2.arcLength(hull,True) 
error: ..\..\..\src\opencv\modules\imgproc\src\contours.cpp:1886: error: (-215) curve.checkVector(2) >= 0 && (curve.depth() == CV_32F || curve.depth() == CV_32S) 
+0

請包括你已經嘗試什麼,以及研究,你已經完成的工作。 – Patricia 2013-02-28 13:03:18

+0

或添加圖像並解釋您期望的輸出? – 2013-02-28 13:10:35

+0

我不試試anithink,教授問我是否有一個函數可以測量粗糙度輪廓,就像在matlab中一樣。 – postgres 2013-02-28 14:07:30

回答

1

該錯誤位於凸起的尋找線中。

hull = cv2.convexHull(unicocnt,returnPoints = False) 

默認情況下,標誌returnPointsTrue。然後返回的船體包含船體點或輪廓點的像素座標。通常用於繪製船體,查找其面積,周長等。

但是,如果將其指定爲False,則返回的值是輪廓中這些座標的索引。它通常用於查找凸性缺陷。

所以,你的代碼應該象下面這樣:

hull = cv2.convexHull(unicocnt) 

上的例子更詳細的說明,請參閱凸形輪廓部分this article.