// 车辆计数Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "车辆计数.h"
#include "车辆计数Dlg.h"
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <cxcore.h>
#include <stdlib.h>
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"highgui.lib")
#pragma comment(lib,"cxcore.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
CWnd *pwnd = NULL;//窗口指针对象,指向某一窗口
CDC *pdc = NULL;//DC句柄指针
HDC hdc;//DC句柄
CRect rect; //指定区域
HANDLE hThread1;
DWORD ThreadID1;//创建线程里参数(CarCount)
int pause; //暂停 继续 标志
int stop; //停止符 stop=1停止
int first=1;//控制第一次创建线程
struct node{ //定义数据结构 用于存储一帧中车辆重心位置
int x;//记录车辆中心的横坐标
int y;//记录车辆中心的纵坐标
node *car_next;
};
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_STOP, OnStop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
pwnd=GetDlgItem(IDC_IMAGE); //获取显示视频图像的控件ID
pdc =pwnd->GetDC();
hdc=pdc->GetSafeHdc(); //获取句柄
pwnd->GetClientRect(rect); //获取控件的RECT
pause=1;//初始化 非暂停状态
stop=1;
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CarCount()
{BeginCarCount:
IplImage* pFrame = NULL;
IplImage* pFrontImg = NULL;
IplImage* pBackImg = NULL;
CvMat* pFrameMat = NULL;
CvMat* pFrontMat = NULL;
CvMat* pBackMat = NULL;
CvCapture* pCapture = NULL;
CvvImage myimg;//用于将图片显示至PIC空间区所使用的CvvImage类
CvMemStorage * storage = cvCreateMemStorage(0);//轮廓边缘提取时的参数
CvSeq * contour = 0;//轮廓边缘提取时的参数
CvPoint dot1,dot2,dot3,dot4,dot5,dot6,dot7,dot8,dot9,dot10,dot11,dot12,dot13,dot14;//dot3,dot4记录车的位置(长方形的对角点位置)
//标记车辆计数检测区域的直线两点 dot1,,dot2,dot12,dot13为计数区域,(0,0)为图像左下角点
dot1.x=27;
dot1.y=156;
dot2.x=221;
dot2.y=126;
//标记车道分界线的六个点坐标
dot5.x=160;
dot5.y=240;
dot6.x=0;
dot6.y=70;
dot7.x=185;
dot7.y=240;
dot8.x=20;
dot8.y=0;
dot9.x=206;
dot9.y=240;
dot10.x=121;
dot10.y=0;
//标记车辆检测区域
dot11.x=122;
dot11.y=230;
dot12.x=0;
dot12.y=135;
dot13.x=219;
dot13.y=100;
dot14.x=229;
dot14.y=230;
int avgX,avgY;//记录车的重心位置
CvRect bndRect=cvRect(0,0,0,0);//用cvBoundingRect画出外接矩形时需要的矩形
int numpFrame=0;//记录帧数
char NumRoad1[10],NumRoad2[10],NumRoad3[10],NumRoad4[10];//记录每个车道上车的数量
int numroad1=0,numroad2=0,numroad3=0,numroad4=0;//记录每个车道上车的数量
node *CarLocationHead,*car1,*car2,*CarLocationHead1,*CarLocationHead2;//记录一帧中所有车的位置
CarLocationHead=0;
CarLocationHead1=0;
CarLocationHead2=0;
int newcar;//记录是否有新车进入界面,newcar=1为有
////////////////////提取背景
ContinueBackground:
pCapture=cvCaptureFromFile("highwayII_raw.avi");
while(pFrame = cvQueryFrame( pCapture ) )
{
numpFrame++;
if(numpFrame>500)goto OverBackground;
if(numpFrame==1)
{
pBackImg = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U,1);
pFrontImg = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U,1);
pBackMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pFrontMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pFrameMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
cvCvtColor(pFrame, pBackImg, CV_BGR2GRAY);//转为灰度图像
cvCvtColor(pFrame, pFrontImg, CV_BGR2GRAY);//转为灰度图像d
cvConvert(pFrontImg, pFrameMat);
cvConvert(pFrontImg, pFrontMat);
cvConvert(pFrontImg, pBackMat);
}
else
{ //转为灰度图像
cvCvtColor(pFrame, pFrontImg, CV_BGR2GRAY);
cvConvert(pFrontImg, pFrameMat);
//高斯滤波先,以平滑图像
cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0);
//更新背景
cvRunningAvg(pFrameMat, pBackMat, 0.005, 0);
}
}
if(numpFrame<500)goto ContinueBackground;
OverBackground:
/////////////////车辆计数开始
pCapture=cvCaptureFromFile("highwayII_raw.avi");
while((pFrame = cvQueryFrame( pCapture ) )&& stop==0)
{
//转�
评论0