gpt4 book ai didi

python - 使用opencv和python进行激光曲线检测

转载 作者:太空宇宙 更新时间:2023-11-03 21:53:45 26 4
gpt4 key购买 nike

我把这张图的激光曲线去掉了:

original image
(来源:hostingpics.net)

curve

现在,我正在尝试获取一组点(越多越好),它们位于这条曲线的中间。我试图将图像分割成垂直条纹,然后检测质心。但是它并没有计算很多点,而且一点也不令人满意!

img = cv2.Canny(img,50,150,apertureSize = 3)
sub = 100
step=int(img.shape[1]/sub)
centroid=[]
for i in range(sub):
x0= i*step
x1=(i+1)*step-1
temp = img[:,x0:x1]
hierarchy,contours,_ = cv2.findContours(temp, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
if contours <> []:
for i in contours :
M = cv2.moments(i)
if M['m00'] <> 0:
centroid.append((x0+int(M['m10']/M['m00']),(int(M['m01']/M['m00']))))

我也试过 cv2.fitLine(),但也不尽如人意。我怎样才能有效地检测这条曲线中间的点?问候。

最佳答案

我认为由于以下两个原因,您获得的积分较少:

  • 使用边缘检测器:根据阈值,有时边缘可能无法合理地表示曲线
  • 使用大步对图像进行采样

尝试以下方法。

# threshold the image using a threshold value 0
ret, bw = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY)
# find contours of the binarized image
contours, heirarchy = cv2.findContours(bw, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# curves
curves = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)

for i in range(len(contours)):
# for each contour, draw the filled contour
draw = np.zeros((img.shape[0], img.shape[1]), np.uint8)
cv2.drawContours(draw, contours, i, (255,255,255), -1)
# for each column, calculate the centroid
for col in range(draw.shape[1]):
M = cv2.moments(draw[:, col])
if M['m00'] != 0:
x = col
y = int(M['m01']/M['m00'])
curves[y, x, :] = (0, 0, 255)

我得到这样的曲线:

enter image description here

您还可以使用距离变换,然后获取与单个轮廓的每一列的最大距离值关联的行。

关于python - 使用opencv和python进行激光曲线检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26561893/

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