I am learning OpenCv. I have a helical gear image to find teeth.
Till now I have tried to find the contours, and then count the teeth. I am able to find the contour also the coordinates of the contour. But I stuck to count the teeth. As I am new in OpenCV may be the way I am trying to finding the teeth is not correct.
My code:
import cv2
import numpy as np
import scipy as sp
import imutils
from skimage.morphology import reconstruction
import csv
raw_image = cv2.imread('./Gear Image/new1.jpg')
#cv2.imshow('Original Image', raw_image)
#cv2.waitKey(0)
bilateral_filtered_image = cv2.bilateralFilter(raw_image, 5, 175, 175)
#cv2.imshow('Bilateral', bilateral_filtered_image)
#cv2.waitKey(0)
edge_detected_image = cv2.Canny(bilateral_filtered_image, 75, 200)
#cv2.imshow('Edge', edge_detected_image)
#cv2.waitKey(0)
contours, hierarchy = cv2.findContours(edge_detected_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contour_list = []
for contour in contours:
approx = cv2.approxPolyDP(contour,0.01*cv2.arcLength(contour,True),True)
area = cv2.contourArea(contour)
if ((len(approx) > 5) & (len(approx) < 25) & (area > 50) ):
contour_list.append(contour)
cv2.drawContours(raw_image, contour_list, -1, (255,0,0), 2)
c = max(contours, key = cv2.contourArea)
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
cv2.circle(raw_image, (cX, cY), 5, (142, 152, 100), -1)
cv2.putText(raw_image, "centroid", (cX - 25, cY - 25),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (142, 152, 100), 2)
contour_length = "Number of contours detected: {}".format(len(contours))
cv2.putText(raw_image,contour_length , (20,40), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (142, 152, 100), 2)
for c in range(len(contours)):
n_contour = contours[c]
for d in range(len(n_contour)):
XY_Coordinates = n_contour[d]
print(len(coordinates))
print(XY_Coordinates)
print(type(XY_Coordinates))
print(XY_Coordinates[0,[0]])
print(XY_Coordinates[0,[1]])
cv2.imshow('Objects Detected',raw_image)
cv2.waitKey(0)
After this stage, how can I calculate the teeth? I can use the coordinates to calculate the interval and calculate the teeth.
or is there another way to calculate the teeth after this stage?