簡體   English   中英

計數齒輪(Python,OpenCV)

[英]Count gear (Python, OpenCV)

對於原型,我需要構建齒輪的3D模型。 這有很多齒。 因此,我嘗試使用OpenCV和Python計算它們的數量。 我發現了這篇 (僅?)帖子,它解釋了如何在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

如您所見,底部的牙齒未正確顯示,可能是由於原始圖像中的陰影所致。 我該如何擺脫呢?

因為您的源圖像比帖子的鏈接更干凈 ,所以您可以對max-area-contour進行近似處理 ,然后獲得一半的點數 ,結果為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()

結果:

在此處輸入圖片說明

解決了..

這是代碼。 一個人的計數是錯誤的,因為右邊的一顆牙齒比另外一顆低,並且它自己發現了兩點。 不知道為什么會這樣。

另外,它是用另一個圖像制作的。 只要它是低清晰度,它都不是我在上面發布的來源。

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)

產量

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM