【发布时间】:2018-08-27 10:10:12
【问题描述】:
见:Image convolution in spatial domain
下面的代码实现了空间域的线性卷积。
public static double[,] ConvolutionSpatial(double[,] paddedImage, double[,] mask, double offset)
{
double min = 0.0;
double max = 1.0;
double factor = GetFactor(mask);
int paddedImageWidth = paddedImage.GetLength(0);
int paddedImageHeight = paddedImage.GetLength(1);
int maskWidth = mask.GetLength(0);
int maskHeight = mask.GetLength(1);
int imageWidth = paddedImageWidth - maskWidth;
int imageHeight = paddedImageHeight - maskHeight;
double[,] convolve = new double[imageWidth, imageHeight];
for (int y = 0; y < imageHeight; y++)
{
for (int x = 0; x < imageWidth; x++)
{
double sum = Sum(paddedImage, mask, x, y);
convolve[x, y] = Math.Min(Math.Max((sum / factor) + offset, min), max);
string str = string.Empty;
}
}
return convolve;
}
public static double Sum(double[,] paddedImage1, double[,] mask1, int startX, int startY)
{
double sum = 0;
int maskWidth = mask1.GetLength(0);
int maskHeight = mask1.GetLength(1);
for (int y = startY; y < (startY + maskHeight); y++)
{
for (int x = startX; x < (startX + maskWidth); x++)
{
double img = paddedImage1[x, y];
double msk = mask1[maskWidth - x + startX - 1, maskHeight - y + startY - 1];
sum = sum + (img * msk);
}
}
return sum;
}
public static double GetFactor(double[,] kernel)
{
double sum = 0.0;
int width = kernel.GetLength(0);
int height = kernel.GetLength(1);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
sum += kernel[x, y];
}
}
return (sum == 0) ? 1 : sum;
}
其性能如下:
image-size kernel-size time-elapsed
------------------------------------------
100x100 3x3 13ms
512x512 3x3 291ms
1018x1280 3x3 1687ms
100x100 100x100 4983ms
512x512 512x512 35624394ms
1018x1280 1018x1280 [practically unusable]
我有两个问题:
- 看起来像下降表演吗?
- 如果不是,我该如何提高性能?
【问题讨论】:
-
您将需要使用傅立叶变换,that answer 可能会有所帮助。哦,这是你的问题,所以,你是在正确的方式。
标签: c# performance optimization