首页 文章

使用OpenCV检测两个圆圈

提问于
浏览
0

我试图通过检测其中的圆圈来检测这个“煎饼”是否有缺陷,这是一个在制造过程中产生的孔 .

到目前为止,我已经设法检测到了大圆圈,即洞穴煎饼,但我无法检测到这个小圆圈 .

如果有人知道为什么我没有检测到它,那将会有很多帮助 . 这是我正在使用的代码:

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

和图像:

2 回答

  • 0

    如果这个洞不是圆形而是更多的撕裂怎么办?

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

  • 0

    您可以关注此图像处理管道:将图像转换为灰色 - >反转图像 - >均衡图像 - >使用高通滤镜对图像进行卷积 - >运行sobelx并在图像上进行sobely - >阈值图片 . 如果你仍然有一些噪音,你可以形态去消除它们 . 我在每个舞台上分享图像 . 一旦你有一个独特的图像,你可以运行fincontours()函数 .

相关问题