`

图像高提升滤波的vc实现

阅读更多




void CSDIELSView::OnSharpeningEnhance()
	{
//程序编制:李立宗   lilizong@gmail.com
//2012-8-12

		if(myImage1.IsNull())
			OnOpenResourceFile();
		if(!myImage2.IsNull())
			myImage2.Destroy();
		if(myImage2.IsNull()){
			myImage2.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);
		}
		//COLORREF pixel; 
		int maxY = myImage1.GetHeight();
		int maxX=myImage1.GetWidth();
		byte* pRealData;
		byte* pRealData2;
		pRealData=(byte*)myImage1.GetBits();
		pRealData2=(byte*)myImage2.GetBits();
		int pit=myImage1.GetPitch();
		int pit2=myImage2.GetPitch();
		//需要注意,pit和pit2的值并不一样,所以如果使用一个值,会导致不同的结果出现
		//CString str;
		//str.Format(TEXT("%d"),pit);
		//MessageBox(str);
		//str.Format(TEXT("%d"),pit2);
		//MessageBox(str);
		int bitCount=myImage1.GetBPP()/8;
		int bitCount2=myImage2.GetBPP()/8;
		int tempR,tempG,tempB;
		int tempX,tempY;
		int temp;
		float tempA;
		//int M[3][3]={{1,2,1},{2,4,2},{1,2,1}};
		int t=100; //门限
		int f=1;  //参数,原图像的比例值
		float maxR,minR,maxG,minG,maxB,minB;
		float spanR,spanG,spanB;
		static int tempArray[1000][1000][3]={0};  //必须定义为static,否则无法支持大数组
		//	tempR=tempG=tempG=0;
		//说明:将生产的图像作为24位图处理。
		for (int y=1; y<maxY-1; y++) {
			for (int x=1; x<maxX-1; x++) {
				temp=
					*(pRealData+pit*(y-1)+(x-1)*bitCount)+*(pRealData+pit*(y-1)+(x)*bitCount)+*(pRealData+pit*(y-1)+(x+1)*bitCount)
					+*(pRealData+pit*(y)+(x-1)*bitCount)-8*(*(pRealData+pit*(y)+(x)*bitCount))+*(pRealData+pit*(y)+(x+1)*bitCount)
					+*(pRealData+pit*(y+1)+(x-1)*bitCount)+*(pRealData+pit*(y+1)+(x)*bitCount)+*(pRealData+pit*(y+1)+(x+1)*bitCount);
				if(temp>255)
					temp=255;
				if(temp<-255)
					temp=-255;

				tempArray[y][x][0]=f*(*(pRealData+pit*(y)+(x)*bitCount))+temp+0.5;
				if(bitCount==1)
				{
					/*tempG=tempR;
					tempB=tempR;*/
					tempArray[y][x][1]=tempArray[y][x][0];
					tempArray[y][x][2]=tempArray[y][x][0];
				}
				else
				{
					temp=
						*(pRealData+pit*(y-1)+(x-1)*bitCount+1)+*(pRealData+pit*(y-1)+(x)*bitCount+1)+*(pRealData+pit*(y-1)+(x+1)*bitCount+1)
						+*(pRealData+pit*(y)+(x-1)*bitCount+1)-8*(*(pRealData+pit*(y)+(x)*bitCount+1))+*(pRealData+pit*(y)+(x+1)*bitCount+1)
						+*(pRealData+pit*(y+1)+(x-1)*bitCount+1)+*(pRealData+pit*(y+1)+(x)*bitCount+1)+*(pRealData+pit*(y+1)+(x+1)*bitCount+1);
					if(temp>255)
						temp=255;
					if(temp<-255)
						temp=-255;
					tempArray[y][x][1]=f*(*(pRealData+pit*(y)+(x)*bitCount+1))+temp+0.5;


					temp=
						*(pRealData+pit*(y-1)+(x-1)*bitCount+2)+*(pRealData+pit*(y-1)+(x)*bitCount+2)+*(pRealData+pit*(y-1)+(x+1)*bitCount+2)
						+*(pRealData+pit*(y)+(x-1)*bitCount+2)-8*(*(pRealData+pit*(y)+(x)*bitCount+2))+*(pRealData+pit*(y)+(x+1)*bitCount+2)
						+*(pRealData+pit*(y+1)+(x-1)*bitCount+2)+*(pRealData+pit*(y+1)+(x)*bitCount+2)+*(pRealData+pit*(y+1)+(x+1)*bitCount+2);
					if(temp>255)
						temp=255;
					if(temp<-255)
						temp=-255;
					tempArray[y][x][2]=f*(*(pRealData+pit*(y)+(x)*bitCount+2))+temp+0.5;

				}


			}
		}
		maxR=maxG=maxB=0;
		minR=minG=minB=65535;

		for (int y=1; y<maxY-1; y++) {
			for (int x=1; x<maxX-1; x++) {
				if(maxR<(tempArray[y][x][0]))
					maxR=tempArray[y][x][0];
				if(maxG<(tempArray[y][x][1]))
					maxG=tempArray[y][x][1];
				if(maxB<(tempArray[y][x][2]))
					maxB=tempArray[y][x][2];
				if(minR>(tempArray[y][x][0]))
					minR=tempArray[y][x][0];
				if(minG>(tempArray[y][x][1]))
					minG=tempArray[y][x][1];
				if(minB>(tempArray[y][x][2]))
					minB=tempArray[y][x][2];


			}
		}
		spanR=maxR-minR;
		spanG=maxG-minG;
		spanB=maxB-minB;

		for (int y=1; y<maxY-1; y++) {
			for (int x=1; x<maxX-1; x++) {

				if(spanR>0)
					tempR=(int)(tempArray[y][x][0]-minR)*255/spanR;
				else if(tempArray[y][x][0]<=255)
					tempR=tempArray[y][x][0];
				else
					tempR=255;

				if(spanG>0)
					tempG=(int)(tempArray[y][x][1]-minR)*255/spanR;
				else if(tempArray[y][x][1]<=255)
					tempG=tempArray[y][x][1];
				else
					tempG=255;
				if(spanB>0)
					tempB=(int)(tempArray[y][x][2]-minR)*255/spanR;
				else if(tempArray[y][x][2]<=255)
					tempB=tempArray[y][x][2];
				else
					tempB=255;


				*(pRealData2+pit2*y+x*bitCount2)=tempR;
				*(pRealData2+pit2*y+x*bitCount2+1)=tempG;
				*(pRealData2+pit2*y+x*bitCount2+2)=tempB;
			}
		}
		Invalidate();
	}


0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics