坚持在框架 windows 上实施边界检查以进行均值过滤
Stuck implementing boundary checks on frame windows for mean filtering
我有一个函数可以成功从 ppm 读取 rgb 值,还有一个函数可以成功写入 ppm。我正在尝试的是一个名为 denoiseImage 的函数,它使用帧 window 大小 n 乘以 n 的均值滤波从 ppm 更改 rgb 值,其中 n 是奇数。我的目的是遍历每个像素,将其用作围绕它的 window n x n 的中心点。然后,我取每种颜色 (r,g,b) 的平均值,除以 window 中的像素数,并将这些新值分配给 window 中每个像素的 rgb。但是,我无法对框架不完全适合像素的情况进行检查(例如,框架中心点是右上角的像素,3x3 的 window 将转到不存在的点。 ) 当它不完全适合时,我打算使用适合的可用像素并取这些数字的平均值。到目前为止,我的代码只适用于框架完全适合的情况。我的函数:
RGB *denoiseImage(int width, int height, const RGB *image, int n)
{
int firstPos, lastPos, i = 0, j = 0, k, numofPix;
int sumR=0,sumG=0,sumB=0;
numofPix = (width * height);
RGB *pixels = malloc(numofPix * sizeof(RGB));
if (n == 1) //Case where the window size is 1 and therefore the image does not get changed.
{
return pixels;
}
for (j=0;j < numofPix;j++)
{
firstPos = (j - width) - ((n - 1)/2);
lastPos = (j + width) + ((n - 1)/2);
//Need to check boundary cases to prevent segmentation fault
for (k=firstPos;k<=lastPos;k++) //Seg fault. Unable to shrink frame to compensate for cases where the frame does not fit.
{
sumR+=image[k].r;
sumG+=image[k].g;
sumB+=image[k].b;
i++;
if (i = n) //Used to skip elements not in frame
{
j += (width-n);
i = 0;
}
}
sumR = sumR/(n*n); //Calculating mean values
sumG = sumG/(n*n);
sumB = sumB/(n*n);
for (k=firstPos;k<=lastPos;k++) //Assigning the RGB values with the new mean values.
{
pixels[k].r=sumR;
pixels[k].g=sumG;
pixels[k].b=sumB;
printf("%d %d %d ",pixels[k].r, pixels[k].g, pixels[k].b);
}
}
return pixels;
}
int main()
{
RGB *RGBValues;
int width, height, max;
int j = 0,testemp=3; //test temp is a sample frame size
char *testfile = "test.ppm";
char *testfile2 = "makeme.ppm";
RGBValues = readPPM(testfile, &width, &height, &max); //Function reads values from a ppm file correctly
RGBValues = denoiseImage(width,height, RGBValues, testemp,testing);
writePPM(testfile2,width,height,max,RGBValues); //Function writes values to a ppm file correctly
}
我将如何实施检查框架是否适合的方法?
这是一个很好的问题,幸运的是在图像处理社区中广为人知。
当涉及到 2D 过滤时,边缘总是被区别对待。
查看它的一种方法是在 2D 中扩展 space 并用从中间推断的值填充边缘。
例如,您可以查看 http://www.librow.com/articles/article-1 并搜索媒体过滤器。
我相信您很快就会找到解决方案,因为您的方向是正确的。
我有一个函数可以成功从 ppm 读取 rgb 值,还有一个函数可以成功写入 ppm。我正在尝试的是一个名为 denoiseImage 的函数,它使用帧 window 大小 n 乘以 n 的均值滤波从 ppm 更改 rgb 值,其中 n 是奇数。我的目的是遍历每个像素,将其用作围绕它的 window n x n 的中心点。然后,我取每种颜色 (r,g,b) 的平均值,除以 window 中的像素数,并将这些新值分配给 window 中每个像素的 rgb。但是,我无法对框架不完全适合像素的情况进行检查(例如,框架中心点是右上角的像素,3x3 的 window 将转到不存在的点。 ) 当它不完全适合时,我打算使用适合的可用像素并取这些数字的平均值。到目前为止,我的代码只适用于框架完全适合的情况。我的函数:
RGB *denoiseImage(int width, int height, const RGB *image, int n)
{
int firstPos, lastPos, i = 0, j = 0, k, numofPix;
int sumR=0,sumG=0,sumB=0;
numofPix = (width * height);
RGB *pixels = malloc(numofPix * sizeof(RGB));
if (n == 1) //Case where the window size is 1 and therefore the image does not get changed.
{
return pixels;
}
for (j=0;j < numofPix;j++)
{
firstPos = (j - width) - ((n - 1)/2);
lastPos = (j + width) + ((n - 1)/2);
//Need to check boundary cases to prevent segmentation fault
for (k=firstPos;k<=lastPos;k++) //Seg fault. Unable to shrink frame to compensate for cases where the frame does not fit.
{
sumR+=image[k].r;
sumG+=image[k].g;
sumB+=image[k].b;
i++;
if (i = n) //Used to skip elements not in frame
{
j += (width-n);
i = 0;
}
}
sumR = sumR/(n*n); //Calculating mean values
sumG = sumG/(n*n);
sumB = sumB/(n*n);
for (k=firstPos;k<=lastPos;k++) //Assigning the RGB values with the new mean values.
{
pixels[k].r=sumR;
pixels[k].g=sumG;
pixels[k].b=sumB;
printf("%d %d %d ",pixels[k].r, pixels[k].g, pixels[k].b);
}
}
return pixels;
}
int main()
{
RGB *RGBValues;
int width, height, max;
int j = 0,testemp=3; //test temp is a sample frame size
char *testfile = "test.ppm";
char *testfile2 = "makeme.ppm";
RGBValues = readPPM(testfile, &width, &height, &max); //Function reads values from a ppm file correctly
RGBValues = denoiseImage(width,height, RGBValues, testemp,testing);
writePPM(testfile2,width,height,max,RGBValues); //Function writes values to a ppm file correctly
}
我将如何实施检查框架是否适合的方法?
这是一个很好的问题,幸运的是在图像处理社区中广为人知。 当涉及到 2D 过滤时,边缘总是被区别对待。
查看它的一种方法是在 2D 中扩展 space 并用从中间推断的值填充边缘。
例如,您可以查看 http://www.librow.com/articles/article-1 并搜索媒体过滤器。
我相信您很快就会找到解决方案,因为您的方向是正确的。