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

📄 image.cpp

📁 opencv实现的人体运动跟踪源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	if(FileName=="txt")
	{
		CString temp = pFile->GetFilePath();
		pFile -> Close();
		BOOL b = SaveAsText(temp, FALSE);
		pFile -> Open(temp, CFile::modeWrite);
		return b;
	}
	
	return WriteAsPGM(pFile);
}

BOOL CImage::WriteToFile(CString filename)
{
	CFile* pFile = new CFile;
	if(!pFile->Open(filename,CFile::modeWrite|CFile::modeCreate))
	{
		AfxMessageBox("In CImage::WriteToFile(),Can not open the file!");
		return false;
	}

	BOOL b = WriteToFile(pFile);
	
	pFile->Close();
	delete pFile;
	return b;	
}

void CImage::ImageClear()
{
    if (m_pDib) 
    GlobalFreePtr(m_pDib);  // free the DIB memory
	m_pDib     = NULL;
	m_pData    = NULL;
	m_bIsDIB   = TRUE;
	m_dwLength = 0L; 
}

DWORD CImage::Transform(WORD width)
{
    DWORD dwBytes = (width * 8) / 32;
    if ((width * 8) % 32) {
        dwBytes ++;
    }
    dwBytes *= 4; //bytes per line	
	return dwBytes;
}

BOOL CImage::ReadFromIMG(CFile* pFile)
{
	int		bpp;
	LPBITMAPINFOHEADER pBMIH;
    DWORD dwFileLength = pFile->GetLength();
    WORD w,h;

	pFile->Read((WORD* )&w,sizeof(WORD));
	pFile->Read((WORD* )&h,sizeof(WORD));

	dwFileLength -= 2 * sizeof(WORD);
	bpp = int(dwFileLength / (w * h));
	if(bpp != 1 && bpp != 3)/* 8 or 24 */
	{
		AfxMessageBox("Wrong Data!");
		return false;
	}

    DWORD dwBytes = Transform(w);//bytes per line

	m_dwLength = DWORD(dwBytes * h * bpp) + sizeof(BITMAPINFOHEADER)
				              + sizeof(RGBQUAD) * 256;
    if (!AllocateMemory()) {
		AfxMessageBox("Allocmemory Error");
        return FALSE;}

    pBMIH=BMInfoHdPtr();
	pBMIH->biSize = sizeof(BITMAPINFOHEADER);
    pBMIH->biWidth = w;
    pBMIH->biHeight = h;
    pBMIH->biPlanes = 1;
    pBMIH->biBitCount = 8 * bpp;
    pBMIH->biCompression = BI_RGB;
    pBMIH->biSizeImage = 0;
    pBMIH->biXPelsPerMeter = 0;
    pBMIH->biYPelsPerMeter = 0;
    pBMIH->biClrUsed = 0;
    pBMIH->biClrImportant = 0;
    m_pData = (BYTE*) m_pDib + sizeof(BITMAPINFOHEADER) +
               sizeof(RGBQUAD) * NumColors();
	CreateGreyPalette();

    TRY {
		pFile->Read(m_pData, dwFileLength);
	}
	CATCH (CFileException, e) {
        AfxMessageBox("Read error");
        GlobalFreePtr(m_pDib);  // free the DIB memory
        return FALSE;
	}
	END_CATCH

	SetDIB(true);
	Dib();
    return TRUE;
}

BOOL CImage::ReadFromDAT(CFile* pFile)
{
	LPBITMAPINFOHEADER pBMIH;
    DWORD dwFileLength = pFile->GetLength();
    WORD w,h;
	///////just for MIT facebase!!!
	w = 256;
	h = 240;

	if(dwFileLength != (DWORD)(w * h))
	{
		AfxMessageBox("Wrong Data!");
		return false;
	}

    DWORD dwBytes = Transform(w);//bytes per line

	m_dwLength = DWORD(dwBytes * h) + sizeof(BITMAPINFOHEADER)
				              + sizeof(RGBQUAD) * 256;
    if (!AllocateMemory()) {
		AfxMessageBox("Allocmemory Error");
        return FALSE;}

    pBMIH=BMInfoHdPtr();
	pBMIH->biSize = sizeof(BITMAPINFOHEADER);
    pBMIH->biWidth = w;
    pBMIH->biHeight = h;
    pBMIH->biPlanes = 1;
    pBMIH->biBitCount = 8;
    pBMIH->biCompression = BI_RGB;
    pBMIH->biSizeImage = 0;
    pBMIH->biXPelsPerMeter = 0;
    pBMIH->biYPelsPerMeter = 0;
    pBMIH->biClrUsed = 0;
    pBMIH->biClrImportant = 0;
    m_pData = (BYTE*) m_pDib + sizeof(BITMAPINFOHEADER) +
               sizeof(RGBQUAD) * NumColors();
	CreateGreyPalette();

    TRY {
		pFile->Read(m_pData, dwFileLength);
	}
	CATCH (CFileException, e) {
        AfxMessageBox("Read error");
        GlobalFreePtr(m_pDib);  // free the DIB memory
        return FALSE;
	}
	END_CATCH

	SetDIB(false);
	Dib();
    return TRUE;
}
/////////////////the property of the image////////////////
/////////Mean grey value
BYTE	CImage::MeanGrey()
{
	BYTE	avegrey;
	double sum = 0.0;
	DWORD i, imgsize;
	imgsize = (DWORD) Width() * Height();
	BYTE*	pData = Data();
	for(i=0;i<imgsize;i++)
		sum += pData[i];
	avegrey = (BYTE)(sum / imgsize);
	Dib();
	return avegrey;
}

