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

場景

成都創(chuàng)新互聯(lián)公司專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、三亞網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5建站、商城建設、集團公司官網(wǎng)建設、外貿網(wǎng)站制作、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為三亞等各大城市提供網(wǎng)站開發(fā)制作服務。

    車輛通過幀間差分,捕獲到輪廓的線條,而大部分的線條是不連續(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);  //自適應閾值化

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

      //繪制輪廓的最小外結矩形  

      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;

}

網(wǎng)頁名稱:膨脹處理提取車輛的大致輪廓
文章URL:http://bm7419.com/article24/jcioce.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、動態(tài)網(wǎng)站、微信公眾號、網(wǎng)頁設計公司網(wǎng)站改版、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設計