我正在学习OpenCv。我有一个斜齿轮图像来寻找 dentry 。
到现在为止,我一直试图找到轮廓,然后计算 dentry 。我能够找到轮廓以及轮廓的坐标。但我坚持算数。
由于我是OpenCV的新手,这可能是我尝试发现 dentry 不正确的方式。
我的代码:
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)
输入图像:
我得到的输出图像:
在此阶段之后,我该如何计算 dentry ?
我可以使用坐标来计算间隔并计算齿数。
还是在此阶段之后还有另一种计算 dentry 的方法?
最佳答案
解决方案的第一部分类似于@HansHirse发布的答案,但是我使用了另一种方法来计算 dentry 。我的完整代码可以在这里找到:link to full code for python3 opencv4。在继续操作之前,请检查齿轮的外轮廓是否正确检测到。如果未正确检测到齿轮,则其余答案将不起作用。
在数齿之前,我先“解开”齿轮。我通过扫一下齿轮并计算从齿轮中心到 dentry 外侧的距离来做到这一点。
这是我用来扫过齿轮并找到从齿轮中心到齿轮外部的距离的代码:
# Start at angle 0, and increment the angle 1/200 rad
angle = 0
increment = 1/200
# Create a list for the distances from the centroid to the edge of the gear tooth
distances = []
# Create an image for display purposes
display_image = raw_image.copy()
# Sweep around the circle (until one full revolution)
while angle < 2*math.pi:
# Compute a ray from the center of the circle with the current angle
img_size = max(raw_image.shape)
ray_end = int(math.sin(angle) * img_size + cX), int(math.cos(angle) * img_size + cY)
center = cX, cY
# Create mask
mask = np.zeros((raw_image.shape[0], raw_image.shape[1]), np.uint8)
# Draw a line on the mask
cv2.line(mask, center, ray_end, 255, 2)
# Mask out the gear slice (this is the portion of the gear the us below the line)
gear_slice = cv2.bitwise_and(raw_image, raw_image, mask = mask)
# Threshold the image
_, thresh = cv2.threshold(cv2.cvtColor(gear_slice, cv2.COLOR_BGR2GRAY), 0 , 255, 0)
# Find the contours in the edge_slice
_, edge_slice_contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Get the center of the edge slice contours
M = cv2.moments(max(edge_slice_contours, key = cv2.contourArea))
edge_location = int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])
cv2.circle(display_image, edge_location, 0, (0,255,0), 4)
# Find the distance from the center of the gear to the edge of the gear...at this specific angle
edge_center_distance = distance(center, edge_location)
# Find the xy coordinates for this point on the graph - draw blue circle
graph_point = int(angle*0.5*raw_image.shape[1]/math.pi), int(edge_center_distance+ 1.5*gear_radius)
cv2.circle(display_image, graph_point, 0, (0,255,0), 2)
# Add this distance to the list of distances
distances.append(-edge_center_distance)
# Create a temporary image and draw the ray on it
temp = display_image.copy()
cv2.line(temp, ray_end, (cX,cY), (0,0,255), 2)
# Show the image and wait
cv2.imshow('raw_image', temp)
vid_writer.write(temp)
k = cv2.waitKey(1)
if k == 27: break
# Increment the angle
angle += increment
# Clean up
cv2.destroyAllWindows()
结果是齿距齿轮中心的距离与角度成函数关系。
import matplotlib.pyplot as plt
plt.plot(distances)
plt.show()
现在,计算齿数要容易得多,因为它们是函数中的峰值(或者在这种情况下为山谷,更多信息稍后介绍)。为了计算峰数,我把
dentry 距离函数的Fourier transform。
import scipy.fftpack
# Calculate the Fourier transform
yf = scipy.fftpack.fft(distances)
fig, ax = plt.subplots()
# Plot the relevant part of the Fourier transform (a gear will have between 2 and 200 teeth)
ax.plot(yf[2:200])
plt.show()
傅立叶变换的峰值出现在37。因此,有37个谷和38个齿轮齿。
num_teeth = list(yf).index(max(yf[2:200])) - 1
print('Number of teeth in this gear: ' + str(num_teeth))
关于python - 通过python查找齿轮齿,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55282855/