double	CImage::Contrast()
{
	double	contrast;
	BYTE	low, high, *pData;
	double	scale;
	DWORD	i, imgSize, scaleNum, lc, rc;
	WORD	w, h;
	WORD	GreyDotNum[256];

	w		=	Width();
	h		=	Height();
	imgSize	=	(DWORD) (w * h);
	scale	=	0.1;
	scaleNum=	(DWORD)(scale * imgSize);
	for(i=0;i<256;i++)
		GreyDotNum[i] = 0;
	pData	=	Data();
	for(i=0;i<imgSize;i++)
		GreyDotNum[pData[i]]++;
	for(i=0,lc=0;i<256;i++)
	{
		lc += GreyDotNum[i];
		if(lc >= scaleNum)
		{
			low = (BYTE)i;
			break;
		}
	}
	for(i=255,rc=0; i>0; i--)
	{
		rc += GreyDotNum[i];
		if(rc >= scaleNum)
		{
			high = (BYTE)i;
			break;
		}
	}
	contrast = (double)(high - low);

	Dib();
	return contrast;
}

double	CImage::CleanMeasure()
{
	double	diff	=	0.0;
	DWORD	i;
	BYTE	mean	=	MeanGrey();
	BYTE*	pData	=	Data();
	DWORD	imgSize	=	Width() * Height();
	for(i=0,diff=0.0; i<imgSize; i++)
		diff += (pData[i] - mean) * (pData[i] - mean);
	diff = sqrt(diff) / imgSize;
	Dib();
	return diff;
}

double	CImage::NoiseMeasure()
{
	double	noise, dif;
	BYTE	*t;
	BYTE	mean	=	MeanGrey();
	BYTE*	pData	=	Data();
	WORD	i, j, w, h;
	long	imgSize, count;
	w = Width();
	h = Height();
	imgSize = (DWORD)(w * h);
	noise = 0.0;
	for(i=1, t = pData + w, count = 0; i < h - 1; i++, t += w) {
		for(j= 1; j < w - 1; j++)
		{
			dif = fabs(((int)*(t + j)) * 2 - (*(t + j -1)) - (*(t + j +1)));
			dif += fabs(((int)*(t + j)) * 2 - (*(t + j - w)) - (*(t + j +w)));
			dif += fabs(((int)*(t + j)) * 2 - (*(t + j - w - 1)) - (*(t + j + w + 1)));
			dif += fabs(((int)*(t + j)) * 2 - (*(t + j - w + 1)) - (*(t + j + w - 1)));
			if (dif / 4 < 30) {
				count++;
				noise += dif / 4;
			}
		}
	}
	noise /= count;

	Dib();
	return noise;
}

BYTE	CImage::Get8NeiborAver(BYTE* pGreyData, WORD w, WORD h, CPoint pos)
{
	double temp;
	int x = pos.x;
	int y = pos.y;
	int t = y * w + x;
	if(x == 0 && y != 0)
	{
		temp  =	pGreyData[t-w] + pGreyData[t-w+1] +
				pGreyData[t+1] + 
				pGreyData[t+w] + pGreyData[t+w+1];
		temp /= 5;
		return (BYTE)temp;
	}
	if(x != 0 && y == 0)
	{
		temp  =	pGreyData[t-1] + pGreyData[t+1] + 
				pGreyData[t+w-1] + pGreyData[t+w] + pGreyData[t+w+1];
		temp /= 5;
		return (BYTE)temp;
	}
	if(x == 0 && y != 0)
	{
		temp  =	pGreyData[t+1] +
				pGreyData[t+w] + pGreyData[t+w+1];
		temp /= 3;
		return (BYTE)temp;
	}

	if(x == w-1 && y != h-1)
	{
		temp  =	pGreyData[t-w-1] + pGreyData[t-w] +
				pGreyData[t-1] + 
				pGreyData[t+w-1] + pGreyData[t+w];
		temp /= 5;
		return (BYTE)temp;
	}
	if(x != w-1 && y == h-1)
	{
	temp  =	pGreyData[t-w-1] + pGreyData[t-w] + pGreyData[t-w+1] +
			pGreyData[t-1] + pGreyData[t+1];
	temp /= 5;
	return (BYTE)temp;
	}
	if(x = w-1 && y == h-1)
	{
		temp  =	pGreyData[t-w-1] + pGreyData[t-w] + 
				pGreyData[t-1];
		temp /= 3;
		return (BYTE)temp;
	}
	temp  =	pGreyData[t-w-1] + pGreyData[t-w] + pGreyData[t-w+1] +
			pGreyData[t-1]   + pGreyData[t+1] +
			pGreyData[t+w-1] + pGreyData[t+w] + pGreyData[t+w+1];
	temp /= 8;
	return (BYTE)temp;
}

