⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 上述是VIsualc++ 数字图像处理一书的源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	if(!theApp.curVideo)
	{
		AfxMessageBox("没有打开视频文件 !");
		return;
	}

	// 创建线程
	th = CreateThread(NULL,0,StaticDetectT,theApp.curVideo->doc,0,&pID);
}

/************************************************************************/
/* 动止背景目标检测                                                     */
/************************************************************************/
DWORD WINAPI MotionDetectT(LPVOID data){

	CVideoDemoDoc *doc = (CVideoDemoDoc*)theApp.curVideo->doc;

	

	int frameLength = theApp.curVideo->ms->GetFrameCount();
	MediaSource *avi = theApp.curVideo->ms;

	unsigned char *frame = avi->GetSingleFrame(0);
	int frameWidth = avi->GetFrameWidth();
	int frameHeight = avi->GetFrameHeight();

	// 色彩变换
	ColorTrans ct;

	int *threshold = (int*)data;
	
	MotionDetectDiag diag;
	//if(diag.DoModal()){
	//	threshold = diag.m_index;
	//}
	CStatsticDetect sd(frameWidth,frameHeight);
	//CStaticDetect sd(frameWidth,frameHeight);
	ChafenMul cm(frameWidth,frameHeight,*threshold);

	// 输出缓冲
	unsigned char * outBuffer = new unsigned char[frameHeight * frameWidth * 3];
	unsigned char * gray = new unsigned char[frameWidth * frameHeight * 3];
	for(int i = 0; i < frameLength - 1; i++)
	{
		// 获得一帧图像	
		frame = avi->GetSingleFrame(i);
		
		ct.RGB2Gray8(frame,gray,frameWidth,frameHeight);

		// 准备数据
		cm.PrepareData(gray,frameWidth,frameHeight);
		
	
		if(i % 4 == 0 && i != 0)
		{
			cm.process();
			
		
			sd.DeNoise(cm.frame1,frameWidth,frameHeight);
			ct.gray2RGB2(cm.frame1,outBuffer,frameWidth,frameHeight);
			doc->id->data = outBuffer;
		
			doc->RefreshView();
	
		}
	}

	delete[] outBuffer;
	return 0;

}

/************************************************************************/
/* 动背景目标检测                                                       */
/************************************************************************/
void CMainFrame::OnMotionDd() 
{	
	DWORD exitCode;
	
	if(GetExitCodeThread(th,&exitCode)){
	   TerminateThread(th,exitCode);
	}
	
	if(theApp.curVideo == NULL)
		AfxMessageBox("没有打开视频文件!");
	DWORD pID;
	//控制线程播放

	int frameLength = theApp.curVideo->ms->GetFrameCount();

	MediaSource *avi = theApp.curVideo->ms;

	
	int threshold = 100;
	MotionDetectDiag mdd;
	if(mdd.DoModal())
	{
		threshold = mdd.m_index;
	}
	
	int * thre = (int*)malloc(sizeof(int));
	*thre = threshold;
	// 创建线程
	th = CreateThread(NULL,0,MotionDetectT,thre,0,&pID);
	
}


/************************************************************************/
/* 动止背景目标检测                                                     */
/************************************************************************/
DWORD WINAPI MSS(LPVOID data){

	CVideoDemoDoc *doc = (CVideoDemoDoc*)theApp.curVideo->doc;//(CVideoDemoDoc*)data;

	int frameLength = theApp.curVideo->ms->GetFrameCount();
	MediaSource *avi = theApp.curVideo->ms;

	unsigned char *frame = avi->GetSingleFrame(0);
	int frameWidth = avi->GetFrameWidth();
	int frameHeight = avi->GetFrameHeight();
	
	MeanShiftSegger* mms = (MeanShiftSegger*)data;

	// 输出缓冲
	unsigned char * outBuffer = new unsigned char[frameHeight * frameWidth * 3];

	for(int i = 0; i < frameLength - 1; i++)
	{
		// 获得一帧图像	
		frame = avi->GetSingleFrame(i);
		
		mms->MeanShiftTrackProcess(frame,i);

		doc->id->data = frame;
		
		doc->RefreshView();
	}

	delete[] outBuffer;
	return 0;

}


