溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

膨脹處理提取車輛的大致輪廓

發(fā)布時間:2020-07-08 02:07:11 來源:網(wǎng)絡(luò) 閱讀:1851 作者:fengyuzaitu 欄目:編程語言

場景

    車輛通過幀間差分,捕獲到輪廓的線條,而大部分的線條是不連續(xù)的,無法鏈接成一個矩形的大致區(qū)域,通過最小矩形獲取到的區(qū)域,可能僅僅是車輛的一部分,可能是車牌,也可能是車頂,甚至可能是車的鏡頭,提出了兩種解決方案:

1)嘗試合并兩個相近的區(qū)域,然后找到一個比較大的矩形

2)利用膨脹的方法,對相鄰的區(qū)域進行合并,效果還是可以的


代碼

#include "opencv2/highgui/highgui.hpp"

#include "opencv2/imgproc/imgproc.hpp"

#include <vector>


using namespace cv;

using namespace std;


int main(int argc,char *argv[])

{

  VideoCapture videoCap("E:/smoky-cars/positive/大慶東路與水機路交叉口(東北)_冀BU0157_02_141502_01_3_50.wh364");

  if(!videoCap.isOpened())

  {

    return -1;

  }

  double videoFPS=videoCap.get(CV_CAP_PROP_FPS);  //獲取幀率

  double videoPause=1000/videoFPS;

  Mat framePrePre; //上上一幀

  Mat framePre; //上一幀

  Mat frameNow; //當前幀

  Mat frameDet; //運動物體

  videoCap>>framePrePre;

  videoCap>>framePre;

  cvtColor(framePrePre,framePrePre,CV_RGB2GRAY);

  cvtColor(framePre,framePre,CV_RGB2GRAY);

  int save=0;

  while(true)

  {

    videoCap>>frameNow;

    if(frameNow.empty()||waitKey(videoPause)==27)

    {

      break;

    }

    cvtColor(frameNow,frameNow,CV_RGB2GRAY);

    Mat Det1;

    Mat Det2;

    absdiff(framePrePre,framePre,Det1);  //幀差1

    absdiff(framePre,frameNow,Det2);     //幀差2

    threshold(Det1,Det1,0,255,CV_THRESH_OTSU);  //自適應(yīng)閾值化

    threshold(Det2,Det2,0,255,CV_THRESH_OTSU);

    Mat element=getStructuringElement(0,Size(3,3));  //膨脹核

    dilate(Det1,Det1,element);    //膨脹

    dilate(Det2,Det2,element);

    bitwise_and(Det1,Det2,frameDet);

    framePrePre=framePre;

    framePre=frameNow;

    imshow("Video",frameNow);



    int dilation_type;

    int dilation_elem = 0;

    int dilation_size = 10;

    if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }

    else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }

    else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }


    Mat dielem = getStructuringElement( dilation_type,

      Size( 2*dilation_size + 1, 2*dilation_size+1 ),

      Point( dilation_size, dilation_size ) );

    ///膨脹操作

    dilate( frameDet, frameDet, dielem );

    imshow("Detection",frameDet);

    //尋找最外層輪廓  

    vector<vector<Point>> contours;  

    vector<Vec4i> hierarchy;  

    findContours(frameDet, contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point()); 

    Mat p_w_picpathContours=Mat::zeros(frameDet.size(),CV_8UC1); //最小外接矩形畫布 

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

    {         

      //繪制輪廓  

      drawContours(p_w_picpathContours,contours,i,Scalar(255, 0, 0),1,8,hierarchy);  


      //繪制輪廓的最小外結(jié)矩形  

      RotatedRect rect=minAreaRect(contours[i]);  

      Point2f P[4];  

      rect.points(P);  

      for(int j=0;j<=3;j++)  

      {  

        line(p_w_picpathContours,P[j],P[(j+1)%4],Scalar(255, 0, 0),2);  

      }  


    }  

    imshow("MinAreaRect",p_w_picpathContours); 

    waitKey(1000);

  }

  return 0;

}


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI