查找和计数齿轮opencv [英] Find and count gear opencv
问题描述
我需要一些有关opencv和齿轮检测的帮助.
I need some help with opencv and gearwheel detection.
我的任务:从这样的图像中计算齿轮齿数:
My task: count gearwheel teeth from images like this:
我正在尝试使用HoughCircles方法,但结果不好,这是这样的:
Im trying to use HoughCircles method but got bad results lile this:
大津门槛:
代码(在openCV Java包装器上):
Code (on openCV Java wrapper):
Mat des = new Mat(sourceImg.rows(), sourceImg.cols(), sourceImg.type());
Imgproc.cvtColor(sourceImg, sourceImg, Imgproc.COLOR_BGR2GRAY, 4);
Imgproc.GaussianBlur(sourceImg,des, new Size(3,3),0,0);
Imgproc.threshold(des, des, 0, 255, Imgproc.THRESH_OTSU | Imgproc.THRESH_OTSU);
Imgproc.Canny(des, des, 0 , 1);
displayImage(Mat2BufferedImage(des));
Mat circles = new Mat();
Imgproc.HoughCircles(des, circles, Imgproc.CV_HOUGH_GRADIENT, 1.0, 50, 70.0, 30.0, 100, 0);
/// Draw the circles detected
for(int i = 0; i < circles.cols(); i++ )
{
double vCircle[] = circles.get(0,i);
if (vCircle == null)
break;
Point pt = new Point(Math.round(vCircle[0]), Math.round(vCircle[1]));
int radius = (int)Math.round(vCircle[2]);
// draw the found circle
Core.circle(des, pt, radius, new Scalar(255,255,255), 3);
Core.circle(des, pt, 3, new Scalar(255,0,0), 3);
}
执行任务的正确方法是什么?如何计算牙齿?感谢您的回答.
What is right way for my task? How to count teeth? Thanks for your answers.
推荐答案
这就是我尝试过的方法.该代码是C ++,但是您可以轻松地使其适应Java.
Here's what I tried. The code is in C++ but you can easily adapt it to Java.
-
加载图像并将其大小调整为一半大小
load the image and resize it to half the size
侵蚀图像,使用Canny检测边缘,然后膨胀以连接边缘
erode the image, use Canny to detect edges, then dilate to connect the edges
找到轮廓并选择最大的轮廓
find contours and choose the largest contour
找到此最大轮廓的凸包.凸包中的点数将为您提供大致的齿数
find the convexhull of this largest contour. Number of point in the convexhull will give you a rough value for the number of teeth
这是最大的轮廓和凸包点:
Here's the largest contour and the convexhull points:
使用以下代码,我得到的值为77.
I get a value of 77 with the following code.
Mat gray = imread("16atchc.jpg", 0);
Mat small, bw, er, kernel;
resize(gray, small, Size(), .5, .5);
kernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
erode(small, er, kernel);
Canny(er, bw, 50, 150);
dilate(bw, bw, kernel);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
int imax = 0, areamax = 0;
findContours(bw, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
for(int idx = 0; idx >= 0; idx = hierarchy[idx][0])
{
Rect rect = boundingRect(contours[idx]);
int area = rect.width * rect.height;
if (area > areamax)
{
areamax = area;
imax = idx;
}
}
vector<Point> hull;
convexHull(contours[imax], hull);
cout << contours[imax].size() << ", " << hull.size() << endl;
这篇关于查找和计数齿轮opencv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!