英文:
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'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;
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论