Evgeniy 发表于 2021-6-10 19:21:07

Fast method to convert HObjecrt to Bitmap

Hi, colleagues!
Can anyone help to understand: is any simple and very fast method to convert Image as HObject to Bitmap???

Image size 3000x3000 converting for 1500 ms - it is bad!!!!!

I am using this code:

      public static Bitmap HObject2ColoredBitmap(this HObject myHObject)
      {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            HTuple pointerRed = null;
            HTuple pointerGreen = null;
            HTuple pointerBlue = null;
            HTuple type;
            HTuple width;
            HTuple height;
            HOperatorSet.GetImagePointer3(myHObject, out pointerRed, out pointerGreen, out pointerBlue, out type, out width, out height);

            if (width.Type == HTupleType.EMPTY || height.Type == HTupleType.EMPTY)
            {
                return null;
            }

            Bitmap bitmap = new Bitmap((Int32)width, (Int32)height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width / 4, bitmap.Height / 4), ImageLockMode.ReadWrite, bitmap.PixelFormat);
            IntPtr source_scan = bmpData.Scan0;

            unsafe
            {
                byte* source_p = (byte*)source_scan.ToPointer();
                byte* Red = (byte*)pointerRed.IP.ToPointer();
                byte* Blue = (byte*)pointerBlue.IP.ToPointer();
                byte* Green = (byte*)pointerGreen.IP.ToPointer();

                for (int h = 0; h < bitmap.Height; h++)
                {
                  for (int w = 0; w < bitmap.Width; w++)
                  {
                        source_p = Blue;//A
                        source_p++;
                        Blue++;
                        source_p = Green;//R
                        source_p++;
                        Green++;
                        source_p = Red;//G
                        source_p++;
                        Red++;
                        source_p++;
                  }
                }

            }

            bitmap.UnlockBits(bmpData);

            sw.Stop();
            Console.WriteLine($"Преобразование HObject в Bitmap: {sw.ElapsedMilliseconds}");
            sw = null;
            GC.Collect();
            return bitmap;
      }

风景你好 发表于 2021-6-10 20:29:59

private static void HObject2ColoredBitmap(HObject image, out Bitmap res)
      {
            try
            {
                HTuple hred, hgreen, hblue, type, width, height;

                HOperatorSet.GetImagePointer3(image, out hred, out hgreen, out hblue, out type, out width, out height);

                res = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

                Rectangle rect = new Rectangle(0, 0, width, height);
                BitmapData bitmapData = res.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
                int imglength = width * height;
                unsafe
                {
                  byte* bptr = (byte*)bitmapData.Scan0;
                  byte* r = ((byte*)hred.I);      //((byte*)hred.L);
                  byte* g = ((byte*)hgreen.I);    // ((byte*)hgreen.L);
                  byte* b = ((byte*)hblue.I);   //((byte*)hblue.L);
                  for (int i = 0; i < imglength; i++)
                  {
                        bptr = (b);
                        bptr = (g);
                        bptr = (r);
                        bptr = 255;
                  }
                }

                res.UnlockBits(bitmapData);
            }
            catch (Exception ex)
            {
                res = null;
                throw ex;
            }
      }

Evgeniy 发表于 2021-6-15 19:59:25

Thank you for answer!

Evgeniy 发表于 2021-6-15 20:01:44

Unfourtanely this code is not working.

Checked.

Criss 发表于 2021-6-19 20:09:11

Evgeniy 发表于 2021-6-15 20:01
Unfourtanely this code is not working.

Checked.
it's helpfull for you:https://www.51halcon.com/forum.p ... &tid=4778&fromuid=3

Evgeniy 发表于 2021-6-29 18:11:09

Hi, Criss )

Thank you for your help.

Is only one problem: I can't download attached project file from Baidu vault.
Is any possibility to download this example with another way?

Evgeniy 发表于 2021-6-29 18:14:59

Hi, Criss )))

https://1drv.ms/u/s!AgtcKzAtnq7fi0Ae4NwpPOrCQPM9?e=FezI1m
Thank you very much!!!

I will send you feedback when I will understand this example )

Criss 发表于 2021-7-2 16:24:52

Evgeniy 发表于 2021-6-29 18:14
Hi, Criss )))

https://1drv.ms/u/s!AgtcKzAtnq7fi0Ae4NwpPOrCQPM9?e=FezI1m


you're welcome!

CSDN阿達 发表于 2022-12-13 15:54:38

thanks a lot
页: [1]
查看完整版本: Fast method to convert HObjecrt to Bitmap