Zaggermeister
Well Known Member
Hey guys, got a quick question.
So I'm trying to blur a large image using a mean 3*3 mask. I helped a friend of mine and we managed to blur smaller images (under 1920*1080) in less than 5 seconds average. I tried to do it on my own and used snippets but different methods (the method closest as to which our lecturer wants) but ended up with a 1080 picture taking 35 seconds. I managed to get that down to 20 seconds average (same picture) but our test file is a 17.2MB satellite image which we need to blur. At this stage it is taking me minutes to blur the image and his program managed to blur it in a lot less. I could ask him to help me with my code but he C+Ped it and spiced it up a bit so he himself doesn't really know why it works.
The concept is simple though, the 3*3 matrix moves over a pixel, calculates the average of the squares around the pixel and then applies the average to that pixel.
This is my method, the commented lines were my original but between the original and the new lines the end product doesn't visually change. I'm still quite far from all the functions we need, so at this point it is still spaghetti code, so don't judge
If anyone can think of a way to optimise this a bit, feel free to let me know.
So I'm trying to blur a large image using a mean 3*3 mask. I helped a friend of mine and we managed to blur smaller images (under 1920*1080) in less than 5 seconds average. I tried to do it on my own and used snippets but different methods (the method closest as to which our lecturer wants) but ended up with a 1080 picture taking 35 seconds. I managed to get that down to 20 seconds average (same picture) but our test file is a 17.2MB satellite image which we need to blur. At this stage it is taking me minutes to blur the image and his program managed to blur it in a lot less. I could ask him to help me with my code but he C+Ped it and spiced it up a bit so he himself doesn't really know why it works.
The concept is simple though, the 3*3 matrix moves over a pixel, calculates the average of the squares around the pixel and then applies the average to that pixel.
C#:
public void Smooth(Bitmap bmpInput)
{
Bitmap temp,final;
int r, g, b;
float sumR, sumB, sumG;
Color c;
temp = bmpInput;
for (int i = 0; i <= temp.Width - 3; i++)
for (int j = 0; j <= temp.Height - 3; j++)
{
sumB = 0;
sumG = 0;
sumR = 0;
for (int x = i; x <= i + 2; x++)
for (int y = j; y <= j + 2; y++)
{
c = temp.GetPixel(x, y);
r = c.R;
g = c.G;
b = c.B;
sumR = sumR + r;
sumG = sumG + g;
sumB = sumB + b;
}
int colorR = (int)(sumR / 9);
int colorG = (int)(sumG / 9);
int colorB = (int)(sumB / 9);
//int colorR = (int)Math.Round(sumR / 9, 10);
//int colorG = (int)Math.Round(sumG / 9, 10);
//int colorB = (int)Math.Round(sumB / 9, 10);
temp.SetPixel(i + 1, j + 1, Color.FromArgb(colorR, colorG, colorB));
}
final = temp;
final.Save("C:\\Users\\The Machine\\Desktop\\test.jpg");
}
This is my method, the commented lines were my original but between the original and the new lines the end product doesn't visually change. I'm still quite far from all the functions we need, so at this point it is still spaghetti code, so don't judge
If anyone can think of a way to optimise this a bit, feel free to let me know.