自适应阈值分割(vc实现)

1.以8×8邻域

划分太细,整体效果像铅笔素描。

[cpp] view plain copy print ?
  1. void CISLSView::OnThresholdAdaptive88()  
  2.     {  
  3. //程序编制:李立宗   
  4. //[email protected]   
  5. //2012-8-14   
  6.         if(myImage1.IsNull())  
  7.             OnOpenResourceFile();  
  8.         if(!myImage2.IsNull())  
  9.             myImage2.Destroy();  
  10.         if(myImage2.IsNull()){  
  11.             myImage2.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);  
  12.         }  
  13.                 if(myImage3.IsNull()){  
  14.             myImage3.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);  
  15.         }  
  16.         //COLORREF pixel;    
  17.         int maxY = myImage1.GetHeight();  
  18.         int maxX=myImage1.GetWidth();  
  19.         byte* pRealData;  
  20.         byte* pRealData2;  
  21.         byte* pRealData3;  
  22.         pRealData=(byte*)myImage1.GetBits();  
  23.         pRealData2=(byte*)myImage2.GetBits();  
  24.         pRealData3=(byte*)myImage3.GetBits();  
  25.         int pit=myImage1.GetPitch();  
  26.         int pit2=myImage2.GetPitch();  
  27.         int pit3=myImage3.GetPitch();  
  28.         //需要注意,pit和pit2的值并不一样,所以如果使用一个值,会导致不同的结果出现  
  29.         //CString str;   
  30.         //str.Format(TEXT("%d"),pit);   
  31.         //MessageBox(str);   
  32.         //str.Format(TEXT("%d"),pit2);   
  33.         //MessageBox(str);   
  34.         int bitCount=myImage1.GetBPP()/8;  
  35.         int bitCount2=myImage2.GetBPP()/8;  
  36.         int bitCount3=myImage3.GetBPP()/8;  
  37.         int tempR,tempG,tempB;  
  38.         //float temp,tempX,tempY;   
  39.         int temp;  
  40.         int T=128;  
  41.         //int pixel[4];   
  42.         float u0,u1;   //均值  
  43.         float w0,w1;   //概率  
  44.         float sum0,sum1;  //像素和  
  45.         int optIndex,optT;   //最优阈值,及其所在像素的值  
  46.         float fVaria,fMaxVaria=0;   //临时方差,最大方差  
  47.         int i;        //循环变量  
  48.         //int pixelR[256],pixelG[256],pixelB[256];  
  49.         int pixel[256]={0};   //不要忘记初始化  
  50.         //灰度化   
  51.         for (int y=0; y
  52.             for (int x=0; x
  53.                 temp=*(pRealData+pit*(y)+(x)*bitCount);  
  54.                 if(bitCount==3)  
  55.                 {  
  56.                     tempR=*(pRealData+pit*(y)+(x)*bitCount);  
  57.                     tempG=*(pRealData+pit*(y)+(x)*bitCount+1);  
  58.                     tempB=*(pRealData+pit*(y)+(x)*bitCount+2);  
  59.                     temp=(int)(tempR*0.49+tempG*0.31+tempB*0.2);  
  60.                     //temp=(int)((tempR+tempG+tempB)/3);  
  61.                 }  
  62.                 *(pRealData3+pit3*(y)+(x)*bitCount3)=temp;  
  63.                 *(pRealData3+pit3*(y)+(x)*bitCount3+1)=temp;  
  64.                 *(pRealData3+pit3*(y)+(x)*bitCount3+2)=temp;  
  65.   
  66.             }  
  67.         }  
  68.         //计算除边界外的点   
  69.         for (int y=1; y
  70.             for (int x=1; x
  71.                 temp=*(pRealData3+pit3*(y)+(x)*bitCount3);  
  72.                 T=*(pRealData3+pit3*(y-1)+(x-1)*bitCount3)+*(pRealData3+pit3*(y-1)+(x)*bitCount3)+*(pRealData3+pit3*(y-1)+(x+1)*bitCount3)  
  73.                 +*(pRealData3+pit3*(y)+(x-1)*bitCount3)+*(pRealData3+pit3*(y)+(x)*bitCount3)+*(pRealData3+pit3*(y)+(x+1)*bitCount3)+  
  74.                 *(pRealData3+pit3*(y+1)+(x-1)*bitCount3)+*(pRealData3+pit3*(y+1)+(x)*bitCount3)+*(pRealData3+pit3*(y+1)+(x+1)*bitCount3);  
  75.                 T=(int)(T/9.0);  
  76.         //              CString str;   
  77.         //str.Format(TEXT("%d"),T);  
  78.         //MessageBox(str);   
  79.                 if(temp>T)  
  80.                     temp=255;  
  81.                 else  
  82.                 temp=0;  
  83.   
  84.                 *(pRealData2+pit2*(y)+(x)*bitCount2)=temp;  
  85.                 *(pRealData2+pit2*(y)+(x)*bitCount2+1)=temp;  
  86.                 *(pRealData2+pit2*(y)+(x)*bitCount2+2)=temp;  
  87.             }  
  88.         }  
  89.         Invalidate();  
  90.     }  
