OpenCV - C ++ - 如何近距离合并blob /边界框?

时间:2017-11-12 01:42:08

标签: c++ opencv computer-vision

提前致谢...

我正在努力检测移动视频中的车辆,并在此过程中计算已经过的车辆数量。

enter image description here

在这张图片中,您可以看到它可以很好地检测到轿车,但是大图像(总线)并没有像我希望的那样被表示为一个大的斑点。相反,它认为公共汽车实际上是一堆彼此相邻的汽车。

Psuedo代码看起来像这样:

if (blob[i].center is close to blob[i + 1].center)
    mergeBlobs;

以下代码之前的步骤:转换为灰色,帧差异,阈值,扩张(x2),侵蚀,找到计数器......

以下是我编写的大部分代码:

vector<vector<Point> > contours;                            //Not sure what this does but it works
findContours(dil, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);    
vector<vector<Point> > contours_poly(contours.size());              
vector<Rect> boundRect(contours.size());                            
vector<vector<Point> > hull(contours.size());

Mat output = Mat::zeros(dil.rows, dil.cols, CV_8UC3);
int counter = 0;                                                    
line(output, Point(0, output.rows / 2), Point(output.cols, output.rows / 2), Scalar(0, 255, 0), 3, 8);  //As cars pass this line, CarCount++

for (int i = 0; i < contours.size(); i++)
{
    approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);      
    boundRect[i] = boundingRect(Mat(contours_poly[i]));             
    convexHull(Mat(contours[i]), hull[i], false);                   

    if ((boundRect[i].width * boundRect[i].height) > 10000)         //If the bounding rectangle has an area less than 10,000 then just ignore it completely
    {
        counter = counter + 1;
        drawContours(output, hull, i, Scalar(255, 255, 255), CV_FILLED);    //draw contours of convexHull. 

        rectangle(output, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 0, 255), 2, 8, 0); //Draws the actual rectangle around the contours
        putText(output, to_string(counter), Point(boundRect[i].x, boundRect[i].y), FONT_HERSHEY_SIMPLEX, 3, Scalar(255, 0, 0), 3, 8);
        if ((boundRect[i].y > ((output.rows / 2) - 6)) && (boundRect[i].y < ((output.rows / 2) + 6)))
        {
            CarCount++;                                                 //Global car count
            Mat croppedImage = frameGray(Rect(boundRect[i].x, boundRect[i].y, boundRect[i].width, boundRect[i].height));
            imshow("Cropped Image", croppedImage);
        }

    }
}

0 个答案:

没有答案