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

📄 dibapi.cpp

📁 影像融合与融合精度评价源码影像融合与融合精度评价源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	case 4:
	case 8:
		hdc=GetDC(NULL);
		pBuffer=new BYTE[ds.dsBmih.biWidth*3];
		ZeroMemory(&bmi,sizeof(bmi));
		bmi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
		bmi.bmiHeader.biWidth=ds.dsBmih.biWidth;
		bmi.bmiHeader.biHeight=ds.dsBmih.biHeight;
		bmi.bmiHeader.biPlanes=1;
		bmi.bmiHeader.biBitCount=24;
		bmi.bmiHeader.biCompression=BI_RGB;
		for(i=0;i<ds.dsBmih.biHeight;i++)
		{
			GetDIBits(hdc,(HBITMAP)hImage,i,1,pBuffer,&bmi,DIB_RGB_COLORS);
			pbBits=pBuffer;
			for(j=0;j<ds.dsBmih.biWidth;j++)
			{
				b=*pbBits++;
				g=*pbBits++;
				r=*pbBits++;
				AddColor(&pTree,r,g,b,nColorBits,0,&nLeafCount,pReducibleNodes);
				while(nLeafCount>nMaxColors)
					ReduceTree(nColorBits,&nLeafCount,pReducibleNodes);
			}
		}
		delete pBuffer;
		ReleaseDC(NULL,hdc);
		break;
	case 16:
		if(ds.dsBmih.biCompression==BI_BITFIELDS)
		{
			rmask=ds.dsBitfields[0];
			gmask=ds.dsBitfields[1];
			bmask=ds.dsBitfields[2];
		}
		else
		{
			rmask=0x7c00;
			gmask=0x03E0;
			bmask=0x001F;
		}
		rright=GetRightShiftCount(rmask);
		gright=GetRightShiftCount(gmask);
		bright=GetRightShiftCount(bmask);

		rleft=GetLeftShiftCount(rmask);
		gleft=GetLeftShiftCount(gmask);
		bleft=GetLeftShiftCount(bmask);

		pwBits=(WORD*)ds.dsBm.bmBits;
		for(i=0;i<ds.dsBmih.biHeight;i++)
		{
			for(j=0;j<ds.dsBmih.biWidth;j++)
			{
				wColor=*pwBits++;
				b=(BYTE)(((wColor&(WORD)bmask)>>bright)<<bleft);
				g=(BYTE)(((wColor&(WORD)gmask)>>gright)<<gleft);
				r=(BYTE)(((wColor&(WORD)rmask)>>rright)<<rleft);
				AddColor(&pTree,r,g,b,nColorBits,0,&nLeafCount,pReducibleNodes);
                while(nLeafCount>nMaxColors)
					ReduceTree(nColorBits,&nLeafCount,pReducibleNodes);
			}
				pwBits=(WORD*)(((BYTE*)pwBits)+npad);
		}
			break;
	case 24:
			pbBits=(BYTE*)ds.dsBm.bmBits;
			for(i=0;i<ds.dsBmih.biHeight;i++)
			{
				for(j=0;j<ds.dsBmih.biWidth;j++)
				{
					b=*pbBits++;
					g=*pbBits++;
					r=*pbBits++;
                    AddColor(&pTree,r,g,b,nColorBits,0,&nLeafCount,pReducibleNodes);
                    while(nLeafCount>nMaxColors)
					ReduceTree(nColorBits,&nLeafCount,pReducibleNodes);
				}
                    pbBits+=npad;
			}
			break;
	case 32:
		if(ds.dsBmih.biCompression==BI_BITFIELDS)
		{
            rmask=ds.dsBitfields[0];
			gmask=ds.dsBitfields[1];
			bmask=ds.dsBitfields[2];
		}
		else
		{
			rmask=0x00FF0000;
			gmask=0x0000FF00;
			bmask=0x000000FF;
		}
        rright=GetRightShiftCount(rmask);
		gright=GetRightShiftCount(gmask);
		bright=GetRightShiftCount(bmask);
		pdwBits=(DWORD*)ds.dsBm.bmBits;
		for(i=0;i<ds.dsBmih.biHeight;i++)
		{
			for(j=0;j<ds.dsBmih.biWidth;j++)
			{
               dwColor=*pdwBits++;
               b=(BYTE)((dwColor&bmask)>>bright);
			   g=(BYTE)((dwColor&gmask)>>gright);
			   r=(BYTE)((dwColor&rmask)>>rright);
               AddColor(&pTree,r,g,b,nColorBits,0,&nLeafCount,pReducibleNodes);
               while(nLeafCount>nMaxColors)
				  ReduceTree(nColorBits,&nLeafCount,pReducibleNodes);
			}
            pdwBits=(DWORD*)((BYTE*)pdwBits+npad);
		}
		break;
	default:
		return NULL;
	}
		if(nLeafCount>nMaxColors)
		{
			DeleteTree(&pTree);
			return NULL;
		}
		dwSize=sizeof(LOGPALETTE)+((nLeafCount-1)*sizeof(PALETTEENTRY));
		if((plp=(LOGPALETTE*)HeapAlloc(GetProcessHeap(),0,dwSize))==NULL)
		{
			DeleteTree(&pTree);
			return NULL;
		}
		plp->palVersion=0x300;
		plp->palNumEntries=(WORD)nLeafCount;
		nIndex=0;
		GetPaletteColors(pTree,plp->palPalEntry,&nIndex);
        hPalette=CreatePalette(plp);

		HeapFree(GetProcessHeap(),0,plp);
		DeleteTree(&pTree);
		return hPalette;
}
void  WINAPI  AddColor(NODE** ppNode,BYTE r,BYTE g,BYTE b,UINT nColorBits,
						   UINT nLevel,UINT* pLeafCount,NODE** pReducibleNodes)
{
	int nIndex,shift;
	static BYTE mask[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};

	if(*ppNode==NULL)
		*ppNode=CreateNode(nLevel,nColorBits,pLeafCount,pReducibleNodes);

	if((*ppNode)->bIsLeaf)
	{
		(*ppNode)->nPixelCount++;
		(*ppNode)->nRedSum+=r;
		(*ppNode)->nGreenSum+=g;
		(*ppNode)->nBlueSum+=b;
	}
	else
	{
		shift=7-nLevel;
		nIndex=(((r&mask[nLevel])>>shift)<<2)|(((g&mask[nLevel])>>shift)<<1)|((b&mask[nLevel])>>shift);
		AddColor(&((*ppNode)->pChild[nIndex]),r,g,b,nColorBits,nLevel+1,
			pLeafCount,pReducibleNodes);
	}
}
void WINAPI  ReduceTree(UINT nColorBits,UINT* pLeafCount,NODE** pReducibleNodes)
{
	int i;
	NODE* pNode;
	UINT  nRedSum,nGreenSum,nBlueSum,nChildren;
	for(i=nColorBits-1;(i>0)&&(pReducibleNodes[i]==NULL);i--);
	pNode=pReducibleNodes[i];
	pReducibleNodes[i]=pNode->pNext;

	nRedSum=nGreenSum=nBlueSum=nChildren=0;
	for(i=0;i<8;i++)
	{
		if(pNode->pChild[i]!=NULL)
		{
			nRedSum+=pNode->pChild[i]->nRedSum;
			nGreenSum+=pNode->pChild[i]->nGreenSum;
            nBlueSum+=pNode->pChild[i]->nBlueSum;
			pNode->nPixelCount+=pNode->pChild[i]->nPixelCount;
			HeapFree(GetProcessHeap(),0,pNode->pChild[i]);
			pNode->pChild[i]=NULL;
            nChildren++;
		}
	}
	pNode->bIsLeaf=TRUE;
	pNode->nRedSum=nRedSum;
	pNode->nBlueSum=nBlueSum;
    pNode->nGreenSum=nGreenSum;
    *pLeafCount-=(nChildren-1);
}
int  WINAPI  GetRightShiftCount(DWORD dwVal)
{
	int i;
	for(i=0;i<sizeof(DWORD)*8;i++)
	{
		if(dwVal&1)
			return i;
		dwVal>>=1;
	}
	return -1;
}
int  WINAPI  GetLeftShiftCount(DWORD dwVal)
{
	int i,nCount;
	nCount=0;
	for(i=0;i<sizeof(DWORD)*8;i++)
	{
		if(dwVal&1)
			nCount++;
		dwVal>>=1;
	}
	return (8-nCount);
}
void  WINAPI  DeleteTree(NODE** ppNode)  
{
	int i;
	for(i=0;i<8;i++)
	{
		if((*ppNode)->pChild[i]!=NULL)
			DeleteTree(&((*ppNode)->pChild[i]));
	}
    HeapFree(GetProcessHeap(),0,*ppNode);
	*ppNode=NULL;
}
void  WINAPI  GetPaletteColors(NODE* pTree,PALETTEENTRY* pPalEntries,UINT* pIndex)
{
	int i;
	if(pTree->bIsLeaf)
	{
		pPalEntries[*pIndex].peRed=(BYTE)((pTree->nRedSum)/(pTree->nPixelCount));
		pPalEntries[*pIndex].peGreen=(BYTE)((pTree->nGreenSum)/(pTree->nPixelCount));
		pPalEntries[*pIndex].peBlue=(BYTE)((pTree->nBlueSum)/(pTree->nPixelCount));
		(*pIndex)++;
	}
	else
	{
		for(i=0;i<8;i++)
		{
			if(pTree->pChild[i]!=NULL)
                  GetPaletteColors(pTree->pChild[i],pPalEntries,pIndex);
		}
	}
}
NODE*  WINAPI  CreateNode(UINT nLevel,UINT nColorBits,UINT* pLeafCount,NODE** pReducibleNodes)
{
	NODE* pNode;
	if((pNode=(NODE*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(NODE)))==NULL)
		return NULL;
    pNode->bIsLeaf=(nLevel==nColorBits)?TRUE:FALSE;
	if(pNode->bIsLeaf)
        (*pLeafCount)++;
	else
	{
		pNode->pNext=pReducibleNodes[nLevel];
		pReducibleNodes[nLevel]=pNode;
	}
	return pNode;
}

