質問

for the purpose of smoothing an image, I need to summarize all pixels around a given center pixel and then build the average, what's basically my new pixel then. There are like two problems now:

1) What's a good way to summarize them? and

2) How can I best avoid the corner pixels?

This is what I did so far, but I don't think it's any good.

for(i = 0; i < image1->nx; i++)
{
    for(j = 0; j < image1->ny; j++)
    {
        if(i == 0 || j == 0 || i == image1->nx - 1 || j == image1->ny - 1)
        {                                                                  
            image2->image[i][j] = image1->image[i][j];
        }
        else
        {
            int average = 0;
            average += image1->image[i][j];
            average += image1->image[i+1][j];
            average += image1->image[i][j+1];
            average += image1->image[i+1][j+1];
            average += image1->image[i-1][j];
            average += image1->image[i-1][j+1];
            average += image1->image[i-1][j-1];
            average += image1->image[i][j-1];
            average += image1->image[i+1][j-1];
            average /= 9;
            image2->image[i][j] = average;
        }
    }
}

My struct in C is something like this:

struct pgm_image
{
    int nx;             // row number
    int ny;             // cell number
    unsigned char image[N1][N2]; // image information
};
役に立ちましたか?

解決

This looks right. To shorten it a bit, I guess you could use a for loop.

int min = -1;
int max = 1;
int average = 0;
int amount = 0;
for(int k = min; k <= max; k++){
    for(int l = min; l <= max; l++){
        amount++;
        average += image1->image[i+k][j+l];
    }
}
average /= amount;
image2->image[i][j] = average;

It still looks messy, but this way you can alter the smooth "radius" by changing the min and max variables.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top