为运动检测设置指定区域

我需要指定将发生运动检测的区域。我要做的是计算通过某个区域的车辆数量。下面是我的代码:


private static void ProcessFrame(Mat backgroundFrame, int threshold, int erodeIterations, int dilateIterations)

{

    // Find difference between background (first) frame and current frame

    CvInvoke.AbsDiff(backgroundFrame, rawFrame, diffFrame);


    // Apply binary threshold to grayscale image (white pixel will mark difference)

    CvInvoke.CvtColor(diffFrame, grayscaleDiffFrame, ColorConversion.Bgr2Gray);

    CvInvoke.Threshold(grayscaleDiffFrame, binaryDiffFrame, threshold, 255, ThresholdType.Binary);


    // Remove noise with opening operation (erosion followed by dilation)

    CvInvoke.Erode(binaryDiffFrame, denoisedDiffFrame, null, new Point(-1, -1), erodeIterations, BorderType.Default, new MCvScalar(1));

    CvInvoke.Dilate(denoisedDiffFrame, denoisedDiffFrame, null, new Point(-1, -1), dilateIterations, BorderType.Default, new MCvScalar(1));


    rawFrame.CopyTo(finalFrame);

    //Rectangle rec = new Rectangle(100, 100, 100, 100);

    //finalFrame = crop_color_frame(rawFrame, rec);

    var img = crop_color_frame(denoisedDiffFrame, rec);

    DetectObject(denoisedDiffFrame, finalFrame);

}


static int vnum = 0;

private static void DetectObject(Mat detectionFrame, Mat displayFrame)

{

    using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())

    {

        // Build list of contours

        CvInvoke.FindContours(detectionFrame, contours, null, RetrType.List, ChainApproxMethod.ChainApproxSimple);


        // Selecting largest contour

        if (contours.Size > 0)

        {

            double maxArea = 0;

            int chosen = 0;

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

            {


目前,此代码可用于检测车辆,但我只是使用


if(center.Y >= 100 && maxArea > 20000) 条件开始计算车辆


这种方法的问题是,框架中的所有运动都受到监控。这就是为什么我只需要设置一个特定的区域。


你能告诉我怎么做吗?


森林海
浏览 118回答 1
1回答

紫衣仙女

您可以为输入图像设置 ROIpublic static Mat crop_roi(Mat input_img){&nbsp; &nbsp; Image<Gray, byte> img = input_img.ToImage<Gray, byte>();&nbsp; &nbsp; double w = input_img.Width;&nbsp; &nbsp; double h = input_img.Height;&nbsp; &nbsp; Rectangle r = new Rectangle((int)(w * 0.2), (int)(h * 0.4), (int)(w * 0.6), (int)(h * 0.6));&nbsp; &nbsp; Image<Gray, byte> output = img.Copy(r);&nbsp; &nbsp; return output.Mat;}//USEprivate static void DetectObject(Mat detectionFrame, Mat displayFrame){&nbsp; &nbsp; using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())&nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //set roi to the frame&nbsp; &nbsp; &nbsp; &nbsp; Mat roi = new Mat()&nbsp; &nbsp; &nbsp; &nbsp; roi = set_roi(detectionFrame);&nbsp; &nbsp; &nbsp; &nbsp; // Build list of contours&nbsp; &nbsp; &nbsp; &nbsp; CvInvoke.FindContours(roi , contours, null, RetrType.List, ChainApproxMethod.ChainApproxSimple);&nbsp; &nbsp; &nbsp; &nbsp; // Selecting largest contour&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; MarkDetectedObject(roi , contours[chosen], maxArea, contours.Size, maxArea);&nbsp;}下面是我在一张图片中绘制 ROI 的图片,您可以通过更改此行中的参数来调整 ROIRectangle r = new Rectangle((int)(w * 0.2), (int)(h * 0.4), (int)(w * 0.6), (int)(h * 0.6));
打开App,查看更多内容
随时随地看视频慕课网APP