//直方图正态化
void    WINAPI    Zhengtaihuawhw(unsigned char *ii, int Row, int Col)
{
	int ymax,ymin;
	///heikin为求出的均值,sigma为求出的标准偏差
	float heikin=0.,busan=0.;
	float sbs;
	double heikin0,sigma0;

	heikin0 = 80.0;               //小数值效果好些
	sigma0  = 60.0;
	ymax    = 250;
	ymin    = 1;

	unsigned char *oi;	
    int hmax,hmin,k,i,j,l;
	float hist[256],oihist[256];
	
    oi =(unsigned char *) malloc(Row*Col);
	if(!oi)
		return ;
    
    for(i=0;i<256;i++)
	{
		hist[i]=0.0;
		oihist[i]=0.0;
	}
	for(i=0;i<Row*Col;i++)
		oi[i]=ii[i];

	 hmax = 0;
	 hmin = 0xff;
//得到灰度最大、最小值
     for( i=0;i<Row*Col;i++)
	 {
		 j =(long int)oi[i] ;
		 if(j > hmax)
			 hmax = j;
		 if(j<hmin)
			 hmin = j;
	 }
//计算原始图像直方图
	for( i=0;i<Col;i++) 
	for( j=0;j<Row;j++)
	{
			hist[oi[j+i*Row]]++;
	
	}
//计算原始图像累积分布函数
	for(i=0;i<256;i++)
	{
		hist[i]=hist[i]/(Row*Col);
	}

	for(i=1;i<256;i++)
	{
		hist[i]=hist[i]+hist[i-1];
	}
//计算正态化直方图
	for( i=ymin;i<=ymax;i++)
	{
		oihist[i] =(float)( exp(-(i-heikin0)*(i-heikin0)
			/2/sigma0 /sigma0 )/sigma0/sqrt(2*3.1415926));
	}
//计算正态化累积分布函数
	for(i=ymin+1;i<=ymax;i++)
	{
		oihist[i]=oihist[i]+oihist[i-1];
	}
//如果累积分布函数不为1,将其拉伸
	sbs=1.0f/oihist[ymax];
	for(i=ymin;i<=ymax;i++)
	{
		oihist[i]=oihist[i]*sbs;
	}
//得正态化处理后的灰度级
    for(i=0;i<Col;i++)
	for(j=0;j<Row;j++)
	{
		k = oi[j+i*Row];
		for(l=0;l<256;l++)
		{
			if(oihist[l]>=hist[k])
			break;
		}
		ii[j+i*Row] =(unsigned char) l;
	}
	free(oi);
	return;
}

