【发布时间】:2017-11-30 00:59:16
【问题描述】:
我有一个函数可以成功地从 ppm 读取 rgb 值,还有一个函数可以成功地写入 ppm。我正在尝试的是一个名为 denoiseImage 的函数,它使用帧窗口大小为 n 乘 n 的均值滤波从 ppm 更改 rgb 值,其中 n 为奇数。我的意图是遍历每个像素,将其用作围绕它的 n x n 窗口的中心点。然后我取每种颜色 (r,g,b) 的平均值并除以窗口中的像素数,并将这些新值分配给窗口中每个像素的 rgb。但是,我无法检查框架不完全适合像素的情况(例如,框架中心点是右上角的像素,3x3 的窗口将转到不存在的点。)当它确实不完全适合,我打算使用适合的可用像素并取这些数字的平均值。到目前为止,我的代码仅适用于框架完全适合的情况。我的功能:
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
}
我将如何实现一种方法来检查框架是否适合?
【问题讨论】:
-
本题没有问题...