0

So I am implementing a convolution function to apply the filters I need for my exercise, my blur filter works correctly, the output image is what i should get.

The sharpening filter gives something kinda ok except it has a lot of abrupt pixels, what is happening?

Here's my output: sharpened by me

Here's what I should get: sharpened by teacher

Here's my messed up code: Convolution function:

//don't mind the preserve variable
Image convolve_image(const Image &im, const Image &filter, bool preserve) {
    assert(filter.c == 1);
    Image ret;
    ret=Image(im.w,im.h,im.c);
    int dimch=im.c;
    int dimw=im.w;
    int dimh=im.h;
    int sizef=filter.w;

    for(int c=0; c<dimch; c++){
        for(int i=0; i<dimw; i++){
            for(int j=0; j<dimh; j++){
                float pixelnew=0;
                for(int a=0; a<sizef; a++){
                    for(int b=0; b<sizef; b++){
                        pixelnew=pixelnew+im.clamped_pixel(i-(sizef/2)+a,j-(sizef/2)+b,c)*filter.clamped_pixel(a,b,0);
                    }
                }
                ret(i,j,c)=pixelnew;
                
            }
        }
    }
    
    return ret;
}

Filter:

Image make_sharpen_filter() {
    // TODO: Implement the filter
    Image ret=Image(3,3,1);
    
    ret(0,0,0)=0;
    ret(0,1,0)=-1;
    ret(0,2,0)=0;
    ret(1,0,0)=-1;
    ret(1,1,0)=5;
    ret(1,2,0)=-1;
    ret(2,0,0)=0;
    ret(2,1,0)=-1;
    ret(2,2,0)=0;
    

    return ret;

}
beginner
  • 11
  • 4
  • What exactly does ``clamp_pixel`` do? You image looks like some pixel values exceed the permitted range. – Christian Halaszovich Mar 09 '23 at 14:47
  • clamped_pixel manages the border of the image, if you give an out of bounds coordinate it gets you the closest one that is correct, it is almost certainly not the issue since it worked with other excercises and it has been written by the teacher – beginner Mar 09 '23 at 14:53
  • Okay. So to me it seems likely that the problem is indeed that some values exceed the permitted range (>255 perhaps?), like I said in my first comment. Could you create a minimal example that we could compile? – Christian Halaszovich Mar 09 '23 at 15:00
  • Thanks for your time, my teacher took this from this repository: [link](https://github.com/holynski/cse576_sp20_hw2) all the files needed for compilation are there – beginner Mar 09 '23 at 15:07
  • The code i'm trying to implement is in src/filter_image.cpp, there is a CMake.txt file also for compiling and generating an exe that runs some tests to check the correctness of the implementation, I got to the paragraph 2.3 where you can find the part related to the sharpening filter – beginner Mar 09 '23 at 15:19

1 Answers1

0

Found a random call to the function shift_pixels in the clamp_image function, not sure why it was there since it was something I shouldn't have touched but i'm happy in the end.

beginner
  • 11
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Ralf Ulrich Mar 16 '23 at 21:21