double	CImage::Entropy()
{
	int  i, h, w;
	long Num[256], counter, pixel_sum;
	unsigned char  *img;
	double	p[256];
	double  Entropy;

	if(Bits() != 8) return false;
	img = Data();
	w = Width();
	h = Height();
	for(i=0;i<256;i++)	    
		Num[i] = 0;
	pixel_sum = (long)h*w;
	//calculate the Histogram
	long sum = 0;
	for (counter=0;counter<pixel_sum;counter++)
		Num[img[counter]]++;
	for(Entropy = 0, i=0;i<256;i++) {
		p[i] = (double)Num[i] / (double)pixel_sum;
		if(p[i] != 0)
			Entropy += p[i] * log(p[i]);
	}
	return -Entropy;
}

double	CImage::ImageQuality()
{
	double	imgQulity;

	BYTE	meangrey = MeanGrey();
	double	contrast = Contrast();
	double	noise	 = NoiseMeasure();
	double	entropy  = Entropy();
	TRACE("Meangrey=%d\n", meangrey);	
	TRACE("contrast=%f\n", contrast);	
	TRACE("noise=%f\n", noise);	
	TRACE("entropy=%f\n", entropy);	
	//imgQulity = (contrast/200.0) * (1.0/fabs(meangrey-128.0)) * (entropy/8.0) * (1.0/noise);

	//return imgQulity;

	double temp = log((entropy * entropy) / ( noise));
	if(temp > 0.0)
		//imgQulity = 20.0 * temp;
		imgQulity = temp;
	else
		imgQulity = 10.0;

	//if(meangrey < 64 || meangrey > 200 || contrast < 64)
		//imgQulity *= 0.5;
	imgQulity *= 20 * exp(-(meangrey-160.0)*(meangrey-160.0)/5832.0);
	imgQulity *= exp(-(contrast-180.0)*(contrast-180.0)/5832.0);
	return imgQulity;
}

DWORD CImage::ByteNumForOneLine(WORD nDIBWidth, int nBpp)
{
    DWORD dwBytes = (nDIBWidth * nBpp) / 32;
	if ((nDIBWidth * nBpp) % 32) 
		dwBytes ++;

    dwBytes *= 4;//bytes per line
	
	return	dwBytes;
}
///////////////////////////////////////////////
/*
 * SSG, 1999.6. 2
 * Save the data as text
*/
///////////////////////////////////////////////
BOOL CImage::SaveAsText(CString	 filename, BOOL	bIsBinary)
{
	int		w;
	long	l, size;
	char	buf[20];
	CFile	f;
	BYTE	*pData;

	pData = Data();
	
	if (m_pDib == NULL)
		return FALSE;
	if(Bits() != 8) {
		AfxMessageBox("It is not 8-greylevel image!");		
		return false;	
	}
	w = Width();
	size = w * Height();

	f.Open(filename, CFile::modeCreate | CFile::modeWrite );
	if(bIsBinary)
	{
		for(l=0; l<size; l++)
		{
			if(*(pData + l) != 0)
				sprintf(buf, "*");
			else
				sprintf(buf, ".");
			f.Write(buf, strlen(buf));
			if( (l+1)%w == 0){
				f.Write("\n", 1);
			}
		}
	}
	else
	{
		for(l=0; l<size; l++)
		{
			sprintf(buf, "%3d ", *(pData + l) );
			f.Write(buf, strlen(buf));
			if( (l+1)%w == 0){
				f.Write("\n", 1);
			}
		}
	}
	f.Close();
    
	Dib();
    return TRUE;
}

BOOL CImage::ValidImage()
{
	if(m_dwLength == 0L || m_pData == NULL || m_pDib == NULL)
		return FALSE;
	else
		return TRUE;
}

⌨️ 快捷键说明

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