灰度图像腐蚀(vc实现)




void CISLSView::OnErosionGray()
	{

		//int structure[3][3]={23,21,10,97,23,98,0,0,123};
		//int structure[3][3]={1,1,1,1,1,1,1,1,1};
		//int structure[3][3]={255,255,255,255,255,255,255,255,255};
		int structure[3][3]={5,225,155,0,255,0,25,25,25};
		erosionGray(structure);
	}





	void CISLSView::erosionGray(int structure[3][3])
	{
		if(myImage1.IsNull())
			OnOpenResourceFile();
		if(!myImage2.IsNull())
			myImage2.Destroy();
		if(myImage2.IsNull()){
			myImage2.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);
		}
		if(myImage3.IsNull()){
			myImage3.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);
		}
		//COLORREF pixel; 
		int maxY = myImage1.GetHeight();
		int maxX=myImage1.GetWidth();
		byte* pRealData;
		byte* pRealData2;
		byte* pRealData3;
		pRealData=(byte*)myImage1.GetBits();
		pRealData2=(byte*)myImage2.GetBits();
		pRealData3=(byte*)myImage3.GetBits();
		int pit=myImage1.GetPitch();
		int pit2=myImage2.GetPitch();
		int pit3=myImage3.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 bitCount3=myImage3.GetBPP()/8;
		int tempR,tempG,tempB;
		//float temp,tempX,tempY;
		int temp;
		float u0,u1;   //均值
		float w0,w1;   //概率
		float sum0,sum1;  //像素和
		int optIndex,optT;   //最优阈值,及其所在像素的值
		float fVaria,fMaxVaria=0;   //临时方差,最大方差

		//int pixelR[256],pixelG[256],pixelB[256];
		int pixel[256]={0};   //不要忘记初始化
		//灰度化
		for (int y=0; y<maxY; y++) {
			for (int x=0; x<maxX; x++) {
				temp=*(pRealData+pit*(y)+(x)*bitCount);
				if(bitCount==3)
				{
					tempR=*(pRealData+pit*(y)+(x)*bitCount);
					tempG=*(pRealData+pit*(y)+(x)*bitCount+1);
					tempB=*(pRealData+pit*(y)+(x)*bitCount+2);
					temp=(int)(tempR*0.49+tempG*0.31+tempB*0.2);
					//temp=(int)((tempR+tempG+tempB)/3);
				}
				*(pRealData3+pit3*(y)+(x)*bitCount3)=temp;
				*(pRealData3+pit3*(y)+(x)*bitCount3+1)=temp;
				*(pRealData3+pit3*(y)+(x)*bitCount3+2)=temp;

			}
		}
		//CString str;
		//str.Format(TEXT("%d"),Td);
		//AfxMessageBox(str);
		//
		//for (int y=0; y<maxY; y++) {
		//	for (int x=0; x<maxX; x++) {

		//		*(pRealData2+pit2*(y)+(x)*bitCount2)=255;
		//		*(pRealData2+pit2*(y)+(x)*bitCount2+1)=255;
		//		*(pRealData2+pit2*(y)+(x)*bitCount2+2)=255;

		//	}
		//}
		int m,n;
		int src,des;
		src=des=255;
		for (int y=1; y<maxY-1; y++) {
			for (int x=1; x<maxX-1; x++) {
				for(m=0;m<3;m++)
					for(n=0;n<3;n++)
					{	
						src=*(pRealData3+pit3*(y-m+1)+(x+n-1)*bitCount3)-structure[m][n];
						//src=abs(src);
						if(src<des)
						{
							des=src;				
						}
					}
					des=abs(des);
					*(pRealData2+pit2*(y)+(x)*bitCount2)=des;
					*(pRealData2+pit2*(y)+(x)*bitCount2+1)=des;
					*(pRealData2+pit2*(y)+(x)*bitCount2+2)=des;
			}
		}
		Invalidate();
	}



void CISLSView::erosionGray(int structure[3][3])
	{
		//程序编制:李立宗  lilizong@gmail.com
		//2012-8-17
		//2012-8-22修改 ,将src=des=255;移动到每个像素循环的内部。
		if(myImage1.IsNull())
			OnOpenResourceFile();
		if(!myImage2.IsNull())
			myImage2.Destroy();
		if(myImage2.IsNull()){
			myImage2.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);
		}
		if(myImage3.IsNull()){
			myImage3.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);
		}
		//COLORREF pixel; 
		int maxY = myImage1.GetHeight();
		int maxX=myImage1.GetWidth();
		byte* pRealData;
		byte* pRealData2;
		byte* pRealData3;
		pRealData=(byte*)myImage1.GetBits();
		pRealData2=(byte*)myImage2.GetBits();
		pRealData3=(byte*)myImage3.GetBits();
		int pit=myImage1.GetPitch();
		int pit2=myImage2.GetPitch();
		int pit3=myImage3.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 bitCount3=myImage3.GetBPP()/8;
		int tempR,tempG,tempB;
		//float temp,tempX,tempY;
		int temp;
		float u0,u1;   //均值
		float w0,w1;   //概率
		float sum0,sum1;  //像素和
		int optIndex,optT;   //最优阈值,及其所在像素的值
		float fVaria,fMaxVaria=0;   //临时方差,最大方差

		//int pixelR[256],pixelG[256],pixelB[256];
		int pixel[256]={0};   //不要忘记初始化
		//灰度化
		for (int y=0; y<maxY; y++) {
			for (int x=0; x<maxX; x++) {
				temp=*(pRealData+pit*(y)+(x)*bitCount);
				if(bitCount==3)
				{
					tempR=*(pRealData+pit*(y)+(x)*bitCount);
					tempG=*(pRealData+pit*(y)+(x)*bitCount+1);
					tempB=*(pRealData+pit*(y)+(x)*bitCount+2);
					temp=(int)(tempR*0.49+tempG*0.31+tempB*0.2);
					//temp=(int)((tempR+tempG+tempB)/3);
				}
				*(pRealData3+pit3*(y)+(x)*bitCount3)=temp;
				*(pRealData3+pit3*(y)+(x)*bitCount3+1)=temp;
				*(pRealData3+pit3*(y)+(x)*bitCount3+2)=temp;

			}
		}
		//CString str;
		//str.Format(TEXT("%d"),Td);
		//AfxMessageBox(str);
		//
		//for (int y=0; y<maxY; y++) {
		//	for (int x=0; x<maxX; x++) {

		//		*(pRealData2+pit2*(y)+(x)*bitCount2)=255;
		//		*(pRealData2+pit2*(y)+(x)*bitCount2+1)=255;
		//		*(pRealData2+pit2*(y)+(x)*bitCount2+2)=255;

		//	}
		//}
		int m,n;
		int src,des;
		for (int y=1; y<maxY-1; y++) {
			for (int x=1; x<maxX-1; x++) {
				src=des=255;
				for(m=0;m<3;m++)
					for(n=0;n<3;n++)
					{	
						src=*(pRealData3+pit3*(y-m+1)+(x+n-1)*bitCount3)-structure[m][n];
						//src=abs(src);
						if(src<des)
						{
							des=src;				
						}
					}
					des=abs(des);
					*(pRealData2+pit2*(y)+(x)*bitCount2)=des;
					*(pRealData2+pit2*(y)+(x)*bitCount2+1)=des;
					*(pRealData2+pit2*(y)+(x)*bitCount2+2)=des;
			}
		}
		Invalidate();
	}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

superdont

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值