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

📄 grimsonbg.cpp

📁 人体运动跟踪 混合高斯模型+GRISON方法
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	PlotBestMixture(median) ;
	//data1是原图
     CalculateUpdate(src) ;
     AddNewImage(src) ;
	 //data2是前景图
     CalculateForeground(src,result);

}

void GrimsonBG::SaveScreen(CDC *pDC, int width, int height)
{
	CDC memDC;
	CBitmap bitmap;
	memDC.CreateCompatibleDC(pDC);
	bitmap.CreateCompatibleBitmap(pDC, width, height);
	memDC.SelectObject(&bitmap);

	memDC.BitBlt(0, 0, width, height, pDC, 0, 0, SRCCOPY);  // win2000: 64

    CPalette tmpPal;
    HANDLE hDIB = DDBToDIB(bitmap, BI_RGB, &tmpPal);
    
    TCHAR szFileName[255];
	sprintf(szFileName, "%d.bmp", m_iNum++);
	
	WriteDIB(szFileName, hDIB);
    GlobalFree( hDIB );

}

BOOL GrimsonBG::WriteDIB(CString szFile, HANDLE hDIB)
{
		BITMAPFILEHEADER hdr;
	LPBITMAPINFOHEADER m_lpBitmapInforHead;

	if (!hDIB)
	return FALSE;

	CFile file;
	if( !file.Open(szFile, CFile::modeWrite|CFile::modeCreate) )
	return FALSE;

	m_lpBitmapInforHead = (LPBITMAPINFOHEADER)hDIB;

	int nColors;

	if (m_lpBitmapInforHead->biBitCount >= 16)
		nColors = 0;
	else
		nColors = (1 << m_lpBitmapInforHead->biBitCount);

	// Fill in the fields of the file header 
	hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
	hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
	hdr.bfReserved1 = 0;
	hdr.bfReserved2 = 0;
	hdr.bfOffBits = (DWORD) (sizeof( hdr ) + m_lpBitmapInforHead->biSize +
	nColors * sizeof(RGBQUAD));

	// Write the file header 
	file.Write( &hdr, sizeof(hdr) );

	// Write the DIB header and the bits 
	file.Write( m_lpBitmapInforHead, GlobalSize(hDIB) );

	return TRUE;

}

HANDLE GrimsonBG::DDBToDIB(CBitmap &bitmap, DWORD dwCompression, CPalette *pPal)
{
	
	BITMAP			bm;
	BITMAPINFOHEADER	bi;
	LPBITMAPINFOHEADER 	m_lpBitmapInforHead;
	DWORD			dwLen;
	HANDLE			hDIB;
	HANDLE			handle;
	HDC 			hDC;
	HPALETTE		hPal;


	ASSERT( bitmap.GetSafeHandle() );

	// The function has no arg for bitfields
	if( dwCompression == BI_BITFIELDS )
		return NULL;

	// If a palette has not been supplied use defaul palette
	hPal = (HPALETTE) pPal->GetSafeHandle();
	if (hPal==NULL)
		hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);

	// Get bitmap information
	bitmap.GetObject(sizeof(bm),(LPSTR)&bm);

	// Initialize the bitmapinfoheader
	bi.biSize		= sizeof(BITMAPINFOHEADER);
	bi.biWidth		= bm.bmWidth;
	bi.biHeight 		= bm.bmHeight;
	bi.biPlanes 		= 1;
	bi.biBitCount		= bm.bmPlanes * bm.bmBitsPixel;
	bi.biCompression	= dwCompression;
	bi.biSizeImage		= 0;
	bi.biXPelsPerMeter	= 0;
	bi.biYPelsPerMeter	= 0;
	bi.biClrUsed		= 0;
	bi.biClrImportant	= 0;

	// Compute the size of the  infoheader and the color table
	int nColors = (1 << bi.biBitCount);
	if( nColors > 256 ) 
		nColors = 0;
	dwLen  = bi.biSize + nColors * sizeof(RGBQUAD);

	// We need a device context to get the DIB from
	hDC = GetDC(NULL);
	hPal = SelectPalette(hDC,hPal,FALSE);
	RealizePalette(hDC);

	// Allocate enough memory to hold bitmapinfoheader and color table
	hDIB = GlobalAlloc(GMEM_FIXED,dwLen);

	if (!hDIB){
		SelectPalette(hDC,hPal,FALSE);
		ReleaseDC(NULL,hDC);
		return NULL;
	}

	m_lpBitmapInforHead = (LPBITMAPINFOHEADER)hDIB;

	*m_lpBitmapInforHead = bi;

	// Call GetDIBits with a NULL lpBits param, so the device driver 
	// will calculate the biSizeImage field 
	GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
			(LPBYTE)NULL, (LPBITMAPINFO)m_lpBitmapInforHead, (DWORD)DIB_RGB_COLORS);

	bi = *m_lpBitmapInforHead;

	// If the driver did not fill in the biSizeImage field, then compute it
	// Each scan line of the image is aligned on a DWORD (32bit) boundary
	if (bi.biSizeImage == 0){
		bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8) 
						* bi.biHeight;

		// If a compression scheme is used the result may infact be larger
		// Increase the size to account for this.
		if (dwCompression != BI_RGB)
			bi.biSizeImage = (bi.biSizeImage * 3) / 2;
	}

	// Realloc the buffer so that it can hold all the bits
	dwLen += bi.biSizeImage;
	if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
		hDIB = handle;
	else{
		GlobalFree(hDIB);

		// Reselect the original palette
		SelectPalette(hDC,hPal,FALSE);
		ReleaseDC(NULL,hDC);
		return NULL;
	}

	// Get the bitmap bits
	m_lpBitmapInforHead = (LPBITMAPINFOHEADER)hDIB;

	// FINALLY get the DIB
	BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
				0L,				// Start scan line
				(DWORD)bi.biHeight,		// # of scan lines
				(LPBYTE)m_lpBitmapInforHead 			// address for bitmap bits
				+ (bi.biSize + nColors * sizeof(RGBQUAD)),
				(LPBITMAPINFO)m_lpBitmapInforHead,		// address of bitmapinfo
				(DWORD)DIB_RGB_COLORS);		// Use RGB for color table

	if( !bGotBits )
	{
		GlobalFree(hDIB);
		
		SelectPalette(hDC,hPal,FALSE);
		ReleaseDC(NULL,hDC);
		return NULL;
	}

	SelectPalette(hDC,hPal,FALSE);
	ReleaseDC(NULL,hDC);
	return hDIB;

}

