0

I have a project which needs image thresholding. What's more, I have found some math method such as Otsu's method(https://en.wikipedia.org/wiki/Otsu%27s_method) for this.

I knew that in C# I can use GDI+ to do it by setting each pixel. Although WPF can reference the GDI+ also, I don't want to use it.

One of the reason is WPF using the DirectX to render but not GDI+. DirectX may be the best choice but it seems DirectX can't do this.

The other reason is the original GDI+ will cost a long time in image thresholding. Although someone said it can use some C++ code to make it much faster, I don't want to use the unsafe code such as C++ pointer in WPF.

I am trying to find a way to do it, but I know maybe I can't. If there is a better way, would you please tell me?

Zoe
  • 27,060
  • 21
  • 118
  • 148
102425074
  • 781
  • 1
  • 7
  • 23
  • Take a look at WriteableBitmap. – Clemens May 10 '19 at 15:02
  • You can use memoryStream to convert a bitmap to bytes. https://stackoverflow.com/questions/7350679/convert-a-bitmap-into-a-byte-array Process it however you like, byte byte. Then convert the array back to a picture https://stackoverflow.com/questions/9564174/convert-byte-array-to-image-in-wpf If you need later processing then writeablebitmap will also work fine. I create a 1155 * 805 writeablebitmap in our app which is a hypsometric representation. That takes 26 milliseconds on my machine. Easily fast enough for our purposes and all in c#. I could find that code if it'll help. – Andy May 10 '19 at 15:10
  • @Andy Encoding a bitmap into a MemoryStream won't give you an array of pixel values. That suggestion makes no sense. – Clemens May 10 '19 at 15:13
  • In case you just want to get a black and white image from a color or grayscale bitmap, FormatConvertedBitmap may be an alternative. – Clemens May 10 '19 at 15:15
  • There's an accepted answer converts a bitmap to bytes in the link I posted. – Andy May 10 '19 at 15:16
  • Sure, but these bytes do not represent individual pixel values. – Clemens May 10 '19 at 15:48
  • @Clemens The answer you provided https://stackoverflow.com/questions/20181132/edit-raw-pixel-data-of-writeablebitmap use the unsafe code also. Is there any other way but not use the unsafe code? – 102425074 May 11 '19 at 03:24
  • @Andy As Clemens said, the bytes do not represent individual pixel values and I can't use the math method to deal with it. – 102425074 May 11 '19 at 03:27
  • Take a look at the documentation. WriteableBitmap has WritePixels methods, and BitmapSource has CopyPixels and Create methods that work with byte arrays. Also, as already mentioned, for simple B/W conversion, FormatConvertedBitmap might be an alternative. – Clemens May 11 '19 at 06:02
  • @Clemens Ok, let me try it. Thank you. – 102425074 May 11 '19 at 13:50

0 Answers0