索引数组自适应中位数的边界外

时间:2014-01-18 05:12:00

标签: 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 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;

    }


}

}

任何帮助将不胜感激,谢谢。

0 个答案:

没有答案