查找并计算齿轮opencv

查找并计算齿轮opencv,opencv,Opencv,我需要一些opencv和齿轮检测方面的帮助 我的任务:从如下图像中计算齿轮齿数: 我尝试使用HoughCircles方法,但结果不好,请执行以下操作: 大津阈值: openCV Java包装器上的代码: Mat des = new Mat(sourceImg.rows(), sourceImg.cols(), sourceImg.type()); Imgproc.cvtColor(sourceImg, sourceImg, Imgproc.COLOR_BGR2GRAY,

我需要一些opencv和齿轮检测方面的帮助

我的任务:从如下图像中计算齿轮齿数:

我尝试使用HoughCircles方法,但结果不好,请执行以下操作:

大津阈值:

openCV Java包装器上的代码:

       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);
    }

我的任务的正确方法是什么?怎么数牙齿?谢谢你的回答。

这是我试过的。代码是C++的,但是你可以很容易地把它改编成java。 加载图像并将其大小调整为原来的一半

腐蚀图像,使用Canny检测边缘,然后放大连接边缘

查找轮廓并选择最大的轮廓

找到这个最大轮廓的凸出部分。凸轮中的点数将为您提供齿数的粗略值

这是最大的轮廓和凸出点:

我用下面的代码得到一个值77

    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;

这是我试过的。代码是C++的,但是你可以很容易地把它改编成java。 加载图像并将其大小调整为原来的一半

腐蚀图像,使用Canny检测边缘,然后放大连接边缘

查找轮廓并选择最大的轮廓

找到这个最大轮廓的凸出部分。凸轮中的点数将为您提供齿数的粗略值

这是最大的轮廓和凸出点:

我用下面的代码得到一个值77

    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;