计数齿轮(Python、OpenCV)
对于原型,我需要建立一个齿轮的3d模型。这有一个“多”个牙齿。 因此,我尝试使用OpenCV和Python计算它们。我找到了(只)帖子,它解释了如何在C++中完成它。p> 我正在遵循这些步骤,目前这是我编写的代码计数齿轮(Python、OpenCV),python,opencv,Python,Opencv,对于原型,我需要建立一个齿轮的3d模型。这有一个“多”个牙齿。 因此,我尝试使用OpenCV和Python计算它们。我找到了(只)帖子,它解释了如何在C++中完成它。p> 我正在遵循这些步骤,目前这是我编写的代码 import numpy as np import cv2 img = cv2.imread('C:\\Users\\Link\\Desktop\\gear.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thres
import numpy as np
import cv2
img = cv2.imread('C:\\Users\\Link\\Desktop\\gear.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
kernel = np.ones((3, 3), np.uint8)
img_erosion = cv2.erode(thresh, kernel, iterations=1)
edges = cv2.Canny(img_erosion, 50, 150)
img_dilate = cv2.dilate(edges, kernel, iterations=1)
cv2.imshow('i', thresh)
cv2.waitKey(0)
cv2.imshow('i', img_erosion)
cv2.waitKey(0)
cv2.imshow('i', edges)
cv2.waitKey(0)
cv2.imshow('i', img_dilate)
cv2.waitKey(0)
阻止我继续前进的是:图像在某一点上变得非常混乱
这是我正在研究的原件:
这是图像放大的输出
如您所见,底部的牙齿显示不正确,可能是因为原始图像中的阴影。我怎样才能摆脱它?解决了它
这是代码。数到一是错误的,因为右边的一颗牙齿比其他牙齿低,而且它自己找到了两个点。我不知道为什么会这样
而且,它是用另一个图像制作的。这不是我上面发布的源代码,只要它是低清晰度的
import numpy as np
import cv2
img = cv2.imread('C:\\Users\\Link\\Desktop\\gear.png')
img2 = cv2.imread('C:\\Users\\Link\\Desktop\\gear.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
kernel = np.ones((3, 3), np.uint8)
img_dilate = cv2.dilate(thresh, kernel, iterations=1)
im2, contours, hierarchy = cv2.findContours(img_dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cv2.drawContours(img, contours, -1, (0, 255, 0), -1)
edges = cv2.Canny(cnts, 350, 350)
cnt = contours[0]
hull = cv2.convexHull(cnt, returnPoints=False)
defects = cv2.convexityDefects(cnt, hull)
for i in range(defects.shape[0]):
s, e, f, d = defects[i, 0]
start = tuple(cnt[s][0])
end = tuple(cnt[e][0])
far = tuple(cnt[f][0])
cv2.line(edges, start, end, [0, 255, 255], 1)
circles = cv2.circle(img2, end, 5, [0, 255, 0], -1)
# print(len(defects)) - number of points
cv2.imshow('thresh', thresh)
cv2.waitKey(0)
cv2.imshow('dilate', img_dilate)
cv2.waitKey(0)
cv2.imshow('edges', edges)
cv2.waitKey(0)
cv2.imshow('cnts', cnts)
cv2.waitKey(0)
cv2.imshow('points', circles)
cv2.waitKey(0)
解决了这个问题
这是代码。数到一是错误的,因为右边的一颗牙齿比其他牙齿低,而且它自己找到了两个点。我不知道为什么会这样
而且,它是用另一个图像制作的。这不是我上面发布的源代码,只要它是低清晰度的
import numpy as np
import cv2
img = cv2.imread('C:\\Users\\Link\\Desktop\\gear.png')
img2 = cv2.imread('C:\\Users\\Link\\Desktop\\gear.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
kernel = np.ones((3, 3), np.uint8)
img_dilate = cv2.dilate(thresh, kernel, iterations=1)
im2, contours, hierarchy = cv2.findContours(img_dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cv2.drawContours(img, contours, -1, (0, 255, 0), -1)
edges = cv2.Canny(cnts, 350, 350)
cnt = contours[0]
hull = cv2.convexHull(cnt, returnPoints=False)
defects = cv2.convexityDefects(cnt, hull)
for i in range(defects.shape[0]):
s, e, f, d = defects[i, 0]
start = tuple(cnt[s][0])
end = tuple(cnt[e][0])
far = tuple(cnt[f][0])
cv2.line(edges, start, end, [0, 255, 255], 1)
circles = cv2.circle(img2, end, 5, [0, 255, 0], -1)
# print(len(defects)) - number of points
cv2.imshow('thresh', thresh)
cv2.waitKey(0)
cv2.imshow('dilate', img_dilate)
cv2.waitKey(0)
cv2.imshow('edges', edges)
cv2.waitKey(0)
cv2.imshow('cnts', cnts)
cv2.waitKey(0)
cv2.imshow('points', circles)
cv2.waitKey(0)
因为你的源图像比你文章的链接更干净,所以你可以在最大面积轮廓上做近似,然后得到一半的点数,结果是
84
示例代码:
#!/usr/bin/python3
# 2018.01.22 11:53:24 CST
import cv2
import myutils
## Read
img = cv2.imread("img13_2.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
## threshold and find contours
ret, threshed = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY_INV)
cnts= cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2]
## Find the max-area-contour
cnt = max(contours, key=cv2.contourArea)
## Approx the contour
arclen = cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, 0.002*arclen, True)
## Draw and output the result
for pt in approx:
cv2.circle(img, (pt[0][0],pt[0][1]), 3, (0,255,0), -1, cv2.LINE_AA)
msg = "Total: {}".format(len(approx)//2)
cv2.putText(img, msg, (20,40),cv2.FONT_HERSHEY_PLAIN, 2, (0,0,255), 2, cv2.LINE_AA)
## Display
cv2.imshow("res", img);cv2.waitKey()
结果:
因为你的源图像比你文章的链接更干净,所以你可以在最大面积轮廓上做近似,然后得到一半的点数,结果是
84
示例代码:
#!/usr/bin/python3
# 2018.01.22 11:53:24 CST
import cv2
import myutils
## Read
img = cv2.imread("img13_2.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
## threshold and find contours
ret, threshed = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY_INV)
cnts= cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2]
## Find the max-area-contour
cnt = max(contours, key=cv2.contourArea)
## Approx the contour
arclen = cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, 0.002*arclen, True)
## Draw and output the result
for pt in approx:
cv2.circle(img, (pt[0][0],pt[0][1]), 3, (0,255,0), -1, cv2.LINE_AA)
msg = "Total: {}".format(len(approx)//2)
cv2.putText(img, msg, (20,40),cv2.FONT_HERSHEY_PLAIN, 2, (0,0,255), 2, cv2.LINE_AA)
## Display
cv2.imshow("res", img);cv2.waitKey()
结果:
尝试LineSegmentDetector检查阈值图像是否静止ok@Micka你能举个例子吗?我正在管理它来处理另一个图像,但如果我能找到解决方案,可能会更好..尝试LineSegmentDetector检查阈值图像是否仍然存在ok@Micka你能举个例子吗?我正在管理它来处理另一个图像,但如果我能找到解决方案,可能会更好。因此,根据这个答案,我的错误是:没有考虑阈值方法和aprox。。这意味着什么?
cv2.approxPolyDP(cnt,0.002*arclen,True)
?它用于以指定精度近似多边形曲线。我将此图像的精度设置为0.002*arclen
。并在需要时调整比率0.002。因此,根据这个答案,我的错误是:没有考虑阈值法和aprox。。这意味着什么?cv2.approxPolyDP(cnt,0.002*arclen,True)
?它用于以指定精度近似多边形曲线。我将此图像的精度设置为0.002*arclen
。并在需要时调整比率0.002
。