void    WINAPI    Logtranslate(unsigned char *ii, int Row, int Col)
{
	double eps=0.001;
    int i,j,z1=20,z2=250;
    double za=9999.,zb=-9999.;
    double logza,zzlba,logfij,fij,gij;

    for(i=0;i<(int)Col;i++)
	{
		for(j=0;j<(int)Row;j++)
		{
			if (za>ii[i*Row+j])
				za=ii[i*Row+j];
			if (zb<ii[i*Row+j])
				zb=ii[i*Row+j];
		}
	}
	if(za==zb)
		return;
	if(za==0)
		za=eps;

     logza=log(za);
     zzlba=(z2-z1)/(log(zb)-logza);


     for(i=0;i<(int)Col;i++)
     {
		 for(j=0;j<(int)Row;j++)
		 {
			 fij=ii[i*Row+j];
			 if (fij==0)
				 fij=eps;
			 logfij=log(fij);
			 gij=zzlba*(logfij-logza)+z1;
			 ii[i*Row+j]=(int)(gij+.5);
		 }
	 }
}
//整个波段的均值
void  WINAPI  C_Means(int *buffer,int m_width,int m_height,int index,double *Means)
{
	CProgressDlg  progdlg;
	progdlg.Create();
	progdlg.SetPos(0);
	progdlg.SetStatus("正在计算波段均值,请稍候 ...");
	progdlg.SetStep(1);
	progdlg.SetRange(0,100);
	int i,k;
	double col_mean;
	int N=m_width*m_height;
	for(k=1;k<index;k++)
	{
		progdlg.SetPos((int)((k*100/index)));
		col_mean=0;
		for(i=0;i<N;i++)
		{
		  col_mean+=buffer[(k-1)*N+i];
		}
		col_mean/=N;
		Means[k]=col_mean;
	}
}
//整个波段的方差
void  WINAPI   C_StandardVar(int *buffer,int m_width,int m_height,double *Means,int index,double *Function)
{
//index-----spectrum num
	CProgressDlg  progdlg;
	progdlg.Create();
	progdlg.SetPos(0);
	progdlg.SetStatus("正在计算波段方差,请稍候 ...");
	progdlg.SetStep(1);
	progdlg.SetRange(0,100);
	int i,j;
	int N;
	N=m_width*m_height;
	for(i=1;i<index;i++)
	{
		progdlg.SetPos((int)((i*100/index)));
		for(j=i;j<index;j++)
		{
			int m;
			double var;
			var=0.0;
			for(m=0;m<N;m++)
				var+=(buffer[(i-1)*N+m]-Means[i])*(buffer[(j-1)*N+m]-Means[j]);
			Function[i*index+j]=Function[j*index+i]=var/(N-1);
		}
	}
}
void  WINAPI   jacobi(double *a, int n, double *d, double* v)
{
    double b[300], z[300],g,s,c,h,tau,sss,ddd,t,tresh,sm,theta;
	int ip,iq,i,j;
    for (ip = 1; ip<n; ip++)
	{
        for (iq = 1; iq<n; iq++)
		{
            v[ip*n+iq] = 0.0;
        }
        v[ip*n+ip] = 1.0;
    }
    for (ip = 1; ip<n; ip++)
	{
        b[ip] = a[ip*n+ip];
        d[ip] = b[ip];
        z[ip] = 0.0;
    }
    for (i = 1; i<=250; i++)
	{
        sm = 0.0;
        for (ip = 1; ip<n - 1; ip++)
		{
            for (iq = ip + 1; iq<n; iq++)
			{
                sm = sm + fabs(a[ip*n+iq]);
            }
        }
        if (sm == 0.0)
		{
			return;
		}
        if (i < 4)
		{
            tresh = 0.2 * sm /double(n*n);
		}
        else
		{
            tresh = 0.0;
        }
        for (ip = 1; ip<n - 1; ip++)
		{
            for (iq = ip + 1; iq<n; iq++)
			{
                g = 100.0 * fabs(a[ip*n+iq]);
                sss = fabs(d[ip]) + g;
                ddd = fabs(d[iq]) + g;
                if ((i > 4) && (sss == fabs(d[ip])) && (ddd == fabs(d[iq])))
				{
                    a[ip*n+iq] = 0.0;
				}
                else 
				{
					if (fabs(a[ip*n+iq]) > tresh)
					{
						h = d[iq] - d[ip];
						if ((fabs(h) + g) == fabs(h))
						{
							t = a[ip*n+iq] / h;
						}
                        else
						{
						  theta=0.5 * h / a[ip*n+iq];
						  t=1.0/(fabs(theta)+sqrt(1.0+pow(theta,2)));
						  if (theta < 0.0 ) 
						  t = -t;
						}
						c = 1.0 / sqrt(1.0 + t *t);
						s = t * c;
						tau = s / (1.0 + c);
						h = t * a[ip*n+iq];
						z[ip] = z[ip] - h;
						z[iq] = z[iq] + h;
						d[ip] = d[ip] - h;
						d[iq] = d[iq] + h;
						a[ip*n+iq] = 0.0;
						for (j = 1; j<=ip - 1; j++)
						{
							g = a[j*n+ip];
							h = a[j*n+iq];
							a[j*n+ip] = g - s * (h + g * tau);
							a[j*n+iq] = h + s * (g - h * tau);
						}
						for (j = ip + 1 ; j<=iq - 1; j++)
						{
							g = a[ip*n+j];
							h = a[j*n+iq];
							a[ip*n+j] = g - s * (h + g * tau);
							a[j*n+iq] = h + s * (g - h * tau);
						}
						for (j = iq + 1 ;j<n; j++)
						{
							g = a[ip*n+j];
							h = a[iq*n+j];
							a[ip*n+j] = g - s * (h + g * tau);
							a[iq*n+j] = h + s * (g - h * tau);
						}
						for (j = 1 ;j<n; j++)

⌨️ 快捷键说明

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