为了账号安全,请及时绑定邮箱和手机立即绑定

为运动检测设置指定区域

为运动检测设置指定区域

C#
森林海 2022-07-10 16:11:36
我需要指定将发生运动检测的区域。我要做的是计算通过某个区域的车辆数量。下面是我的代码: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) 条件开始计算车辆这种方法的问题是,框架中的所有运动都受到监控。这就是为什么我只需要设置一个特定的区域。你能告诉我怎么做吗?
查看完整描述

1 回答

?
紫衣仙女

TA贡献1839条经验 获得超15个赞

您可以为输入图像设置 ROI


public static Mat crop_roi(Mat input_img)

{

    Image<Gray, byte> img = input_img.ToImage<Gray, byte>();

    double w = input_img.Width;

    double h = input_img.Height;

    Rectangle r = new Rectangle((int)(w * 0.2), (int)(h * 0.4), (int)(w * 0.6), (int)(h * 0.6));

    Image<Gray, byte> output = img.Copy(r);


    return output.Mat;

}


//USE

private static void DetectObject(Mat detectionFrame, Mat displayFrame)

{

    using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())

    { 

        //set roi to the frame

        Mat roi = new Mat()

        roi = set_roi(detectionFrame);


        // Build list of contours

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


        // Selecting largest contour

        ...


        MarkDetectedObject(roi , contours[chosen], maxArea, contours.Size, maxArea);


 }

下面是我在一张图片中绘制 ROI 的图片,您可以通过更改此行中的参数来调整 ROIRectangle r = new Rectangle((int)(w * 0.2), (int)(h * 0.4), (int)(w * 0.6), (int)(h * 0.6));

//img1.sycdn.imooc.com//62ca89ea000148ce07760696.jpg

查看完整回答
反对 回复 2022-07-10
  • 1 回答
  • 0 关注
  • 117 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号