Note that ic is the input column and oc is the output column Convert the pixel to it's luminance using the formula: OutputData = output.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly,īyte* bmpPtr = (byte*)(), Make sure the palette is a grayscale palette and not some otherĬase PixelFormat.Format24bppRgb: bytesPerPixel = 3 break Ĭase PixelFormat.Format32bppArgb: bytesPerPixel = 4 break Ĭase PixelFormat.Format32bppRgb: bytesPerPixel = 4 break ĭefault: throw new InvalidOperationException("Image format not supported") īmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, If (pfIn = PixelFormat.Format8bppIndexed) No need to convert formats if already in 8 bit Palette.Entries = Color.FromArgb(255, i, i, i) Output = new Bitmap(w, h, PixelFormat.Format8bppIndexed) Ĭolor tmp = Color.FromArgb(255, i, i, i) R, ic, oc, bmpStride, outputStride, bytesPerPixel Public static Bitmap ColorToGrayscale(Bitmap bmp) / Converts a bitmap into an 8-bit grayscale bitmap These times were taken from the following code:īitmap bmp = new Bitmap("d:/tmp/screenshot.png") Ĭonsole.WriteLine(sw.Elapsed.TotalMilliseconds) Please let me know if you have any questions.Ĭonverting a 2560x1024 24-bit color image to 2560x1024 8-bit grayscale image on a 2.0GHz Opteron, I get the following consistent run times: The key to the performance here is the use of unsafe code and directly accessing the pointers to the memory used by the image. This method will handle 24 bit or 32 bit images (24 bit w/ an 8bit alpha channel). I have tried to document my code thoroughly. It's not trivial, but it's not rocket science either. I have been using this for performance critical machine vision applications for some time now.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |