2

Is there an easy way to convert the grayscale Halcon/MVtec Himage object to a c# bitmap? Sample code exists here (mvtec documentation) for a color image:

  HTuple type, width, height;
  HImage patras = new HImage("patras");

  HImage interleaved = patras.InterleaveChannels("argb", "match", 255);
  IntPtr ptr = interleaved.GetImagePointer1(out type, out width, out height);

  Image img = new Bitmap(width/4, height, width,
                         PixelFormat.Format32bppPArgb, ptr);

  pictureBox.Image = img;

But from this sample, it is not clear how I can work with grayscale images.

shelbypereira
  • 2,097
  • 3
  • 27
  • 50

3 Answers3

1

I have researched your problem and at this link, https://multipix.com/supportblog/halcon-bitmap-himage-conversion/ it explains how to create a bitmap object for both RBG channel and single channels which is what you are looking for.

It states:

The creation of a bitmap from a HALCON image can be done through the constructors of the bitmap class. With single channel images this is straight forward by using the pointer from the operator get_image_pointer1 and the dimensions of the image.

I believe this means that it is the exact same format as the sample code you have given, but you just remove the line HImage interleaved = patras.InterleaveChannels("argb", "match", 255);

Your code will probably look like this if patras is a gray scale image:

HTuple type, width, height;
HImage patras = new HImage("patras");
IntPtr ptr = patras.GetImagePointer1(out type, out width, out height);
Image img = new Bitmap(width/4, height, width, PixelFormat.Format16bppGrayScale, ptr);

pictureBox.Image = img;
Catherine
  • 99
  • 9
1

Since you cannot directly create 8-bit grayscale bitmap, the quickest way would be to convert gray image into RGB:

        HImage hiImageNew = new HImage();
        hiImageNew = hiImage.Compose3(hiImage, hiImage);

        hiImageNew = hiImageNew.InterleaveChannels("argb", "match", 255);
        IntPtr ptr = hiImageNew.GetImagePointer1(out htType, out htWidth, out htHeight);
        System.Drawing.Image bImage = new Bitmap(htWidth/4, htHeight, htWidth, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, ptr);
Vladimir Perković
  • 1,351
  • 3
  • 12
  • 30
  • this might be "quickest" from a code point of view, but I work with very high res images, and the difference between read/writing a whole color image to disk and a grayscale image is huge. I am still testing @Catherine answer which may be acceptable for ym case even though it is 16bpp grayscale. – shelbypereira Jul 13 '18 at 11:36
  • Why are you not just saving HImages directly without conversion to Bitmap? – Vladimir Perković Jul 13 '18 at 14:47
  • we would like to return the Bitmap to the Middleware of our application where it may either be saved to disk or the DB. However we don't want to expose the HImage type to the rest of the application. – shelbypereira Jul 13 '18 at 19:16
1

check this: https://github.com/Joncash/HanboAOMClassLibrary/blob/master/Hanbo.Helper/ImageConventer.cs

In this Class you can find the function in which you can choose if you have a grayscale or rgb image

public static Bitmap ConvertHalconImageToBitmap(HObject halconImage, bool isColor)
Michel
  • 25
  • 4