void CISLSView::OnThresholdAdaptive88()
	{
//程序编制:李立宗
//[email protected]
//2012-8-14
		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;
		int T=128;
		//int pixel[4];
		float u0,u1;   //均值
		float w0,w1;   //概率
		float sum0,sum1;  //像素和
		int optIndex,optT;   //最优阈值,及其所在像素的值
		float fVaria,fMaxVaria=0;   //临时方差,最大方差
		int i;        //循环变量
		//int pixelR[256],pixelG[256],pixelB[256];
		int pixel[256]={0};   //不要忘记初始化
		//灰度化
		for (int y=0; yT)
					temp=255;
				else
				temp=0;

				*(pRealData2+pit2*(y)+(x)*bitCount2)=temp;
				*(pRealData2+pit2*(y)+(x)*bitCount2+1)=temp;
				*(pRealData2+pit2*(y)+(x)*bitCount2+2)=temp;
			}
		}
		Invalidate();
	}

2.将图像划分为四个区域计算

该模式缺点,可以看到四个区域之间具有明显的边界。

[cpp] view plain copy print ?
  1. void CISLSView::OnThresholdAdaptive41()  
  2.     {  
  3. //程序编制:李立宗 [email protected]   
  4. //2012-8-14   
  5.         if(myImage1.IsNull())  
  6.             OnOpenResourceFile();  
  7.         if(!myImage2.IsNull())  
  8.             myImage2.Destroy();  
  9.         if(myImage2.IsNull()){  
  10.             myImage2.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);  
  11.         }  
  12.                 if(myImage3.IsNull()){  
  13.             myImage3.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);  
  14.         }  
  15.         //COLORREF pixel;    
  16.         int maxY = myImage1.GetHeight();  
  17.         int maxX=myImage1.GetWidth();  
  18.         byte* pRealData;  
  19.         byte* pRealData2;  
  20.         byte* pRealData3;  
  21.         pRealData=(byte*)myImage1.GetBits();  
  22.         pRealData2=(byte*)myImage2.GetBits();  
  23.         pRealData3=(byte*)myImage3.GetBits();  
  24.         int pit=myImage1.GetPitch();  
  25.         int pit2=myImage2.GetPitch();  
  26.         int pit3=myImage3.GetPitch();  
  27.         //需要注意,pit和pit2的值并不一样,所以如果使用一个值,会导致不同的结果出现  
  28.         //CString str;   
  29.         //str.Format(TEXT("%d"),pit);  
  30.         //MessageBox(str);   
  31.         //str.Format(TEXT("%d"),pit2);  
  32.         //MessageBox(str);   
  33.         int bitCount=myImage1.GetBPP()/8;  
  34.         int bitCount2=myImage2.GetBPP()/8;  
  35.         int bitCount3=myImage3.GetBPP()/8;  
  36.         int tempR,tempG,tempB;  
  37.         //float temp,tempX,tempY;  
  38.         int temp;  
  39.         int T=128;  
  40.         //int pixel[4];   
  41.         float u0,u1;   //均值  
  42.         float w0,w1;   //概率  
  43.         float sum0,sum1;  //像素和  
  44.         int optIndex,optT;   //最优阈值,及其所在像素的值  
  45.         float fVaria,fMaxVaria=0;   //临时方差,最大方差  
  46.         int i;        //循环变量  
  47.         //int pixelR[256],pixelG[256],pixelB[256];  
  48.         int pixel[256]={0};   //不要忘记初始化  
  49.         int sum=0,avg;  
  50.         //灰度化   
  51.         for (int y=0; y
  52.             for (int x=0; x
  53.                 temp=*(pRealData+pit*(y)+(x)*bitCount);  
  54.                 if(bitCount==3)  
  55.                 {  
  56.                     tempR=*(pRealData+pit*(y)+(x)*bitCount);  
  57.                     tempG=*(pRealData+pit*(y)+(x)*bitCount+1);  
  58.                     tempB=*(pRealData+pit*(y)+(x)*bitCount+2);  
  59.                     temp=(int)(tempR*0.49+tempG*0.31+tempB*0.2);  
  60.                     //temp=(int)((tempR+tempG+tempB)/3);  
  61.                 }  
  62.                 *(pRealData3+pit3*(y)+(x)*bitCount3)=temp;  
  63.                 *(pRealData3+pit3*(y)+(x)*bitCount3+1)=temp;  
  64.                 *(pRealData3+pit3*(y)+(x)*bitCount3+2)=temp;  
  65.   
  66.             }  
  67.         }  
  68.         //计算左上角阈值   
  69.         for (int y=0; y
  70.             for (int x=0; x
  71.                 sum+=*(pRealData3+pit3*(y)+(x)*bitCount3);                
  72.             }  
  73.         }         
  74.         avg=(int)(sum/(maxY/2*(maxX/2)));  
  75.         //处理左上角   
  76.         for (int y=0; y
  77.             for (int x=0; x
  78.                 temp=*(pRealData3+pit3*(y)+(x)*bitCount3);    
  79.                 if(temp>avg)  
  80.                     temp=255;  
  81.                 else  
  82.                 temp=0;  
  83.   
  84.                 *(pRealData2+pit2*(y)+(x)*bitCount2)=temp;  
  85.                 *(pRealData2+pit2*(y)+(x)*bitCount2+1)=temp;  
  86.                 *(pRealData2+pit2*(y)+(x)*bitCount2+2)=temp;  
  87.             }  
  88.         }  
  89.         //右上角   
  90.         sum=0;  
  91.         for (int y=0; y
  92.             for (int x=maxX/2; x
  93.                 sum+=*(pRealData3+pit3*(y)+(x)*bitCount3);                
  94.             }  
  95.         }         
  96.         avg=(int)(sum/(maxY/2*(maxX/2)));  
  97.         for (int y=0; y
  98.             for (int x=maxX/2; x
  99.                 temp=*(pRealData3+pit3*(y)+(x)*bitCount3);    
  100.                 if(temp>avg)  
  101.                     temp=255;  
  102.                 else  
  103.                 temp=0;  
  104.   
  105.                 *(pRealData2+pit2*(y)+(x)*bitCount2)=temp;  
  106.                 *(pRealData2+pit2*(y)+(x)*bitCount2+1)=temp;  
  107.                 *(pRealData2+pit2*(y)+(x)*bitCount2+2)=temp;  
  108.             }  
  109.         }  
  110.         //左下角   
  111.         sum=0;  
  112.                 for (int y=maxY/2; y
  113.             for (int x=0; x
  114.                 sum+=*(pRealData3+pit3*(y)+(x)*bitCount3);                
  115.             }  
  116.         }         
  117.         avg=(int)(sum/(maxY/2*(maxX/2)));  
  118.         for (int y=maxY/2; y
  119.             for (int x=0; x
  120.                 temp=*(pRealData3+pit3*(y)+(x)*bitCount3);    
  121.                 if(temp>avg)  
  122.                     temp=255;  
  123.                 else  
  124.                 temp=0;  
  125.   
  126.                 *(pRealData2+pit2*(y)+(x)*bitCount2)=temp;  
  127.                 *(pRealData2+pit2*(y)+(x)*bitCount2+1)=temp;  
  128.                 *(pRealData2+pit2*(y)+(x)*bitCount2+2)=temp;  
  129.             }  
  130.         }  
  131.         //右下角   
  132.         sum=0;  
  133.                 for (int y=maxY/2; y
  134.             for (int x=maxX/2; x
  135.                 sum+=*(pRealData3+pit3*(y)+(x)*bitCount3);                
  136.             }  
  137.         }         
  138.         avg=(int)(sum/(maxY/2*(maxX/2)));  
  139.         for (int y=maxY/2; y
  140.             for (int x=maxX/2; x
  141.                 temp=*(pRealData3+pit3*(y)+(x)*bitCount3);    
  142.                 if(temp>avg)  
  143.                     temp=255;  
  144.                 else  
  145.                 temp=0;  
  146.   
  147.                 *(pRealData2+pit2*(y)+(x)*bitCount2)=temp;  
  148.                 *(pRealData2+pit2*(y)+(x)*bitCount2+1)=temp;  
  149.                 *(pRealData2+pit2*(y)+(x)*bitCount2+2)=temp;  
  150.             }  
  151.         }  
  152.   
  153.   
  154.         Invalidate();  
  155.     }  

你可能感兴趣的:(图像处理)