锐化滤波器和卷积函数几乎正常工作,但出现了一些问题。

huangapple go评论71阅读模式
英文:

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;
}
英文:

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&#39;t mind the preserve variable
Image convolve_image(const Image &amp;im, const Image &amp;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&lt;dimch; c++){
		for(int i=0; i&lt;dimw; i++){
			for(int j=0; j&lt;dimh; j++){
				float pixelnew=0;
				for(int a=0; a&lt;sizef; a++){
					for(int b=0; b&lt;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;

}

答案1

得分: 0

在 clamp_image 函数中发现了对函数 shift_pixels 的随机调用,不确定为什么会有这个调用,因为这是我不应该触碰的东西,但最终我很满意。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月9日 22:11:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定