gpt4 book ai didi

python - 通过 python opencv 查找齿轮的 dentry

转载 作者:太空狗 更新时间:2023-10-29 23:56:34 61 4
gpt4 key购买 nike

我正在学习 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)

输入图像:Input Image

我得到的输出图像: OutPut Image

在这个阶段之后,我该如何计算 dentry ?我可以使用坐标来计算间隔并计算 dentry 。

或者在这个阶段之后还有其他方法可以计算 dentry 吗?

最佳答案

我的解决方案的第一部分类似于@HansHirse 发布的答案,但我使用了不同的方法来计算 dentry 数量。我的完整代码可以在这里找到:link to full code for python3 opencv4 .在继续之前检查是否正确检测到齿轮的外轮廓。如果未正确检测到齿轮,则其余答案将无效。

在数齿数之前,我“打开”了齿轮。我通过扫过齿轮并计算从齿轮中心到轮齿外侧的距离来做到这一点。 sweeping around the gear

这是我用来扫描齿轮并找到从齿轮中心到齿轮外侧的距离的代码:

# 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()

gear teeth as a function of angle

现在计算 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()

Fourier transform傅立叶变换的波峰出现在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 opencv 查找齿轮的 dentry ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55282855/

61 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com