void GrimsonBG::ComputeCentroid(BYTE *labeled, short *region_info, CRect *position, short *ResultCentroid)
{
	memset(ResultCentroid, 0, sizeof(m_sCentroid));
	int x=0, y=0, current, i,j ;
	unsigned numx, numy, totalx, totaly;
	unsigned indexx[352], indexy[288];

	

	for (int loop=0; loop<255; loop++)
	{
		if (region_info[2*loop+1] >=m_iMinObjectSize) 
		{
			numx=0;
			numy=0;
			totalx=0;
			totaly=0;
			current=region_info[2*loop+0];
			for (i=0; i<352; i++) indexx[i]=0;
			for (j=0; j<288; j++) indexy[j]=0;

			// y - axis

			for (j=position[loop].top; j<=position[loop].bottom; j++)
				for (i=position[loop].left; i<=position[loop].right; i++)
				{
					if (labeled[i+j*m_biWidth] == current)
					{
						indexy[j]++;
						numy++;
					}
				}
			for (j=position[loop].top; j<=position[loop].bottom; j++)
							totaly=totaly+indexy[j]*j;
			ResultCentroid[loop*2+1]= (int)(totaly/numy+0.5);

			// x - axis

			for (i=position[loop].left; i<=position[loop].right; i++)
				for (j=position[loop].top; j<=position[loop].bottom; j++)
				{
					if (labeled[i+j*m_biWidth] == current)
					{
						indexx[i]++;
						numx++;
					}
				}
			for (i=position[loop].left; i<=position[loop].right; i++)
							totalx=totalx+indexx[i]*i;
			ResultCentroid[loop*2+0]= (int)(totalx/numx+0.5);
//			CDebug::dprintf( " %d th segment- CenterX: %d, CenterY: %d \n", loop, ResultCentroid[loop*2], ResultCentroid[loop*2+1]);
		}
		else loop=256;
	}

}

void GrimsonBG::ComputeProjection(BYTE *Morp, short *region_info, CRect *position, int whichone, short *m_sXProjection, short *m_sYProjection)
{

	int x=0, y=0, current, i,j ;
//	unsigned numx, numy, totalx, totaly;
	
	
	if (region_info[2*whichone+1] >=m_iMinObjectSize) 
		{
	//		numx=0;
	//		numy=0;
	//		totalx=0;
	//		totaly=0;
			current=region_info[2*whichone+0];
			for (i=0; i<352; i++) m_sXProjection[i]=0;
			for (j=0; j<288; j++) m_sYProjection[j]=0;

			// y - axis

			for (j=position[whichone].top; j<=position[whichone].bottom; j++)
				for (i=position[whichone].left; i<=position[whichone].right; i++)
				{
					if (Morp[i+j*m_biWidth] == current)
					{
						m_sYProjection[j]++;
				//		numy++;
					}
				}


			// x - axis

			for (i=position[whichone].left; i<=position[whichone].right; i++)
				for (j=position[whichone].top; j<=position[whichone].bottom; j++)
				{
					if (Morp[i+j*m_biWidth] == current)
					{
						m_sXProjection[i]++;
				//		numx++;
					}
				}
		}
}

