在好例子网,分享、交流、成长!
您当前所在位置:首页Others 开发实例一般编程问题 → HumanDet(公交车系统模式识别)

HumanDet(公交车系统模式识别)

一般编程问题

下载此实例
  • 开发语言:Others
  • 实例大小:3.01M
  • 下载次数:7
  • 浏览次数:182
  • 发布时间:2020-01-13
  • 实例类别:一般编程问题
  • 发 布 人:robot666
  • 文件格式:.rar
  • 所需积分:2
 相关标签:

实例介绍

【实例简介】公交车系统模式识别,能够自动识别人头部位,统计某个座位的人数。

【实例截图】

from clipboard

【核心代码】

// HumanDetView.cpp : implementation of the CHumanDetView class
//

#include "stdafx.h"
#include "HumanDet.h"
#include "math.h"

#include "HumanDetDoc.h"
#include "HumanDetView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHumanDetView

IMPLEMENT_DYNCREATE(CHumanDetView, CScrollView)

BEGIN_MESSAGE_MAP(CHumanDetView, CScrollView)
	//{{AFX_MSG_MAP(CHumanDetView)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHumanDetView construction/destruction

CHumanDetView::CHumanDetView()
{
	// TODO: add construction code here
	image = 0;
	fnum = 0;
	fnames = 0;
	noPerson=0;
	m_nPersonNum=0;
	
	Img1 = 0;
	
	humanCircle.degree=0;
	humanCircle.r=0;
	humanCircle.x=0;
	humanCircle.y=0;
	//region1
	SReg1.x1= 60;
    SReg1.x2= 175;
	SReg1.y1 = 0;
	SReg1.y2 = 55;

	//region2
	SReg2.x1= 60;
    SReg2.x2= 175;
	SReg2.y1 = 55;
	SReg2.y2 = 110;
	
	//region3
	SReg3.x1= 60;
    SReg3.x2= 175;
	SReg3.y1 = 110;
	SReg3.y2 = 150;

}

CHumanDetView::~CHumanDetView()
{
	if( image )
		delete image;
	if( fnames )
		delete fnames;
	if( Img1 )
		delete Img1;

}

BOOL CHumanDetView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CHumanDetView drawing

void CHumanDetView::OnDraw(CDC* pDC)
{
	CHumanDetDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	if( image == 0 )
		return;

	int i, j;
	BYTE gray;
	BYTE g;
	CString str;

	for( i=0; i<height; i  )
	{
		for( j=0; j<width; j  )
		{
			//original image
			gray = image[i*width j];
			pDC->SetPixel( j, i, RGB(gray,gray,gray));

			//processed image
			g    = Img1[i*width j];
			pDC->SetPixel( j width 10, i, RGB(g,g,g));
		}
	}

	// indicate the position of head if a human is detected
	if( humanCircle.degree>3&&m_nRegFlag==1)
		pDC->TextOut( humanCircle.x 60 width 10, humanCircle.y, "x");
	if( humanCircle.degree>2&&m_nRegFlag==2)
		pDC->TextOut( humanCircle.x 60 width 10, humanCircle.y, "x");
	if( humanCircle.degree>2&&m_nRegFlag==3)
		pDC->TextOut( humanCircle.x 60 width 10, humanCircle.y, "x");

	str.Format("PersonNumber=%d",m_nPersonNum);
	pDC->TextOut(width 10,height 10,str);

}

void CHumanDetView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	if( image !=0 )
	{
		sizeTotal.cx = width;
		sizeTotal.cy = height;
	}
	else
	{
		sizeTotal.cx = 100;
		sizeTotal.cy = 100;
	}

	SetScrollSizes(MM_TEXT, sizeTotal);}

/////////////////////////////////////////////////////////////////////////////
// CHumanDetView printing

BOOL CHumanDetView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CHumanDetView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CHumanDetView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CHumanDetView diagnostics

