Blur and sharpen a bitmap

This is a discussion on Blur and sharpen a bitmap within the C++ Programming forums, part of the General Programming Boards category; Is there a tutorial or an algorithm that describes how you implement a blur or sharpen function for bitmaps in ...

Bubba thanks but what I am trying to do is a solution that is close to standard C++ as much as it can be. I am aware of getCopyOfPixel() and setPixel() are not standard. It feels like I have hit the wall with this one.

Now I get result but the whole image is a mess big time and I still don't understand why. I am sure it is very simple to fix it, but.....

First of all, you keep calling getCopyOfPixel(i, j) or whatever. That's the same pixel you are sampling every time! You need to sample the correct pixel as if the matrix was overlying on top of your pixels. You also need to keep an additional sum variable to store the values from the matrix. So if you matrix was

[0 1 0]
[1 0 1]
[0 1 0]

the value for the new sum of the matrix would be 4. Just a simple line of code is all that takes:

Code:

ksum += Kernel[krow][kcol];

or whatever your variable names are.

Then, after your matrix loop, you set your pixel to (sum / ksum) for each r,g,b component respectively. Hopefully that makes sense to you.

"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

You still are not doing what I was originally saying. Also, why do you have a matrix now that you fill? This is unnecessary.

Also, what are you going to do about the corners of your image? You can't ignore them.

Take this filter for example:

columns numbered 0,1,2
rows numbered 0,1,2

[0 1 0]
[1 1 1]
[0 1 0]

ksum = 5 for this filter. Let's run through pixel filtering pixel at (0,0).

you would make pixel (0,0) correspond to the center of your matrix (i.e. col #1 row #1).

assume row and col are filter loop indices and x and y are image loop indices. You should try going from -1 to 1 on your filter indices rather than 0 to 3, makes life easier. Perform bounds checking before you access pixels that are off the image.

"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Huh, I didn't manage to get this thing to work so I asked two of my friends if they were able to pull it off and neiter of them succeded. I am beginning to wonder if there is something wrong with the complier or the library.

What? If you are referring to my filter I used in my example then you should know it was just for educational purposes. He already knew the filter he was going to use so I just picked arbitrary numbers. He originally asked how to sharpen and blur an image. I believe he means by way of a convolution kernel. Which is way more flexible than "bilinear filtering".

OP:
I'll look at your source at some point and get back to you.

Found your problem. You have the line:

Code:

p = bmp->getCopyOfPixel(x, y);

To retrieve a pixel from the source image but you are using the wrong variable. You need to use:

Code:

p = bitmap.getCopyOfPixel(x, y);

Also make sure to reset rsum, gsum, bsum, ksum all to 0 after you set the pixel in the destination image so they are ready for the next pass. This should at least get you in the right direction.

"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers