2017-11-30 5 views
1

ppmからrgb値を正常に読み取り、ppmに正常に書き込む関数があります。私がしようとしているのは、denoiseImageと呼ばれる関数で、フレームウィンドウのサイズnをnでnを奇数とする平均フィルタリングを使用して、ppmからrgbの値を変更します。私の意図は、それを囲むn×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        
} 

フレームが適合するかどうかを確認する方法は?

+0

この質問に疑問はありません... –

答えて

1

これは素晴らしい質問です。幸いなことに、画像処理のコミュニティで知られています。 エッジは、2Dフィルタリングに関しては、常に異なって扱われます。

これを見る方法の1つは、2Dでスペースを拡張し、中間からの外挿値でエッジを塗りつぶすことです。

例えば、http://www.librow.com/articles/article-1を調べて、メディアフィルタを検索することができます。

正しい方向に進んでいるので、すぐに解決策を見つけることができます。

関連する問題