Search code examples
c++imagefilterconvolution

Sharpening filter and convolution function almost working, but something is wrong


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;

}

Solution

  • 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.