/************************************************************************/
/* MeanShift跟踪                                                        */
/************************************************************************/
void CMainFrame::OnTracekMeanshift() 
{
	DWORD exitCode;
	
	if(GetExitCodeThread(th,&exitCode)){
	   TerminateThread(th,exitCode);
	}
	DWORD pID;
	if(theApp.curVideo == NULL){
		AfxMessageBox("没有打开视频!");
		return;
	}
	
	int x,y,width,height;
	POSDiag pd;
	if(pd.DoModal())
	{
		x= pd.m_x;
		y= pd.m_y;
		width = pd.m_width;
		height = pd.m_height;
	}
	else
		return;
	
	mss = new MeanShiftSegger();
	int frameLength = theApp.curVideo->ms->GetFrameCount();
	MediaSource *avi = theApp.curVideo->ms;

	unsigned char *frame = avi->GetSingleFrame(0);
	int frameWidth = avi->GetFrameWidth();
	int frameHeight = avi->GetFrameHeight();

	mss->InitMeanShiftTracker(frame,frameWidth,frameHeight,x,y,width,height);

	
	th = CreateThread(NULL,0,MSS,mss,0,&pID);

	

}


/************************************************************************/
/* 重心 法 跟踪                                                         */
/************************************************************************/
DWORD WINAPI GAV(LPVOID data){

	int* threshold = (int*)data;

	CVideoDemoDoc *doc = (CVideoDemoDoc*)theApp.curVideo->doc;

	int frameLength = theApp.curVideo->ms->GetFrameCount();
	MediaSource *avi = theApp.curVideo->ms;

	unsigned char *frame = avi->GetSingleFrame(0);
	int frameWidth = avi->GetFrameWidth();
	int frameHeight = avi->GetFrameHeight();

	// 输出缓冲
	unsigned char * outBuffer = new unsigned char[frameHeight * frameWidth * 3];

	unsigned char * grayBuffer =new unsigned char[frameWidth * frameHeight];

	ColorTrans ct;
	CGravityCenter* m_gravitycenter;
	TARGETPARA m_target;
	for(int i = 0; i < frameLength - 1; i++)
	{
		// 获得一帧图像	
		frame = avi->GetSingleFrame(i);
		
		ct.RGB2Gray8(frame,grayBuffer,frameWidth,frameHeight);

		if(i == 0)
		{
			m_gravitycenter = new CGravityCenter(frameWidth,frameHeight);
			m_gravitycenter->InitalObjectTracker(T_GRACENTER,*threshold);
			m_gravitycenter->GravityCenter(i, frame);

		}
		else 
		{
			m_gravitycenter->GravityCenter(i, frame);
		}
		
		m_target = m_gravitycenter->m_result;
		/*
		// 重心跟踪
		WININFO info = GravityTrack(grayBuffer,frameWidth,frameHeight,*threshold);
		if(!info.flag)
		{
			for(int j = max(info.pt.y - info.h / 2,0);j <= min(info.pt.y + info.h / 2,frameHeight); j ++)
				for(int i = max(info.pt.x - info.w / 2,0);i <= min(info.pt.x + info.w / 2,frameWidth); i ++)
				{
					frame[j * frameWidth * 3 + i * 3] = 255;
				}
		
		}*/
		
		doc->id->data = frame;
		
		doc->RefreshView();
	}

	delete[] grayBuffer;

	delete[] outBuffer;
	return 0;

}
/************************************************************************/
/* 重心法跟踪                                                           */
/************************************************************************/
void CMainFrame::OnTracekGav() 
{
	DWORD exitCode;
	
	if(GetExitCodeThread(th,&exitCode)){
	   TerminateThread(th,exitCode);
	}
	DWORD pID;
	if(theApp.curVideo == NULL){
		AfxMessageBox("没有打开视频!");
		return;
	}
	

	int threshold = 100;
	MotionDetectDiag mdd;
	if(mdd.DoModal())
	{
		threshold = mdd.m_index;
	}

	int * thre = (int*)malloc(sizeof(int));
	*thre = threshold;

	th = CreateThread(NULL,0,GAV,thre,0,&pID);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -