【问题标题】:Stuck implementing boundary checks on frame windows for mean filtering卡在框架窗口上实施边界检查以进行均值滤波
【发布时间】: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                             
}

我将如何实现一种方法来检查框架是否适合?

【问题讨论】:

  • 本题没有问题...

标签: c algorithm ppm


【解决方案1】:

这是一个很好的问题,幸运的是在图像处理社区中广为人知。 在进行 2D 过滤时,边缘的处理方式总是不同的。

查看它的一种方法是在 2D 中扩展空间并用从中间推断的值填充边缘。

例如,您可以查看http://www.librow.com/articles/article-1 并搜索媒体过滤器。

我相信你很快就会找到解决方案,因为你正朝着正确的方向前进。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-02
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 2015-09-02
    • 2010-12-26
    相关资源
    最近更新 更多