C# 数组自适应中值的索引外部边界

C# 数组自适应中值的索引外部边界,c#,algorithm,median,imagefilter,C#,Algorithm,Median,Imagefilter,我尝试在应用自适应中值滤波器后输出一个新图像,但它仅在最大窗口大小为3*3时有效,但它应适用于所有奇数窗口大小,并且在图像非常小(例如10*10像素)时有效,但是,如果图像是例如440*445像素,则仅当最大窗口大小为3*3时才有效,否则表示索引超出数组边界,以下是我使用计数排序对像素排序的代码: using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace Ima

我尝试在应用自适应中值滤波器后输出一个新图像,但它仅在最大窗口大小为3*3时有效,但它应适用于所有奇数窗口大小,并且在图像非常小(例如10*10像素)时有效,但是,如果图像是例如440*445像素,则仅当最大窗口大小为3*3时才有效,否则表示索引超出数组边界,以下是我使用计数排序对像素排序的代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace ImageFilters
{
    public class Class2
{
    public byte[,] median(byte[,] array, int width, int height, int msize)
   {
       byte[,] marr = new byte[height + 1, width + 1];

       int size = 3;

       for (int i = 0; i <height+1; i++)
       {

               marr[i, 0] = 255;

       }

       for (int j = 0; j < width+1; j++)
       {
           marr[0, j] = 255;
       }

       int n=0;
       int z=0;


   for (int k = 0; k < (height) * width; k++)
   {

          repeat:
       byte[] farr = new byte[size * size];

           int I=0;

       for (int i = n; i < n+size; i++)
       {
           for (int j = z; j < z+size; j++)
           {
               if (j < width && i < height)
               {
                   farr[I] = array[i, j];
                   I++;
               }
               else
               {
                   farr[I] = 0;
                   I++;
               }
           }
       }


       int zxy = farr[(size*size)/2];
       byte[] check = new byte[size * size];
       check=counting(farr,size);
       int median = 0;
       int lennn;
       lennn = check.Length;
       int min = 99999;
       int maxi = -1;
       int a1, a2;

       min = check.Min();
       maxi = check.Max();

       median = check[lennn / 2];
       a1 = median - min;
       a2 = maxi - median;
       int b1, b2;
       b1 = zxy - min;
       b2 = maxi - zxy;


       if (a1 > 0 && a2 > 0)
       {
           if (b1 > 0 && b2 > 0)
           {
               marr[n + 1, z + 1] = Convert.ToByte(zxy);
               z++;
               if (z + (size - 1) > (width + 1))
               {
                   n++;
                   z = 0;
               }
           }
           else
           {
               marr[n + 1, z + 1] = Convert.ToByte(median);
               z++;
               if (z + (size - 1) > (width + 1))
               {
                   n++;
                   z = 0;
               }
           }


       }
       else
       {
           size = size + 2;
           if (size <= msize)
               goto repeat;
           else
           {

               marr[n +1, z +1] = Convert.ToByte(median);
               z++;
               if (size > 3)
                   size = 3;
               if (z + (size - 1) > (width + 1))
               {
                   n++;
                   z = 0;
               }
           }
       }

     }
       return marr;
   }
    public static byte[] counting(byte[] array1D, int siz)
    {


        int max = -10000000;
        byte[] SortedArray = new byte[siz * siz];

        max = array1D.Max();

        byte[] Array2 = new byte[max + 1];
        //for (int i = 0; i < Array2.Length; i++)
        //    Array2[i] = 0;                             // create new array ( Array2) with zeros in every index .
        for (int i = 0; i < (siz*siz); i++)
        {
            Array2[array1D[i]] += 1;                  //take the element in the index(i) of(array1d) AS the index of (Array2) 
            // and increment the element in this index by 1 .
        }
        for (int i = 1; i <= max; i++)
        {
            Array2[i] += Array2[i - 1];             // Count every element in (array1d) and put it in (Array2).
        }
        for (int i = (siz*siz) - 1; i >= 0; i--)
        {
            SortedArray[Array2[array1D[i]] - 1] = array1D[i];   // transfer the element in index (i) of (array1d) to (SortedArray
            Array2[array1D[i]]--;
        }



        return SortedArray;

    }


}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.Linq;
名称空间图像过滤器
{
公共课2
{
公共字节[,]中位数(字节[,]数组,整数宽度,整数高度,整数msize)
{
字节[,]marr=新字节[高度+1,宽度+1];
int size=3;
对于(int i=0;i 0&&a2>0)
{
如果(b1>0&&b2>0)
{
marr[n+1,z+1]=换算成ToByte(zxy);
z++;
如果(z+(尺寸-1)>(宽度+1))
{
n++;
z=0;
}
}
其他的
{
marr[n+1,z+1]=换算为ToByte(中值);
z++;
如果(z+(尺寸-1)>(宽度+1))
{
n++;
z=0;
}
}
}
其他的
{
尺寸=尺寸+2;
if(尺寸3)
尺寸=3;
如果(z+(尺寸-1)>(宽度+1))
{
n++;
z=0;
}
}
}
}
返回marr;
}
公共静态字节[]计数(字节[]数组1d,整数大小)
{
int max=-10000000;
字节[]SortedArray=新字节[siz*siz];
max=array1D.max();
字节[]数组2=新字节[max+1];
//for(int i=0;i
}


非常感谢您的帮助。

您不认为告诉我是哪一行引发了异常会有帮助吗?您在调试器中单步执行了代码吗?是的,它在第19行marr[i,0]=255中引发了异常;这一点都没有意义