Pixel operations on a very large image in c#
-
Hello folks, I have a rather large image, whose size is around 6000 x 5000 pixels... I would like to perfom image normalization and other such operations...which include some mathematical calculation for each pixel.. But when I do this on my image, it take about 20 mins to finish... How to speed it up...?? Here is the normalization code which I am running.. public void NormalizeImage(ref Bitmap objBitmap, int iMaxColor) { DateTime dt = System.DateTime.Now; System.Diagnostics.Trace.WriteLine(" Normalizing Image: " + dt); int iX = objBitmap.Size.Width; int iY = objBitmap.Size.Height; int iMaxINTColor = 16777215; try { for (int i = 0; i < iX; i++) { for (int j = 0; j < iY; j++) { int iPrevColorVal = objBitmap.GetPixel(i, j).ToArgb(); //if (iPrevColorVal < 0) // iPrevColorVal = iPrevColorVal * -1; int iNormalizedVal = System.Convert.ToInt32(Math.Pow((iPrevColorVal / iMaxColor), 1)) * iMaxINTColor; objBitmap.SetPixel(i, j, Color.FromArgb(iNormalizedVal)); } } } catch { System.Diagnostics.Debug.Assert(false); } DateTime dt2 = System.DateTime.Now; System.Diagnostics.Trace.WriteLine("Done Normalizing Image:" + dt2); }
-
Hello folks, I have a rather large image, whose size is around 6000 x 5000 pixels... I would like to perfom image normalization and other such operations...which include some mathematical calculation for each pixel.. But when I do this on my image, it take about 20 mins to finish... How to speed it up...?? Here is the normalization code which I am running.. public void NormalizeImage(ref Bitmap objBitmap, int iMaxColor) { DateTime dt = System.DateTime.Now; System.Diagnostics.Trace.WriteLine(" Normalizing Image: " + dt); int iX = objBitmap.Size.Width; int iY = objBitmap.Size.Height; int iMaxINTColor = 16777215; try { for (int i = 0; i < iX; i++) { for (int j = 0; j < iY; j++) { int iPrevColorVal = objBitmap.GetPixel(i, j).ToArgb(); //if (iPrevColorVal < 0) // iPrevColorVal = iPrevColorVal * -1; int iNormalizedVal = System.Convert.ToInt32(Math.Pow((iPrevColorVal / iMaxColor), 1)) * iMaxINTColor; objBitmap.SetPixel(i, j, Color.FromArgb(iNormalizedVal)); } } } catch { System.Diagnostics.Debug.Assert(false); } DateTime dt2 = System.DateTime.Now; System.Diagnostics.Trace.WriteLine("Done Normalizing Image:" + dt2); }
Hi Safee, The problem is the call to SetPixel. It's horribly slow. You can use the free .NET library FastBitmap[^] to speed up pixel operations, which will result in your algorithm being much faster.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: I Plead (audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Hello folks, I have a rather large image, whose size is around 6000 x 5000 pixels... I would like to perfom image normalization and other such operations...which include some mathematical calculation for each pixel.. But when I do this on my image, it take about 20 mins to finish... How to speed it up...?? Here is the normalization code which I am running.. public void NormalizeImage(ref Bitmap objBitmap, int iMaxColor) { DateTime dt = System.DateTime.Now; System.Diagnostics.Trace.WriteLine(" Normalizing Image: " + dt); int iX = objBitmap.Size.Width; int iY = objBitmap.Size.Height; int iMaxINTColor = 16777215; try { for (int i = 0; i < iX; i++) { for (int j = 0; j < iY; j++) { int iPrevColorVal = objBitmap.GetPixel(i, j).ToArgb(); //if (iPrevColorVal < 0) // iPrevColorVal = iPrevColorVal * -1; int iNormalizedVal = System.Convert.ToInt32(Math.Pow((iPrevColorVal / iMaxColor), 1)) * iMaxINTColor; objBitmap.SetPixel(i, j, Color.FromArgb(iNormalizedVal)); } } } catch { System.Diagnostics.Debug.Assert(false); } DateTime dt2 = System.DateTime.Now; System.Diagnostics.Trace.WriteLine("Done Normalizing Image:" + dt2); }
-
Hello folks, I have a rather large image, whose size is around 6000 x 5000 pixels... I would like to perfom image normalization and other such operations...which include some mathematical calculation for each pixel.. But when I do this on my image, it take about 20 mins to finish... How to speed it up...?? Here is the normalization code which I am running.. public void NormalizeImage(ref Bitmap objBitmap, int iMaxColor) { DateTime dt = System.DateTime.Now; System.Diagnostics.Trace.WriteLine(" Normalizing Image: " + dt); int iX = objBitmap.Size.Width; int iY = objBitmap.Size.Height; int iMaxINTColor = 16777215; try { for (int i = 0; i < iX; i++) { for (int j = 0; j < iY; j++) { int iPrevColorVal = objBitmap.GetPixel(i, j).ToArgb(); //if (iPrevColorVal < 0) // iPrevColorVal = iPrevColorVal * -1; int iNormalizedVal = System.Convert.ToInt32(Math.Pow((iPrevColorVal / iMaxColor), 1)) * iMaxINTColor; objBitmap.SetPixel(i, j, Color.FromArgb(iNormalizedVal)); } } } catch { System.Diagnostics.Debug.Assert(false); } DateTime dt2 = System.DateTime.Now; System.Diagnostics.Trace.WriteLine("Done Normalizing Image:" + dt2); }
Hi! There's an excellent series of articles here dealing with the topic. Starting here: http://www.codeproject.com/cs/media/csharpgraphicfilters11.asp[^]
Regards, mav -- Black holes are the places where God divided by 0...