它的计算方法类似于面积/凸包区域之间的比率:
#calculating area from contour
area = cv2.contourArea(unicocnt)
#calculating hull and hull area
hull = cv2.convexHull(unicocnt)
hull_area = cv2.contourArea(hull)
#solidity
solidity = float(area)/hull_area
计算对于圆形图像,我得到的所有值都接近1,所以我想当我计算轮廓的面积时,我计算圆内的面积而不考虑内部是否是白色像素(轮廓是黑色)
图片示例:https://docs.google.com/file/d/0ByS6Z5WRz-h2b0JITFB4aHR0OWc/edit?usp=sharing
CODE:
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]
#calculating area from contour
area = cv2.contourArea(unicocnt)
#calculating hull and hull area
hull = cv2.convexHull(unicocnt)
hull_area = cv2.contourArea(hull)
#solidity
solidity = float(area)/hull_area
更新
我是这样做的:
ColoredArea = 0
for i in range(0,len(imgbnbin)):
a = imgbnbin[i]
for j in range (0, len(a)):
if (cv2.pointPolygonTest(hull, unicocnt) >= 0):
if (getPixel(x,y) == black):
ColoredArea = ColoredArea +1;
出现此错误:
if (cv2.pointPolygonTest(hull, unicocnt) >= 0):
TypeError: Required argument 'measureDist' (pos 3) not found
答案 0 :(得分:1)
尝试以下算法(伪代码):
int ColoredArea = 0;
for (x=MinX to MaxX)
for (y=MinY to MaxY)
if (cv2.pointPolygonTest(hull, pt(x,y)) >= 0)
if (getPixel(x,y) == black)
ColoredArea++;