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

📄 image.cpp

📁 图像标注程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    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");
        delete []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;
	//w = 128;
	//h = 120;
	//for bern faces
	w = 512;
	h = 342;
	
	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();
	
	//for bern faces
	pFile->Seek(32L, CFile::begin);
	
    TRY {
		pFile->Read(m_pData, (long(w) * h));
	}
	CATCH (CFileException, e) {
        AfxMessageBox("Read error");
        delete []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;
}

/********************************************************************
///////////////////////////from the CImage///////////////////
********************************************************************/


void CImage::Sobel()
{
	int conv1[3][3],conv2[3][3];
	int i,j,value,v1,v2,v3;
	long j0,j1,j2,target_line_offset,source_line1_offset,source_line2_offset;
	unsigned char  *img, *sobbed;
	int high, width;
	
	width = Width();
	high = Height();
	CSize size=CSize(width,high);
	CImage* sobel=new CImage(size);
	
	img = Data();
	sobbed = sobel->Data();
	
	memset(sobbed, '\0', (long)high * width);
	
	/* inter pixel */
	conv1[0][0]=1; conv1[0][1]=2; conv1[0][2]=1;
	conv1[1][0]=0; conv1[1][1]=0; conv1[1][2]=0;
	conv1[2][0]=-1; conv1[2][1]=-2; conv1[2][2]=-1;
	
	conv2[0][0]=1; conv2[0][1]=0; conv2[0][2]=-1;
	conv2[1][0]=2; conv2[1][1]=0; conv2[1][2]=-2;
	conv2[2][0]=1; conv2[2][1]=0; conv2[2][2]=-1;
	
	target_line_offset=(long)-width;
	source_line1_offset=0;
	source_line2_offset=(long)width;
	for (i=1;i<high-1;i++) {
		target_line_offset=source_line1_offset;
		source_line1_offset=source_line2_offset;
		source_line2_offset+=(long)width;
		j1=0;	  j2=1;
		for (j=1;j<width-1;j++) {
			j0=j1;	j1=j2;	j2++;
			value=0;
			v1=(int)*(img+target_line_offset+j0)*conv1[0][0];
			v1+=(int)*(img+target_line_offset+j1)*conv1[0][1];
			v1+=(int)*(img+target_line_offset+j2)*conv1[0][2];
			v2=(int)*(img+source_line1_offset+j0)*conv1[1][0];
			v2+=(int)*(img+source_line1_offset+j1)*conv1[1][1];
			v2+=(int)*(img+source_line1_offset+j2)*conv1[1][2];
			v3=(int)*(img+source_line2_offset+j0)*conv1[2][0];
			v3+=(int)*(img+source_line2_offset+j1)*conv1[2][1];
			v3+=(int)*(img+source_line2_offset+j2)*conv1[2][2];
			value=abs(v1+v2+v3);
			v1=(int)*(img+target_line_offset+j0)*conv2[0][0];
			v1+=(int)*(img+target_line_offset+j1)*conv2[0][1];
			v1+=(int)*(img+target_line_offset+j2)*conv2[0][2];
			v2=(int)*(img+source_line1_offset+j0)*conv2[1][0];
			v2+=(int)*(img+source_line1_offset+j1)*conv2[1][1];
			v2+=(int)*(img+source_line1_offset+j2)*conv2[1][2];
			v3=(int)*(img+source_line2_offset+j0)*conv2[2][0];
			v3+=(int)*(img+source_line2_offset+j1)*conv2[2][1];
			v3+=(int)*(img+source_line2_offset+j2)*conv2[2][2];
			v3=abs(v1+v2+v3);
			
			//?			value=sqrt(value*value+v3*v3);
			value=(value>v3)?value:v3;
			if (value>255) value=255;
			*(sobbed+target_line_offset+j0)=value;
		}
	}
	memcpy(img, sobbed, (long)high * width);
	Dib();
	delete sobel;
}
int  CImage::DajinThre()
{
	long	largest = 0L;
	int		i, grey, r_v, h, w;
	long	p[GREY], pixel_sum1, pixel_sum2, grey_sum1, grey_sum2;
	double	cigma, dif_cigma, mean1, mean2;
	BYTE	*img;
	
	img = Data();
	w	= Width();
	h	= Height();
	
	for(i = 0; i < GREY; i++)	    
		p[i]=0;
	pixel_sum2 = long(h) * w;
	grey_sum1  = grey_sum2  = 0;
	for(pixel_sum1=0; pixel_sum1<pixel_sum2; pixel_sum1++)
	{
		grey = *(img+pixel_sum1) + 1;
		grey_sum2 += grey;
		p[grey]++;
	}
	
	pixel_sum1=0;
	dif_cigma=double(0);
	for (i=0;i<254;i++)		
	{
		pixel_sum1+=p[i];     pixel_sum2-=p[i];
		grey_sum1+=(p[i]*(i+1));  grey_sum2-=(p[i]*(i+1));
		if (pixel_sum2!=0)	{
			if (pixel_sum1!=0)	{
				mean1=double(grey_sum1/pixel_sum1);
				mean2=double(grey_sum2/pixel_sum2);
				cigma=(double)pixel_sum1*(double)pixel_sum2*fabs(mean1-mean2);
				if (cigma<0)     return(256);
				if (cigma>dif_cigma)	{
					dif_cigma=cigma; r_v=i;
				}
			}
		}
		else      break;
	}
	
	/*	for(i = 0; i < GREY; i++)
	if(p[i] > largest)

⌨️ 快捷键说明

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