I did a performance analysis on your method and it is indeed the GetPixel
method that is slowing things done. I did a Google search and apparently a lot of people are dealing with this. Here's an interesting question on StackOverflow that has a possible solution: C# - Faster Alternatives to SetPixel and GetPixel for Bitmaps for Windows Forms App C# - Faster Alternatives to SetPixel and GetPixel for Bitmaps for Windows Forms App.
I did a performance analysis on your method and it is indeed the GetPixel
method that is slowing things done. I did a Google search and apparently a lot of people are dealing with this. Here's an interesting question on StackOverflow that has a possible solution: C# - Faster Alternatives to SetPixel and GetPixel for Bitmaps for Windows Forms App.
I did a performance analysis on your method and it is indeed the GetPixel
method that is slowing things done. I did a Google search and apparently a lot of people are dealing with this. Here's an interesting question on StackOverflow that has a possible solution: C# - Faster Alternatives to SetPixel and GetPixel for Bitmaps for Windows Forms App.
I did a performance analysis on your method and it is indeed the GetPixel
method that is slowing things done. I did a Google search and apparently a lot of people are dealing with this. Here's an interesting question on StackOverflow that has a possible solution: C# - Faster Alternatives to SetPixel and GetPixel for Bitmaps for Windows Forms App .
Seems that an important part of this approach is the LockBits()
method call. More reading about it on MSDN: Bitmap.LockBits Method (Rectangle, ImageLockMode, PixelFormat)
I did a performance analysis on your method and it is indeed the GetPixel
method that is slowing things done. I did a Google search and apparently a lot of people are dealing with this. Here's an interesting question on StackOverflow that has a possible solution: C# - Faster Alternatives to SetPixel and GetPixel for Bitmaps for Windows Forms App .
Seems that an important part of this approach is the LockBits()
method call. More reading about it on MSDN: Bitmap.LockBits Method (Rectangle, ImageLockMode, PixelFormat)
The ́var ́ keyword:
From the C# Programming Guide:
The var keyword can also be useful when the specific type of the variable is tedious to type on the keyboard, or is obvious, or does not add to the readability of the code.
So lines like:
int threshold = 7;
Bitmap bitmap0 = (Bitmap)imag.Clone();
would become:
var threshold = 7;
var bitmap0 = (Bitmap)imag.Clone();
Variable names:
Names like bitmap
, bitmap0
, Sqdistance
don't have a useful meaning, not for you, nor for others reading/reviewing your code. Use meaningful names for your variables, better for readability and maintainability.
Curly braces:
Although you're not writing "wrong" code by omitting the braces on the outer loops, it seriously decreases readability. Following is only 2 lines longer and is much easier to understand:
for (row = 0; row < rows; row++)
{
for (col = 0; col < cols; col++)
{
//Code...
}
}
Other:
if (count >= threshold)
outmap.SetPixel(col, row, Color.FromArgb(255, 255, 255));
else
outmap.SetPixel(col, row, Color.FromArgb(0, 0, 0));
can be rewritten to:
var colorBit = count >= treshold ? 255 : 0;
outmap.SetPixel(col, row, Color.FromArgb(colorBit, colorBit, colorBit));
Following code contains too much braces in my opinion:
private int SQ(int a) { return ((a) * (a)); }
Rewrite it as follows:
private int Square(int a)
{
return a * a;
}
or a different approach:
private int Square(int a)
{
return (int)Math.Pow(a, 2);
}
Also for those two last methods, don't make them upper case. Method names in .NET are Pascal case (Capitalization Conventions).
Performance:
Regarding the performance. I didn't take a deep dive in the code or test it, but is there a reason why you perform the loops twice? Isn't there a possibility to execute all the code inse the double for-loop?