改善OpenCV的检测

时间:2020-06-06 19:43:35

标签: c++ opencv detection threshold

我目前正在尝试检测巢中的卵。为此,我使用的是OpenCV。

首先,我用2个鸡蛋拍摄了一张照片。然后,我将其转换为HSV。并定义了阈值范围。

但是正如您所看到的,当我尝试显示阈值窗口时,并未删除所有内容。所以这是我的问题,我怎么只能检测到鸡蛋。

<------------原始-------------------- | ------------ --- HSV -------------------- | -----------------阈值------- ---> enter image description here

谢谢

2 个答案:

答案 0 :(得分:0)

如果您要从这一点继续,可以简单地将其应用于阈值输出minAreaRect()函数,该函数将帮助您为每个轮廓绘制一个合适的矩形。之后,您可以比较这些矩形的长度,也可以检查这些矩形的颜色密度以达到结果。

作为替代方案,我尝试了HoughCircle(),它也能够找到具有适当参数的卵。

这是带有houghcircle的结果和代码:

输入图像:

enter image description here

输出图像:

enter image description here

代码:

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
using namespace cv;
using namespace std;


int main()
{
    // Loads an image
    Mat src_hough_circle = imread("/ur/image/directory/eggs.png", IMREAD_COLOR );

    Mat gray;
    cvtColor(src_hough_circle, gray, COLOR_BGR2GRAY);

    Mat gray_segmentation = gray.clone();

    //medianBlur(gray, gray, 5);
    GaussianBlur(gray,gray,Size(5,5),0);
    vector<Vec3f> circles;
    HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
                 gray.rows/8,  // change this value to detect circles with different distances to each other
                 100, 30, 80, 170 // change the last two parameters
            // (min_radius & max_radius) to detect larger circles
    );
    for( size_t i = 0; i < circles.size(); i++ )
    {
        Vec3i c = circles[i];
        Point center = Point(c[0], c[1]);
        // circle center
        circle( src_hough_circle, center, 1, Scalar(0,100,100), 3, LINE_AA);
        // circle outline
        int radius = c[2];
        circle( src_hough_circle, center, radius, Scalar(255,0,255), 3, LINE_AA);
    }

    imshow("detected circles", src_hough_circle);
    waitKey(0);



    return 0;
}

答案 1 :(得分:0)

感谢Yunus,因为您是方法,这就是我所做的,我不知道它是否比您方法更好。 (对不起,我没有发表评论:()

Output Image

代码:

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;

int Compteur;
int Area;
int Contours;
Mat ImageSource;
Mat HSV;
Mat Image_Blur;
Mat Seuil;


int main() {

string FichierSource = "C:/Users/xxxxx/Desktop/TBT/cas.png";
ImageSource = imread(FichierSource);

cvtColor(ImageSource, HSV, COLOR_BGR2HSV);

inRange(HSV, Scalar(0, 39 , 149), Scalar(179, 255, 255), Seuil);

erode(Seuil, Seuil, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
dilate(Seuil, Seuil, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

dilate(Seuil, Seuil, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
erode(Seuil, Seuil, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

blur(Seuil, Image_Blur, Size(5, 5));

imshow("Image original", ImageSource);

imshow("Image convertie en HSV", HSV);

imshow("Image seuillé", Seuil);


Mat Threshold_Output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

threshold(Image_Blur, Threshold_Output, 100, 255, THRESH_BINARY);
findContours(Threshold_Output, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));

vector<RotatedRect> minRect(contours.size());
vector<RotatedRect> minEllipse(contours.size());

for (int i = 0; i < contours.size(); i++)
{

    Area = contourArea(contours[i]);
    Contours = contours[i].size();

    // -------------------------- DEBUG ZONE ---------------------------- 
    cout << "Contours de la zone : " << contourArea(contours[i]) << endl;
    cout << "Nombre de contours : " << contours[i].size() << endl;
    // ------------------------------------------------------------------ 

    if (Contours >= 90 && Contours <= 100) {
        if (Area >= 2000 && Area <= 3200) {
                minRect[i] = minAreaRect(Mat(contours[i]));
                minEllipse[i] = fitEllipse(Mat(contours[i]));
        }
            Compteur++;
    }
}

for (int i = 0; i < contours.size(); i++)
{
    Area = contourArea(contours[i]);
    Contours = contours[i].size();

    if (Contours >= 90 && Contours <= 100) {
        if (Area >= 2000 && Area <= 3200) {
            drawContours(ImageSource, contours, i, Scalar(0, 165, 255), 1, 8, vector<Vec4i>(), 0, Point());

            ellipse(ImageSource, minEllipse[i], Scalar(0, 165, 255), 2);

            Point2f rect_points[4]; minRect[i].points(rect_points);
            for (int j = 0; j < 4; j++)
                line(ImageSource, rect_points[j], rect_points[(j + 1) % 4], Scalar(0, 165, 255));
        }
    }
}

imshow("Image Analysée", ImageSource);

cout << "Nombre d'oeufs : " << Compteur;

waitKey(0);

}