Opencv: How do I count thread peaks on a bolt?

Viewed 1007

I have the set of images with bolts. Two of them are shown below.
I need to count thread peaks.

I have tried Harris corner detector and HoughLines transform without any success.

Any ideas?

enter image description here

enter image description here

1 Answers

Hello try below code its having accuracy of 90-90% see if help

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)

enter image description here

res,thresh_img=cv2.threshold(inv_img,202,255,cv2.THRESH_BINARY_INV) 
cv2.imwrite('image2.jpg',thresh_img)

enter image description here

  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

sorry, for hardcoding threshold value I assume the background and light condition won't change.

Related