答案 0 :(得分:1)
您好,请尝试下面的代码,其准确度为90-90%,看是否有帮助
import cv2
img=cv2.imread('bolt.jpg')
img_gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
inv_img=cv2.bitwise_not(img_gray)
cv2.imwrite('image1.jpg',inv_img)
res,thresh_img=cv2.threshold(inv_img,202,255,cv2.THRESH_BINARY_INV)
cv2.imwrite('image2.jpg',thresh_img)
thresh_img=255- thresh_img
im2, contours, hierarchy = cv2.findContours(thresh_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
sum1=0
for c in contours:
area=cv2.contourArea(c)
if area>4:
print (area)
sum1+=1
print sum1
抱歉,对于硬编码阈值,我假设背景和光照条件不会改变。