整数颜色值大于255

时间:2018-08-04 08:56:46

标签: c# image-processing bitmap filtering

假设,我正在按以下方式处理过滤器:

    public static int[,] Apply(int[,] image, double rH, double rL, double Sigma, double Slope)
    {
        Complex[,] cImage = FourierShifter.ShiftFft(FourierTransform.ForwardFFT(ImageData2d.ToComplex2d(image)));

        int Width = cImage.GetLength(0);
        int Height = cImage.GetLength(1);

        double[,] dKernel2d = Gaussian.GetHighPassKernel(Width, Sigma, Slope);
        Complex[,] cKernel2d = FourierShifter.ShiftFft(FourierTransform.ForwardFFT(ImageData2d.ToComplex2d(dKernel2d)));

        for (int i = 0; i <= dKernel2d.GetLength(0) - 1; i++)
        {
            for (int j = 0; j <= dKernel2d.GetLength(1) - 1; j++)
            {
                double real = (rH - rL) * cKernel2d[i, j].Real + rL;
                double imag = (rH - rL) * cKernel2d[i, j].Imaginary + rL;

                cKernel2d[i, j] = new Complex(real, imag);
            }
        }

        //multiply image and kernel in frequency domain
        Complex[,] cOutput2d = new Complex[Width, Height];

        for (int y = 0; y < Height; y++)
        {
            for (int x = 0; x < Width; x++)
            {
                cOutput2d[x, y] = cImage[x, y] * cKernel2d[x, y];
            }
        }

        Complex[,] cOutput = FourierTransform.InverseFFT(FourierShifter.RemoveFFTShift(cOutput2d));

        return ImageData2d.ToInteger2d(cOutput);
    }

并且,在此过滤器的返回值int[,]中,我观察到某些元素大于255。

enter image description here

因此,我尝试通过两种方式对其进行修复。

  1. 我通过应用以下函数规范输出cOutput

    public static Complex[,] Normalize(Complex[,] fftImage)
    {
        int width = fftImage.GetLength(0);
        int height = fftImage.GetLength(1);
    
        Complex[,] outcome = new Complex[width, height];
        double scale = width * height;
        for (int y = 0; y < width; y++)
        {
            for (int x = 0; x <height; x++)
            {
                Complex c = fftImage[x, y];
    
                outcome[x, y] = new Complex(c.Real / scale,
                                            c.Imaginary / scale);
            }
        }
    
        return outcome;
    } 
    

给出完全黑的输出。

enter image description here

  1. 我在ToInteger2d()-函数中应用了约束,以将值限制为255

    public static Array2d<int> ToInteger(Array2d<Complex> image) 
    {
        int Width = image.Width;
        int Height = image.Height;
    
        Array2d<int> array2d = new Array2d<int>(Width, Height);
    
        for (int j = 0; j <= Height - 1; j++)
        {
            for (int i = 0; i <= Width - 1; i++)
            {
                array2d[i, j] = (int)image[i, j].Magnitude;
    
                if (array2d[i, j] > 255)
                {
                    array2d[i, j] = 255;
                }
            }
        }
    
        return array2d;
    }
    

消除了过滤器的作用:

enter image description here

在这种情况下应如何解决?

0 个答案:

没有答案