def edge_demo(image):
blurred = cv.GaussianBlur(image,(3,3),0)#高斯模糊降噪
gray = cv.cvtColor(blurred,cv.COLOR_BGR2GRAY)#灰度图
edge_output = cv.Canny(gray,50,150)#不求梯度也可以
return edge_output
def edge_area(image):
contours, hierarchy = cv.findContours(image, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)# 轮廓发现
dst = cv.cvtColor(image,cv.COLOR_GRAY2BGR)
cv.drawContours(dst, contours, -1, (0, 0, 255), 3) # 画出轮廓
area = 0
for c in range(len(contours)):
area += cv.contourArea(contours[c])# 面积
cv.putText(dst, "area/sum:" + str(area/image.size), (50, 50), cv.FONT_HERSHEY_SIMPLEX, .7, (255, 0, 0), 2)#显示
cv.imshow("t3",dst)
src = cv.imread("t1.jpg")
edge_output=edge_demo(src)
edge_area(edge_output)
结果是这个: