亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為運動檢測設置指定區域

為運動檢測設置指定區域

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));

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

查看完整回答
反對 回復 2022-07-10
  • 1 回答
  • 0 關注
  • 102 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號