使用OpenCV检测两个圆圈

时间:2018-12-09 19:44:05

标签: c++ opencv

我试图通过检测其中的圆圈(即在其制造过程中产生的孔)来检测这种“薄煎饼”是否有缺陷。

到目前为止,我已经设法检测到了一个大圆孔,即孔煎饼,但我无法检测到一个小圆孔。

如果有人对为什么我没有发现它有所了解,那将有很大帮助。这是我正在使用的代码:

    // OpenCV.cpp : Este archivo contiene la función "main". La ejecución del programa comienza y termina ahí.
//

#include "pch.h"
#include <opencv2\opencv.hpp>
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"

#include <vector>
#include <fstream>
#include <iostream>
#include <math.h>
#include <Windows.h>

#include "opencv2/video/background_segm.hpp"

#include <iostream>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <stdio.h>

int history = 1;
float varThreshold = 16;
bool bShadowDetection = true;

using namespace cv;
using namespace std;
int main(int argc, char** argv) {

    std::clock_t start;
    double duracion;
    Mat buena = imread("C:/Users/Jorge/Downloads/ImagenesTortas/TortasUnitarias/Tortasbuenas/B1.bmp");
    Mat mala = imread("C:/Users/Jorge/Downloads/ImagenesTortas/TortasUnitarias/Tortasmalas/Agujereadas/A1.bmp");
    //Mat mala = imread("C:/Users/Jorge/Downloads/ImagenesTortas/TortasUnitarias/Tortasbuenas/B3.bmp");

    if (buena.empty() || mala.empty()) {
        cout << "Error : Image cannot be loaded..!!" << endl;
        return -1;
    }

    int offset_x = 70;
    int offset_y = 70;

    cv::Rect roi;
    roi.x = offset_x;
    roi.y = offset_y;
    roi.width = buena.size().width - (offset_x * 2);
    roi.height = buena.size().height - (offset_y * 2);

    /* Crop the original image to the defined ROI */

    buena = buena(roi);
    mala = mala(roi);

    start = std::clock();

    cvtColor(buena, buena, CV_BGR2HSV);
    cvtColor(mala, mala, CV_BGR2HSV);

    int tortaBuena = 0;
    int tortaMala = 0;
    int salto = 100;

    cvtColor(mala, mala, COLOR_BGR2GRAY);
    //medianBlur(mala, mala, 3);
    vector<Vec3f> circles;
    HoughCircles(mala, circles, CV_HOUGH_GRADIENT, 1,
        buena.rows / 1,  // change this value to detect circles with different distances to each other
        100, 30, 1, 150 // change the last two parameters
   // (min_radius & max_radius) to detect larger circles
    );
    std::cout << circles.at(0);
    for (size_t i = 0; i < circles.size(); i++)
    {
        Vec3i c = circles[i];
        Point center = Point(c[0], c[1]);
        // circle center
        circle(mala, center, 1, Scalar(0, 100, 100), 3,8);
        // circle outline
        int radius = c[2];
        circle(mala, center, radius, Scalar(255, 0, 255), 3, 8);
    }

    duracion = (std::clock() - start) / (double)CLOCKS_PER_SEC;
    std::cout << "Tiempo: " << duracion << '\n';


    //mostrar las imágenes
    namedWindow("Buena", CV_WINDOW_AUTOSIZE);
    imshow("Buena", buena);
    namedWindow("Mala", CV_WINDOW_AUTOSIZE);
    imshow("Mala", mala);

    waitKey(0);
    destroyWindow("Buena");
    destroyWindow("Mala");
}

图片:

enter image description here

2 个答案:

答案 0 :(得分:0)

如果孔不是圆形而是更多的泪怎么办?

我建议选择(或排除)背景色tutorial。对结果执行阈值以创建二进制图像。接下来,使用findContours检测薄煎饼和孔。

答案 1 :(得分:0)

您可以按照以下图像处理流程进行操作:将图像转换为灰度->反转图像->均衡图像->用高通滤波器对图像进行卷积->对图像运行sobelx和sobely- >对图像进行阈值处理。如果仍然有噪声,可以通过形态消除它们。我在每个阶段共享图像。一旦有了清晰的图像,就可以在其上运行fincontours()函数。

Gray Scale Invert Equalise High Pass Sobel Threshold