📄 filters.cs
字号:
sum=sum/80;
if(flag==1) sum=array[2][2];
else if(sum>255) sum=255;
else if(sum<0) sum=0;
return sum;
}
private double[][] ArrayMinuse(double[][] array1,double[][] array2)
{
double[][] re=(double[][] )array1.Clone();
for(int i=0;i<array1.Length;i++)
for(int j=0;j<array1[0].Length;j++)
re[i][j]=re[i][j]-array2[i][j];
return re;
}
private double[][] NumMulArray(double[][] array,double lamda)
{
double[][] re=GetClone(array);
for(int i=0;i<array.Length;i++)
for(int j=0;j<array[0].Length;j++)
re[i][j]=re[i][j]*lamda;
return re;
}
private int[][] FilterArray(Bitmap myBitmap,int width,int height) //算3x3的窗口矩阵
{
int[][] array=new int[3][];
array[0]=new int[3];
if(height>0&&width>1) array[0][0]=myBitmap.GetPixel(width-1,height-1).R;
else array[0][0]=999;
if(height>0) array[0][1]=myBitmap.GetPixel(width,height-1).R;
else array[0][1]=999;
if(height>1&&width<myBitmap.Width-1) array[0][2]=myBitmap.GetPixel(width+1,height-1).R;
else array[0][2]=999;
array[1]=new int[3];
if(width>1) array[1][0]=myBitmap.GetPixel(width-1,height).R;
else array[1][0]=999;
array[1][1]=myBitmap.GetPixel(width,height).R;
if(width<myBitmap.Width-1) array[1][2]=myBitmap.GetPixel(width+1,height).R;
else array[1][2]=999;
array[2]=new int[3];
if(height<myBitmap.Height-1&&width>1) array[2][0]=myBitmap.GetPixel(width-1,height+1).R;
else array[2][0]=999;
if(height<myBitmap.Height-1) array[2][1]=myBitmap.GetPixel(width,height+1).R;
else array[2][1]=999;
if(height<myBitmap.Height-1&&width<myBitmap.Width-1) array[2][2]=myBitmap.GetPixel(width+1,height+1).R;
else array[2][2]=999;
return array;
}
private int Regular(int a) //规范化
{
if (a>255) return 255;
if (a<0) return 0;
else return a;
}
private int[][] FilterArray(int width,int height) //算3x3的窗口矩阵
{
int[][] array=new int[3][];
array[0]=new int[3];
if(height>0&&width>1) array[0][0]=BitmapArray[width-1][height-1];
else array[0][0]=999;
if(height>0) array[0][1]=BitmapArray[width][height-1];
else array[0][1]=999;
if(height>1&&width<nWidth-1) array[0][2]=BitmapArray[width+1][height-1];
else array[0][2]=999;
array[1]=new int[3];
if(width>1) array[1][0]=BitmapArray[width-1][height];
else array[1][0]=999;
array[1][1]=BitmapArray[width][height];
if(width<nWidth-1) array[1][2]=BitmapArray[width+1][height];
else array[1][2]=999;
array[2]=new int[3];
if(height<nHeight-1&&width>1) array[2][0]=BitmapArray[width-1][height+1];
else array[2][0]=999;
if(height<nHeight-1) array[2][1]=BitmapArray[width][height+1];
else array[2][1]=999;
if(height<nHeight-1&&width<nWidth-1) array[2][2]=BitmapArray[width+1][height+1];
else array[2][2]=999;
return array;
}
private void AveragePixel(Bitmap myBitmap,int width,int height,int[][] array) //均值窗
{
int sum=0;
int flag=0;
for(int i=0;i<=2;i++)
for(int j=0;j<=2;j++)
{
if(array[i][j]!=999)
{
sum+=Average[i][j]*array[i][j];
}
else
{
flag=1;
}
}
sum=sum/9;
if(sum>255) sum=255;
if(sum<0) sum=0;
myBitmap.SetPixel(width,height,Color.FromArgb(sum,sum,sum));
//MySetPixel(width,height,myBitmap,(byte)sum);
}
private void PrewittPixel(Bitmap myBitmap,int width,int height,int[][] array) //Prewitt窗
{
int sum=0;
int flag=0;
for(int i=0;i<=2;i++)
for(int j=0;j<=2;j++)
{
if(array[i][j]!=999)
{
sum+=(Prewitt1[i][j]+Prewitt2[i][j])*array[i][j];
}
else
{
flag=1;
}
}
if(sum>255) sum=255;
if(sum<0) sum=0;
myBitmap.SetPixel(width,height,Color.FromArgb(sum,sum,sum));
}
private void SobelPixel(Bitmap myBitmap,int width,int height,int[][] array) //Sobel窗
{
int sum=0;
int flag=0;
for(int i=0;i<=2;i++)
for(int j=0;j<=2;j++)
{
if(array[i][j]!=999)
{
sum+=(Sobel1[i][j]+Sobel2[i][j])*array[i][j];
}
else
{
flag=1;
}
}
if(sum>255) sum=255;
if(sum<0) sum=0;
myBitmap.SetPixel(width,height,Color.FromArgb(sum,sum,sum));
}
private void KirschPixel(Bitmap myBitmap,int width,int height,int[][] array) //Kirsch窗
{
int sum=0;
int flag=0;
for(int i=0;i<=2;i++)
for(int j=0;j<=2;j++)
{
if(array[i][j]!=999)
{
sum+=Kirsch1[i][j]*array[i][j];
//sum+=(Kirsch1[i][j]+Kirsch2[i][j]+Kirsch3[i][j]+Kirsch4[i][j]+Kirsch5[i][j]+Kirsch6[i][j]+Kirsch7[i][j]+Kirsch8[i][j])*array[i][j];
}
else
{
flag=1;
}
}
//sum=sum/8;
if(sum>255) sum=255;
if(sum<0) sum=0;
myBitmap.SetPixel(width,height,Color.FromArgb(sum,sum,sum));
}
private void LaplacianPixel(Bitmap myBitmap,int width,int height,int[][] array) //Laplacian窗
{
int sum=0;
int flag=0;
for(int i=0;i<=2;i++)
for(int j=0;j<=2;j++)
{
if(array[i][j]!=999)
{
sum+=Laplacian[i][j]*array[i][j];
}
else
{
flag=1;
}
}
//sum=sum/8;
if(sum>255) sum=255;
if(sum<0) sum=0;
myBitmap.SetPixel(width,height,Color.FromArgb(sum,sum,sum));
}
private void GraPixel(Bitmap myBitmap,int width,int height,int[][] array) //梯度窗
{
int sum=0;
int flag=0;
for(int i=0;i<=2;i++)
for(int j=0;j<=2;j++)
{
if(array[i][j]!=999)
{
sum+=Sobel1[i][j]*array[i][j];
}
else
{
flag=1;
}
}
//sum=sum/8;
if(sum>255) sum=255;
if(sum<0) sum=0;
myBitmap.SetPixel(width,height,Color.FromArgb(sum,sum,sum));
}
private void MidPixel(Bitmap myBitmap,int width,int height,int[][] array) //中值窗
{
int sum=0;
int[] ar=new int[9];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
ar[i*3+j]=array[i][j];
}
BubbleUp(ar,9);
sum=ar[4];
if(sum==999)
sum=array[1][1];
myBitmap.SetPixel(width,height,Color.FromArgb(sum,sum,sum));
}
private void MaxPixel(Bitmap myBitmap,int width,int height,int[][] array) //最大值窗
{
int sum=0;
int[] ar=new int[9];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
ar[i*3+j]=array[i][j];
}
BubbleUp(ar,9);
sum=ar[8];
if(sum>255||sum<0)
sum=array[1][1];
myBitmap.SetPixel(width,height,Color.FromArgb(sum,sum,sum));
}
private void MinPixel(Bitmap myBitmap,int width,int height,int[][] array) //最小值窗
{
int sum=0;
int[] ar=new int[9];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
ar[i*3+j]=array[i][j];
}
BubbleUp(ar,9);
sum=ar[0];
if(sum>255||sum<0)
sum=array[1][1];
myBitmap.SetPixel(width,height,Color.FromArgb(sum,sum,sum));
}
private void MaxMinPixel(Bitmap myBitmap,int width,int height,int[][] array) //最大最小值窗
{
int sum=0;
int[] ar=new int[9];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
ar[i*3+j]=array[i][j];
}
BubbleUp(ar,9);
sum=(ar[0]+ar[8])/2;
if(sum>255||sum<0)
sum=array[1][1];
myBitmap.SetPixel(width,height,Color.FromArgb(sum,sum,sum));
}
private void AlphaPixel(Bitmap myBitmap,int width,int height,int[][] array) //alpha修饰滤波窗
{
int sum=0;
int[] ar=new int[9];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
ar[i*3+j]=array[i][j];
}
BubbleUp(ar,9);
sum=Alpha(2,ar);
if(sum>255||sum<0)
sum=array[1][1];
myBitmap.SetPixel(width,height,Color.FromArgb(sum,sum,sum));
}
public Bitmap Filter_Middle(Bitmap myBitmap) //中值滤波
{
int width=myBitmap.Width;
int height=myBitmap.Height;
Bitmap clone=new Bitmap(width,height);
for(int i=0;i<=height-1;i++)
for(int j=0;j<=width-1;j++)
MidPixel(clone,j,i,FilterArray(myBitmap,j,i));
return clone;
}
public Bitmap Filter_Alpha(Bitmap myBitmap) //alpha修饰滤波
{
int width=myBitmap.Width;
int height=myBitmap.Height;
Bitmap clone=new Bitmap(width,height);
for(int i=0;i<=height-1;i++)
for(int j=0;j<=width-1;j++)
AlphaPixel(clone,j,i,FilterArray(myBitmap,j,i));
return clone;
}
public Bitmap Filter_Max(Bitmap myBitmap) //最大值滤波
{
int width=myBitmap.Width;
int height=myBitmap.Height;
Bitmap clone=new Bitmap(width,height);
for(int i=0;i<=height-1;i++)
for(int j=0;j<=width-1;j++)
MaxPixel(clone,j,i,FilterArray(myBitmap,j,i));
return clone;
}
public Bitmap Filter_Min(Bitmap myBitmap) //最小值滤波
{
int width=myBitmap.Width;
int height=myBitmap.Height;
Bitmap clone=new Bitmap(width,height);
for(int i=0;i<=height-1;i++)
for(int j=0;j<=width-1;j++)
MinPixel(clone,j,i,FilterArray(myBitmap,j,i));
return clone;
}
public Bitmap Filter_Grad(Bitmap myBitmap) //梯度滤波
{
int width=myBitmap.Width;
int height=myBitmap.Height;
Bitmap clone=new Bitmap(width,height);
for(int i=0;i<=height-1;i++)
for(int j=0;j<=width-1;j++)
GraPixel(clone,j,i,FilterArray(myBitmap,j,i));
return clone;
}
public Bitmap Filter_MaxMin(Bitmap myBitmap) //最大最小滤波
{
int width=myBitmap.Width;
int height=myBitmap.Height;
Bitmap clone=new Bitmap(width,height);
for(int i=0;i<=height-1;i++)
for(int j=0;j<=width-1;j++)
MaxMinPixel(clone,j,i,FilterArray(myBitmap,j,i));
return clone;
}
public Bitmap Filter_Average(Bitmap myBitmap) //平均滤波
{
int width=myBitmap.Width;
int height=myBitmap.Height;
Bitmap clone=new Bitmap(width,height);
for(int i=0;i<=height-1;i++)
for(int j=0;j<=width-1;j++)
AveragePixel(clone,j,i,FilterArray(myBitmap,j,i));
return clone;
}
public Bitmap Filter_Sobel(Bitmap myBitmap) //Sobel滤波
{
int width=myBitmap.Width;
int height=myBitmap.Height;
Bitmap clone=new Bitmap(width,height);
for(int i=0;i<=height-1;i++)
for(int j=0;j<=width-1;j++)
SobelPixel(clone,j,i,FilterArray(myBitmap,j,i));
return clone;
}
public Bitmap Filter_Prewitt(Bitmap myBitmap) //Prewitt滤波
{
int width=myBitmap.Width;
int height=myBitmap.Height;
Bitmap clone=new Bitmap(width,height);
for(int i=0;i<=height-1;i++)
for(int j=0;j<=width-1;j++)
PrewittPixel(clone,j,i,FilterArray(myBitmap,j,i));
return clone;
}
public Bitmap Filter_Kirsch(Bitmap myBitmap) //Kirsch滤波
{
int width=myBitmap.Width;
int height=myBitmap.Height;
Bitmap clone=new Bitmap(width,height);
for(int i=0;i<=height-1;i++)
for(int j=0;j<=width-1;j++)
KirschPixel(clone,j,i,FilterArray(myBitmap,j,i));
return clone;
}
public Bitmap Filter_Laplacian(Bitmap myBitmap) //Laplacian滤波
{
int width=myBitmap.Width;
int height=myBitmap.Height;
Bitmap clone=new Bitmap(width,height);
for(int i=0;i<=height-1;i++)
for(int j=0;j<=width-1;j++)
LaplacianPixel(clone,j,i,FilterArray(myBitmap,j,i));
return clone;
}
private double Log(float x,float y) //Log算子
{
double re=(-1)/Math.PI;
re=re/(sigma*sigma*sigma*sigma);
double temp=(x*x+y*y)/(2*sigma*sigma);
re=re*(1-temp);
re=re*Math.Exp(temp);
return re;
}
private void BubbleUp(int[] array, int count) //冒泡排序
{
for(int ii=0; ii<count; ii++)
{
for(int jj=count-1; jj>ii; jj=jj-1)
if(array[jj-1]>array[jj])
{
int tmp = array[jj-1];
array[jj-1] = array[jj];
array[jj] = tmp;
}
}
}
private int Alpha(int alpha,int[] array) //Alpha修饰
{
int sum=0;
for(int i=alpha;i<array.Length-alpha;i++)
sum+=array[i];
sum/=array.Length-2*alpha;
return sum;
}
private double[][] MatchArray(int i,int j,double[][] array)//匹配矩阵
{
double[][] re=new double[17][];
for(int ii=0;ii<=16;ii++)
{
re[ii]=new double[17];
re[ii][0]=array[i-8][j-8+ii];
re[ii][1]=array[i-7][j-8+ii];
re[ii][2]=array[i-6][j-8+ii];
re[ii][3]=array[i-5][j-8+ii];
re[ii][4]=array[i-4][j-8+ii];
re[ii][5]=array[i-3][j-8+ii];
re[ii][6]=array[i-2][j-8+ii];
re[ii][7]=array[i-1][j-8+ii];
re[ii][8]=array[i][j-8+ii];
re[ii][9]=array[i+1][j-8+ii];
re[ii][10]=array[i+2][j-8+ii];
re[ii][11]=array[i+3][j-8+ii];
re[ii][12]=array[i+4][j-8+ii];
re[ii][13]=array[i+5][j-8+ii];
re[ii][14]=array[i+6][j-8+ii];
re[ii][15]=array[i+7][j-8+ii];
re[ii][16]=array[i+8][j-8+ii];
}
return re;
}
private double[] Match(double[][] array1,double[][] array2)//匹配算法
{
double[][] re1=(double[][] )array1.Clone();
double[][] re2=(double[][] )array2.Clone();
int height1=array1.Length;
int width1=array1[0].Length;
int height2=array2.Length;
int width2=array2[0].Length;
double[] re=new double[5];
re[0]=9999;
re[1]=0;
re[2]=0;
double[][] beMatched=MatchArray(200,200,array1);
for(int i=8;i<height2-8;i++)
for(int j=8;j<width2-8;j++)
{
double[][] match=MatchArray(i,j,array2);
double r=GetError(beMatched,match);
if(r<re[0]) {re[0]=r;re[1]=i;re[2]=j;}
}
re[3]=200;
re[4]=200;
return re;
}
private double GetError(double[][] array1,double[][] array2) //取均差
{
double re=0;
for(int i=0;i<=array1.Length-1;i++)
for(int j=0;j<=array1[0].Length-1;j++)
{
re+=Math.Pow((array1[i][j]-array2[i][j]),2);
}
re=re/(array1.Length*array1[0].Length);
re=Math.Sqrt(re);
return re;
}
private int Min(int x1,int x2)//去小值
{
int re;
if(x1<x2) re=x1;
else re=x2;
return re;
}
public double BlearBitmapError(Bitmap myBitmap1,Bitmap myBitmap2)//算方差
{
double[][] array1=GetArray(myBitmap1);
double[][] array2=GetArray(myBitmap2);
double[] re=Match(array1,array2);
int h1=(int)re[3];
int w1=(int)re[4];
int h2=(int)re[1];
int w2=(int)re[2];
int h=Min(h1,h2)+Min(array1.Length-h1,array2.Length-h2);
//int h=5;
int h01=h1-Min(h1,h2);
int h02=h2-Min(h1,h2);
//int w=5;
int w=Min(w1,w2)+Min(array1[0].Length-w1,array2[0].Length-w2);
int w01=w1-Min(w1,w2);
int w02=w2-Min(w1,w2);
RectangleF rect1=new RectangleF(w01+4,h01+4,w-4,h-4);
RectangleF rect2=new RectangleF(w02+4,h02+4,w-4,h-4);
Bitmap a1=myBitmap1.Clone(rect1,myBitmap1.PixelFormat);
Bitmap a2=myBitmap2.Clone(rect2,myBitmap2.PixelFormat);
double rre=GetError2(a1,a2);
return rre;
}
public double GetError2(Bitmap myBitmap1,Bitmap myBitmap2)
{
double[][] array1=GetArray(myBitmap1);
double[][] array2=GetArray(myBitmap2);
double re=0;
for(int i=0;i<=array1.Length-1;i++)
for(int j=0;j<=array1[0].Length-1;j++)
{
re+=Math.Pow((array1[i][j]-array2[i][j]),2);
}
re=re/(array1.Length*array1[0].Length);
return re;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -