计数齿轮(Python、OpenCV)

2024-05-15 09:23:00 发布

您现在位置:Python中文网/ 问答频道 /正文

对于一个原型,我需要建立一个齿轮的三维模型。这有很多牙齿。 所以我试着用OpenCV和Python计算它们。我找到了this(只有?)这篇文章解释了如何在C++中完成。在

我正在按照步骤操作,现在这是我编写的代码。在

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)

阻止我继续前进的是:图像在某个时刻变得一团糟。在

这是我正在研究的原件:

src

这是image_dilate的输出

o1

如您所见,底部的牙齿没有正确显示,可能是因为原始图像中的阴影。我怎样才能摆脱这个?在


Tags: importimgnpcv2kernelimshowedges牙齿
2条回答

解决了。。在

这是密码。数错了一个,因为右边的一颗牙齿比其他牙齿低,而且它自己找到了两个点。不知道为什么会这样。在

而且,它是用另一个图像制作的。这不是我在上面发布的来源,只要它是低清晰度的。在

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)

output

因为你的源图像比链接你的帖子更干净,所以你可以在最大区域轮廓上做近似运算,然后得到一半的点数,结果是84。在

enter image description here


样本代码:

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

结果:

enter image description here

相关问题 更多 >