Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.
Here's some code for building an 8-bit grayscale bitmap:. Gray8, null ;. WritePixels new Int32Rect 0, 0, , , pixelData, , 0 ;. What you've descibred there is an 8-bit grayscale image! So I don't think what you've written there can be quite right. That's what you'd expect for a bit grayscale image of course. So, I'm going to make the following assumption: when you said that each pixel has a gray scale value in the range of 0 to , you meant to say that each pixel has two bytes, each of which is in the range 0 to , but each pair constitutes a single bit value in the range of 0 to This is how bit grayscale images usually work.
I still don't know how your bit image works. Now we know the source image format, this is pretty simple. And has absolutely nothing to do with WPF by the way. This is just perfectly ordinary image processing, and it'd be the same on any platform.
Just throw away every other byte. Make a byte[] array with just , elements, and copy in alternate bytes from the original bit image. You will now have an 8-bit grayscale image. The remaining question is: which bytes do you keep and which do you throw away? This is why I asked if your image data is big-endian or little-endian.
If it's big-endian, then you want to keep even-numbered bytes and throw away odd-numbered bytes. If it's little-endian, do the opposite.
If you don't know whether it's big-endian or little-endian, try both and see which one looks right. If you pick the wrong one you will most likely see something looking like random noise, or, depending on the nature of the source image, a bizarrely noisy and weird but vaguely recognizable version of the original image.
I'm not going to insult your intelligence by showing you how to write a loop that copies alternate bytes out of one array into another array half the size Once you've got your , element byte array, you can use the code I already showed you in an earlier post to turn this byte[] into an 8-bit grayscale bitmap. The basic idea is pretty straightforward. The simplest approach is to take the top 8 bits of each 12bit pixel value and use that A slightly more sophisticated approach is to read all the values in the image to find the lowest and highest values - let's say no pixel is darker than , and no pixel is brighter than For each pixel, you then subtract the lowest value , divide by the range , and then multiply by You will now have pixel values ranging from 0 to , which is the range of an 8-bit grayscale.
There are more sophisticated approaches still - you can do non-linear mapping, such as exponential mapping to increase the contrast for dark areas while decreasing it in light areas or vice versa.
Or even more sophisticated stuff like changing the mapping for different areas of the image. But that's probably a bit too much for now. To start with, I'd just try the simple approach where you take the top 8 bits of the 12 bit value and see where that gets you. A better solution would be to have WPF do the heavy lifting and grayscale the image for us.
This can be done rather easily by using the FormatConvertedBitmap class. By supplying a target PixelFormat of PixelFormats. Gray8 we easily get a grayscale version of our image. Thus, we end up with the following Image control:. We can now use the AutoDisableImage as a drop-in replacement for the Image controls in our buttons:.
0コメント