#ifdef _DEBUG
void CHumanDetView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CHumanDetView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CHumanDetDoc* CHumanDetView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHumanDetDoc)));
	return (CHumanDetDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CHumanDetView message handlers

void CHumanDetView::OnFileOpen() 
{
	// TODO: Add your command handler code here
	CFileDialog MyFDlg(TRUE, NULL,NULL, OFN_HIDEREADONLY, NULL,NULL );

	MyFDlg.m_ofn.lpstrFilter = "(*.lst;*.raw)\0*.lst;*.raw\0";
	MyFDlg.m_ofn.lpstrInitialDir = "D:\\Huang\\Teaching\\ImagePro\\Programs\\5_HumanDet";

	if( MyFDlg.DoModal() == IDOK )
	{
		CString SFName;

		SFName = MyFDlg.GetPathName( ); //full name with path

		char *fnstr;
		fnstr = SFName.GetBuffer(4); //read the name from string 

		FILE *fpLst;
		int n;
		
		int len;
		len = strlen( fnstr );
		if( !strcmp( fnstr len-3, "raw" ) ) //raw image
		{
			fnum = 0;
			char *ptr;
			ptr = fnstr len-3;
			while( *ptr != '\\')
				ptr--;
			*ptr = 0;
			strcpy( directory, fnstr);//Directory

			fnames = new char[500];
			strcpy( fnames, ptr 1); //image name
		}
		else //list file
		{
			fpLst = fopen( fnstr, "rb");
			fscanf( fpLst, "%3d", &fnum);
			fscanf( fpLst, "%s", directory);//directory

			fnames = new char[fnum*100];
			for( n=0; n<fnum; n  )
				fscanf( fpLst, "%s", fnames n*100);// image names

			fclose(fpLst);
		}

		findex = 0;
		readImg( findex);
	}
}

void CHumanDetView::readImg( int findex)
{
	char fullName[120];
	sprintf( fullName, "%s\\%s", directory, fnames findex*100);

	FILE *fpImg;
	fpImg = fopen( fullName, "rb");
	if( fpImg==0 )
	{
		AfxMessageBox( "Cannot open the list file", MB_OK, 0 );
		return;
	}

	CString sFTitle;
	sFTitle.Format( "%s", fnames findex*100 );

	CHumanDetDoc* pDoc;
	pDoc = GetDocument();
	pDoc->SetTitle( sFTitle );

	fread( &width, sizeof(int), 1, fpImg);
	fread( &height, sizeof(int), 1, fpImg);

	if( image )
		delete image;

	image = new BYTE[width*height];
	fread( image, sizeof(BYTE), width*height, fpImg);
	fclose(fpImg);


	if( Img1 )
		delete Img1;
	Img1 = new BYTE[width*height];

	// cut region 1
	BYTE *rImg, *pImg;
	Rw = SReg1.x2 - SReg1.x1;
	Rh = SReg1.y2 - SReg1.y1;

	rImg = new BYTE[Rw*Rh];
	pImg = new BYTE[Rw*Rh];


	int i, j;
	for( i=SReg1.y1; i<SReg1.y2; i  )
		for( j=SReg1.x1; j<SReg1.x2; j  )
			rImg[(i-SReg1.y1)*Rw j-SReg1.x1] = image[i*width j];
		
		//binarize and dilation 
		myPro.Process1( rImg, Rw, Rh, pImg);
		
		//detection
		humanCircle = myDet.detect1( pImg, Rw, Rh );
		//for display
		memcpy( Img1, image, sizeof(BYTE)*width*height);
		if(humanCircle.degree>3)
		{	
			m_nRegFlag=1;
			m_nPersonNum  ;
			// for display 
			for( i=SReg1.y1; i<SReg1.y2; i  )
				for( j=SReg1.x1; j<SReg1.x2; j  )
					Img1[i*width j] = pImg[(i-SReg1.y1)*Rw j-SReg1.x1];
				
				delete rImg;
				delete pImg;
		}
		
		/////////////////detection in Region 2///////////////////////
		else
		{
			
			if(rImg)
				delete rImg;
			if(pImg)
				delete pImg;
			Rw = SReg2.x2 - SReg2.x1;
			Rh = SReg2.y2 - SReg2.y1;
			rImg = new BYTE[Rw*Rh];
			pImg = new BYTE[Rw*Rh];
			
			for( i=SReg2.y1; i<SReg2.y2; i  )
				for( j=SReg2.x1; j<SReg2.x2; j  )
					rImg[(i-SReg2.y1)*Rw j-SReg2.x1] = image[i*width j];
				
				BYTE *temp;
				temp=new BYTE[Rw*Rh];
				smooth(rImg,Rw,Rh,temp);
				myPro.Process2( rImg, Rw, Rh, pImg);
				humanCircle = myDet.detect2( pImg, Rw, Rh ); 
			
					
					if(humanCircle.degree>2)
					{	
						m_nRegFlag=2;
						m_nPersonNum  ;
						for( i=SReg2.y1; i<SReg2.y2; i  )
							for( j=SReg2.x1; j<SReg2.x2; j  )
						Img1[i*width j] = pImg[(i-SReg2.y1)*Rw j-SReg2.x1];
						
						delete pImg;
						delete temp;
						delete rImg;
					}
					else
					{
						m_nRegFlag=3;
						if(rImg)
							delete rImg;
						if(pImg)
							delete pImg;
						Rw = SReg3.x2 - SReg3.x1;
						Rh = SReg3.y2 - SReg3.y1;
						rImg = new BYTE[Rw*Rh];
						pImg = new BYTE[Rw*Rh];
						
						for( i=SReg3.y1; i<SReg3.y2; i  )
							for( j=SReg3.x1; j<SReg3.x2; j  )
								rImg[(i-SReg3.y1)*Rw j-SReg3.x1] = image[i*width j];
							
							BYTE *temp;
							temp=new BYTE[Rw*Rh];
							smooth(rImg,Rw,Rh,temp);
							myPro.Process3( temp, Rw, Rh, pImg);
							humanCircle = myDet.detect3( pImg, Rw, Rh ); 
							for( i=SReg3.y1; i<SReg3.y2; i  )
								for( j=SReg3.x1; j<SReg3.x2; j  )
									Img1[i*width j] = pImg[(i-SReg3.y1)*Rw j-SReg3.x1];
								
								if(humanCircle.degree>2)
								{	
									m_nPersonNum  ;
									delete pImg;
									delete temp;
									delete rImg;
								}
								
								
					}
		}
////////////////////////////////////////////////////////////////


    OnInitialUpdate();
	CRect ClientRect;
	GetClientRect( &ClientRect );
	InvalidateRect(  &ClientRect );	
	
}


void CHumanDetView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	if( nChar == VK_NEXT)
	{
		if( findex < fnum )
		{
			findex  ;
			readImg( findex );
		}
	}
	if( nChar == VK_PRIOR )
	{
		if( findex > 0 )
		{
			findex--;
			readImg( findex );
		}
	}	
	
	CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CHumanDetView::tranByte( int*temp, BYTE* array, int w, int h)
{
	int dim;
	dim = w*h;
	
	double stm,stvar;
	stm = 120;
	stvar = 60;
	int i;
	
	double mean = 0;
	for( i=10000; i<dim; i   )
		mean  = temp[i];
	mean /= dim; // mean value
	
	double varia = 0;
	double diff;
	for( i=0; i<dim; i   )
	{
		diff = temp[i]-mean;
		varia  = diff*diff;
	}
	varia /= dim; // varianve
	
	if( varia<0.0001 )
		return;
	
	double ratio;
	ratio = stvar/sqrt( varia );
	
	BYTE value;
	for( i=0; i<dim; i   )
	{
		value = (BYTE)( (temp[i]-mean)*ratio stm );
		
		if( value >= 200 )
			array[i] = 200;
		else if( value < 80 )
			array[i] = 80;
		else
			array[i] = value;
		
	}
	/*int dim;
	dim = w*h;

	double stm,stvar;
	stm = 120;
	stvar = 60;
	int i;

	double mean = 0;
	for( i=0; i<dim; i   )
			mean  = temp[i];
	mean /= dim; // mean value
		
	double varia = 0;
	double diff;
	for( i=0; i<dim; i   )
	{
			diff = temp[i]-mean;
			varia  = diff*diff;
	}
	varia /= dim; // varianve

	if( varia<0.0001 )
		return;

	double ratio;
	ratio = stvar/sqrt( varia );

	BYTE value;
	for( i=0; i<dim; i   )
	{
		value = (BYTE)( (temp[i]-mean)*ratio stm );

		if( value >= 250 )
			array[i] = 250;
		else if( value < 80 )
			array[i] = 80;
		else
			array[i] = value;
		
	}*/
}

void CHumanDetView::smooth(BYTE*window, int wid, int hei, BYTE*temp)
{
	int i,j,m,n;
	int value;
	BYTE block[9];
	int Gaus[9];
	Gaus[0]=1;
	Gaus[1]=1;
	Gaus[2]=1;
	Gaus[3]=1;
	Gaus[4]=1;
	Gaus[5]=1;
	Gaus[6]=1;
	Gaus[7]=1;
	Gaus[8]=1;
	
	for(i=0;i<hei;i  )
		for(j=0;j<wid;j  )
		{
			if(i==0||j==0||i==hei-1||j==wid-1)
				temp[i*wid j]=0;
			else
			{	
				
				for(m=-1;m<2;m  )
					for(n=-1;n<2;n  )
						block[(m 1)*3 n 1]=window[(i m)*wid n j];
					value=convolution(Gaus,block);
					value=(int)(value/9.);
					temp[i*wid j]=(BYTE)value;
			}
		}
}

void CHumanDetView::sobol( BYTE*window, int wid, int hei, 
						   int *sob_x,int *sob_y)
{
	int so_x[9],so_y[9];
	so_x[0]=-1;
	so_x[1]=0;
	so_x[2]=1;
	so_x[3]=-2;
	so_x[4]=0;
	so_x[5]=2;
	so_x[6]=-1;
	so_x[7]=0;
	so_x[8]=1;
	
	so_y[0]=-1;
	so_y[1]=-2;
	so_y[2]=-1;
	so_y[3]=0;
	so_y[4]=0;
	so_y[5]=0;
	so_y[6]=1;
	so_y[7]=2;
	so_y[8]=1;	

	BYTE block[9];
	int i,j,m,n;
	int value;
	for(i=0;i<hei;i  )
		for(j=0;j<wid;j  )
		{
			if(i>hei-3||j>wid-3)
			{	sob_x[i*hei j]=0;
				sob_y[i*hei j]=0;
			}
			else
			{
				for(m=0;m<3;m  )
					for(n=0;n<3;n  )
						block[m*3 n]=window[(i m)*wid j n];
				
				value=convolution(so_x,	block);
				sob_x[i*hei j]=value;
				value=convolution(so_y,	block);
				sob_y[i*hei j]=value;

			}
		}
	
}

int CHumanDetView::convolution( int *sobel, BYTE*block)
{
	int value=0;
	int m,n;
	for(m=0;m<3;m  )
		for(n=0;n<3;n  )
			value =sobel[m*3 n]*block[m*3 n];
	return value;
}

标签:

实例下载地址

HumanDet(公交车系统模式识别)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警