void	GrimsonBG::FindBestRectangle(BYTE *labeled,short *region_info, CRect *position, short *projection,int whichone, int *outputnumber,  CRect *result)
{
	
//	outputnumber=0;
	int tt=0;
	int temp, i, j,jj, xcentroid, pos=300;//, value;
	
	short	xsum[352], ysum[288];
	memset(xsum, 0, sizeof(xsum));
	memset(ysum, 0, sizeof(ysum));
	byte	checkover[352];
	memset(checkover, 0, sizeof(checkover));

		int previous_total=-2^16, total=0;
//	CRect best;
//	best.bottom=0, best.left=0, best.right=0, best.top=0;

	
	if (region_info[2*whichone+1] >=m_iMinObjectSize) 
	{
		temp=region_info[whichone*2+0];

		xcentroid = m_sCentroid[pos*2+0];

		int max=0, medium=0;
		for (i=position[whichone].left; i<=position[whichone].right; i++)
			if (projection[i]>=max) max=projection[i];
		//	medium=int(max/2+0.5);
			medium = int (max * 0.4 +0.5);

		for (i=position[whichone].left; i<=position[whichone].right; i++)
			if (projection[i]>medium)
			{
				checkover[i]=1;
			}

		// Labelling
		int previous=0;
		for (i=position[whichone].left; i<=position[whichone].right; i++)
		{
			if (checkover[i]>0)
			{
				if (i>0)
				{
					if (checkover[i-1]>0)
						checkover[i]=checkover[i-1];
					else if(checkover[i-1]==0)
					{
						for (int jjj=position[whichone].left; jjj<=i-1; jjj++)
							if (checkover[jjj]>=previous) previous=checkover[jjj];
						checkover[i]=previous+1;
					}
				}
			}
		}
		tt=0;
		for (i=position[whichone].left; i<=position[whichone].right; i++)
			if ( checkover[i]>=tt) tt=checkover[i];

//			CDebug::dprintf("*******Number of %d\n", tt);



		// find a position for each segment
		for (i=1; i<=tt; i++)
		{
			result[i-1].left=m_biWidth;
			result[i-1].right=0;


		for (j=position[whichone].left; j<=position[whichone].right; j++)
			if (checkover[j]==i)
			{
				if (result[i-1].left>=j) result[i-1].left=j;
				if (result[i-1].right<=j) result[i-1].right=j;
			}
//			CDebug::dprintf(" **** %d **** left: %d, right: %d\n", i, result[i-1].left, result[i-1].right);
		}




		// compute top and bottom positions
		int temp_left, temp_right, width, height;
		for (int loop=0; loop<tt; loop++)
		{
				//previous_total=0;
			total=0;
			previous_total=-2^16;
			temp=region_info[whichone*2+0];
		
			if ( (result[loop].right - result[loop].left) >=5)
				{

					width=result[loop].right-result[loop].left;
					height=int((2.5)*width);
					if (height>max) height=max;
				//	for (jj=width/2; jj<m_biHeight-(width/2); jj++)
					for (jj=height/2; jj<m_biHeight-(height/2); jj++)
				//	for (jj=result[loop].top; jj<m_biHeight-(height); jj++)
					{
						total=0;
						for (i=result[loop].left; i<=result[loop].right; i++)
						//	for (j=-width/2; j<=width/2; j++)
							for (j=-height/2; j<=height/2; j++)
						//	for (j=0; j<=height; j++)
							{
								if (labeled[i+(jj+j)*m_biWidth]==temp)
								{
									if ( (jj -j) < (position[whichone].bottom-position[whichone].top)/2+position[whichone].top)	
										total=total+3;
									else total++;
								}
								else total--;
							}
						if (total>= previous_total) 
						{
							previous_total=total;
							result[loop].top=jj-height/2;
							result[loop].bottom=jj+height/2;
						}	

					}
				}
				else  // 5*5 block
				{
					width=5;
					height=width*2;
					if (height>max) height=max;
					for (jj=height/2; jj<m_biHeight-(height/2); jj++)
					{
						total=0;
						if (( result[loop].right+2>m_biWidth) || (result[loop].left-2 <0))
						{
							if (result[loop].right+2>m_biWidth)
							{
								temp_right=m_biWidth-1;
								temp_left=m_biWidth-6;
							}
							else 
							{
								temp_right=5;

⌨️ 快捷键说明

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