从灰度图像中的相似颜色轮廓中获取斑点

信息处理 图像处理 opencv
2022-02-10 07:38:47

我想在这些图像中选择工作人员行作为单独的 blob:

在此处输入图像描述 在此处输入图像描述

这可能吗?

1个回答

这是 C++ 中识别要检测的对象的简单步骤!希望这能解决您的问题!


void main()
{
    Mat mSource_Bgr,mSource_Gray,mSmoothed,mEdge,mThres;
    mSource_Bgr= imread(FileName_S.c_str(),IMREAD_COLOR);
    cvtColor(mSource_Bgr,mSource_Gray,COLOR_BGR2GRAY);

    medianBlur(mSource_Gray,mSmoothed,5); /*Just to smooth the Edges*/
/*This is for Automatically calculating the Canny Threshold values*/
    double CannyAccThresh = threshold(mSmoothed,mThres,0,255,CV_THRESH_BINARY|CV_THRESH_OTSU);
    double CannyThresh = 0.1 * CannyAccThresh;

    Canny(mSmoothed,mEdge,CannyThresh,CannyAccThresh); 

    Mat kernal_E,kernal_D;
    int erosion_size=2,dilation_size=2;
    kernal_E= getStructuringElement( MORPH_RECT,Size( 2*erosion_size + 1, 2*erosion_size+1 ),
                                       Point( erosion_size, erosion_size ) );
    kernal_D= getStructuringElement( MORPH_RECT,Size( 2*dilation_size + 1, 2*dilation_size+1 ),
                                       Point( dilation_size, dilation_size ) );
    dilate(mEdge,mEdge,kernal_D);//Just to make sure all the Edges are closed
    erode(mEdge,mEdge,kernal_E);

    imshow("mCanny_Edge",mEdge);
    imwrite(FileName_S+ "mCanny_Edge.bmp",mEdge);

在此处输入图像描述

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    /// Find contours
    findContours( mEdge.clone(), contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
    /// Approximate contours to polygons + get bounding rects and circles
    vector<vector<Point> > contours_poly( contours.size() );
    for( size_t i = 0; i < contours.size(); i++ )
    { 
        approxPolyDP( Mat(contours[i]), contours_poly[i], 1, true );
    }

    for (int i = 0; i < contours_poly.size(); i++)
    {
        if(contours_poly[i].size()>100)//Just a filter You can add more here
            drawContours(mSource_Bgr,contours_poly,i,Scalar(0,255,0),2);
    }
    imshow("mResult",mSource_Bgr);

在此处输入图像描述 }