【发布时间】:2017-01-18 12:30:48
【问题描述】:
我编写了以下代码来开发同态滤波器。
我认为(虽然我不确定)彩色图像的过滤效果很好。
如果是灰度图像,
为什么内核总是绿色的?
此外,滤镜应该可以锐化图像。但是,它没有这样做。
可能出了什么问题?
.
.
源代码:
Here is the Github repository.
public class HomomorphicFilter
{
public HomoMorphicKernel Kernel = null;
public bool IsPadded { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public double RH { get; set; }
public double RL { get; set; }
public double Sigma { get; set; }
public double Slope { get; set; }
public int PaddedWidth { get; set; }
public int PaddedHeight { get; set; }
public Bitmap KernelBitmap
{
get
{
if (IsPadded)
{
return Kernel.PaddedKernelBitmap;
}
else
{
return Kernel.KernelBitmap;
}
}
}
#region private methods
private int[,] Apply8bit(int[,] imageData2d)
{
Complex[,] imageData2dShiftFftCplx = FourierShifter.ShiftFft(FourierTransform.ForwardFFT(ImageDataConverter.ToComplex(imageData2d)));
Complex[,] fftShiftedFiltered = null;
if (IsPadded)
{
fftShiftedFiltered = Tools.Multiply(Kernel.PaddedKernel, imageData2dShiftFftCplx);
}
else
{
fftShiftedFiltered = Tools.Multiply(Kernel.Kernel, imageData2dShiftFftCplx);
}
return ImageDataConverter.ToInteger(FourierTransform.InverseFFT(FourierShifter.RemoveFFTShift(fftShiftedFiltered)));
}
private int[, ,] Apply3d(int[, ,] image3d)
{
int[, ,] filteredImage3d = new int[image3d.GetLength(0), image3d.GetLength(1), image3d.GetLength(2)];
int widtH = image3d.GetLength(1);
int heighT = image3d.GetLength(2);
int[,] imageData2d = new int[widtH, heighT];
for (int dimension = 0; dimension < 3; dimension++)
{
for (int i = 0; i <= widtH - 1; i++)
{
for (int j = 0; j <= heighT - 1; j++)
{
imageData2d[i, j] = image3d[dimension, i, j];
}
}
int[,] filteredImage2d = Apply8bit(imageData2d);
for (int i = 0; i <= widtH - 1; i++)
{
for (int j = 0; j <= heighT - 1; j++)
{
filteredImage3d[dimension, i, j] = filteredImage2d[i, j];
}
}
}
return filteredImage3d;
}
#endregion
public void Compute()
{
if (IsPadded)
{
if (Width >= PaddedWidth || Height >= PaddedHeight)
{
throw new Exception("PaddedWidth or PaddedHeight must be greater than Width or Height.");
}
}
Kernel = new HomoMorphicKernel();
Kernel.Width = Width;
Kernel.Height = Height;
Kernel.RH = RH;
Kernel.RL = RL;
Kernel.Sigma = Sigma;
Kernel.Slope = Slope;
Kernel.PaddedWidth = PaddedWidth;
Kernel.PaddedHeight = PaddedHeight;
Kernel.Compute();
}
public Bitmap Apply8bit(Bitmap image)
{
int[,] image2d = ImageDataConverter.ToInteger(image);
int[,] filtered = Apply8bit(image2d);
return ImageDataConverter.ToBitmap(filtered);
}
public Bitmap Apply32bitColor(Bitmap image)
{
int[, ,] image3d = ImageDataConverter.ToInteger3d_32bit(image);
int[, ,] filtered = Apply3d(image3d);
return ImageDataConverter.ToBitmap3d_32bit(filtered);
}
}
【问题讨论】:
-
您看到绿色内核是因为您仅在
ImageDataConverter类的ToBitmap32bitColor()方法中编写了绿色组件(address[0] = 0;用于红色组件,address[2] = 0;用于蓝色组件)。要查看灰度内核,您应该将image[j, i]值写入每个组件(红色、绿色、蓝色)。此外,FourierShifter类的ShiftFft()方法会在图像高度或宽度奇数的情况下产生IndexOutOfRangeException异常。 -
我注意到您使用的是 Little-endian 计算机架构。颜色字节存储在下一个字节顺序(反向):蓝色、绿色、红色、Alpha。所以
address[0]是蓝色,address[1]是绿色,address[2]是蓝色,address[4]是 Alpha 字节。您可以使用BitConverter.IsLittleEndian Field检查字节顺序。
标签: c# image-processing filter fft convolution