锐化后图像变暗

时间:2017-04-13 09:23:56

标签: c# image-processing bitmap

锐化的图像变暗。

为什么你认为是这种情况?

在哪里寻找问题?

enter image description here

相关源代码

  

锐化代码

    public static Bitmap FftSharpen(Bitmap image, double[,] mask)
    {
        if (image.PixelFormat == PixelFormat.Format8bppIndexed)
        {
            Bitmap imageClone = (Bitmap)image.Clone();
            double[,] maskClone = (double[,])mask.Clone();

            Complex[,] cPaddedImage = ImageDataConverter.ToComplex(imageClone);
            Complex[,] cPaddedMask = ImageDataConverter.ToComplex(maskClone);
            Complex[,] cConvolved = Convolution.Convolve(cPaddedImage, cPaddedMask);

            return ImageDataConverter.ToBitmap(cConvolved);
        }
        else
        {
            throw new Exception("not a grascale");
        }
    }
  

WinForms代码

    public SharpeningFilterForm()
    {
        InitializeComponent();

        //Obtain image and kernel
        Bitmap inputImage = Grayscale.ToGrayscale(Bitmap.FromFile(lenaPath) as Bitmap);
        double[,] numericalKernel = new double[,]  { 
                                                    { -1, -1, -1, }, 
                                                    {  -1,  9, -1, }, 
                                                    { -1, -1, -1, }, 
                                                };

        //Padding operation
        Bitmap inputImageCopy = (Bitmap)inputImage.Clone();
        int maxWidth = (int)Math.Max(inputImageCopy.Width, numericalKernel.GetLength(0));
        int maxHeight = (int)Math.Max(inputImageCopy.Height, numericalKernel.GetLength(1));

        Bitmap paddedInputImage = ImagePadder.Pad(inputImageCopy, maxWidth, maxHeight);
        double [,]paddedNumericalKernel = ImagePadder.Pad(numericalKernel, maxWidth, maxHeight);

        //Sharpening
        Bitmap sharpened = SharpenFilter.FftSharpen(paddedInputImage, paddedNumericalKernel);

        //Displaying
        inputImagePictureBox.Image = inputImage;
        maskPictureBox.Image = ImageDataConverter.ToBitmap(numericalKernel);
        paddedImagePictureBox.Image = paddedInputImage;
        paddedMaskPictureBox.Image = ImageDataConverter.ToBitmap(paddedNumericalKernel);
        filteredPictureBox.Image = sharpened as Image;
    }
  

Convolution.cs

public static partial class Convolution
{
    public static Complex[,] Convolve(Complex[,] image1, Complex[,] mask1)
    {
        Complex[,] image = (Complex[,])image1.Clone();
        Complex[,] mask = (Complex[,])mask1.Clone();
        Complex[,] convolve = null;

        int imageWidth = image.GetLength(0);
        int imageHeight = image.GetLength(1);

        int maskWidth = mask.GetLength(0);
        int maskeHeight = mask.GetLength(1);

        if (imageWidth == maskWidth && imageHeight == maskeHeight)
        {
            Complex[,] ftForImage = FourierTransform.ForwardFFT(image);
            Complex[,] ftForMask = FourierTransform.ForwardFFT(mask);

            Complex[,] fftImage = ftForImage;
            Complex[,] fftKernel = ftForMask;

            Complex[,] fftConvolved = new Complex[imageWidth, imageHeight];

            for (int j = 0; j < imageHeight; j++)
            {
                for (int i = 0; i < imageWidth; i++)
                {
                    fftConvolved[i, j] = fftImage[i, j] * fftKernel[i, j];
                }
            }

            Complex[,] ftForConv = FourierTransform.InverseFFT(fftConvolved);

            convolve = ftForConv;

            Rescale(convolve);

            convolve = FourierShifter.ShiftFft(convolve);
        }
        else
        {
            throw new Exception("padding needed");
        }

        return convolve;
    }

    private static void Rescale(Complex[,] convolve)
    {
        int imageWidth = convolve.GetLength(0);
        int imageHeight = convolve.GetLength(1);

        double maxAmp = 0.0;

        for (int j = 0; j < imageHeight; j++)
        {
            for (int i = 0; i < imageWidth; i++)
            {
                maxAmp = Math.Max(maxAmp, convolve[i, j].Magnitude);
            }
        }

        double scale = 255.0 / maxAmp;

        for (int j = 0; j < imageHeight; j++)
        {
            for (int i = 0; i < imageWidth; i++)
            {
                convolve[i, j] = new Complex(convolve[i, j].Real * scale, convolve[i, j].Imaginary * scale);
                maxAmp = Math.Max(maxAmp, convolve[i, j].Magnitude);
            }
        }
    }

0 个答案:

没有答案