设置首页收藏本站
开启左侧

Fast method to convert HObjecrt to Bitmap

[复制链接]
Evgeniy 发表于 2021-6-10 19:21:07 | 显示全部楼层 |阅读模式
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[0] = Blue[0];  //A
                        source_p++;
                        Blue++;
                        source_p[0] = Green[0];  //R
                        source_p++;
                        Green++;
                        source_p[0] = Red[0];  //G
                        source_p++;
                        Red++;
                        source_p++;
                    }
                }

            }

            bitmap.UnlockBits(bmpData);

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

奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
风景你好 发表于 2021-6-10 20:29:59 | 显示全部楼层
  1. private static void HObject2ColoredBitmap(HObject image, out Bitmap res)
  2.         {
  3.             try
  4.             {
  5.                 HTuple hred, hgreen, hblue, type, width, height;

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

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

  8.                 Rectangle rect = new Rectangle(0, 0, width, height);
  9.                 BitmapData bitmapData = res.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
  10.                 int imglength = width * height;
  11.                 unsafe
  12.                 {
  13.                     byte* bptr = (byte*)bitmapData.Scan0;
  14.                     byte* r = ((byte*)hred.I);      //((byte*)hred.L);
  15.                     byte* g = ((byte*)hgreen.I);    // ((byte*)hgreen.L);
  16.                     byte* b = ((byte*)hblue.I);     //((byte*)hblue.L);
  17.                     for (int i = 0; i < imglength; i++)
  18.                     {
  19.                         bptr[i * 4] = (b)[i];
  20.                         bptr[i * 4 + 1] = (g)[i];
  21.                         bptr[i * 4 + 2] = (r)[i];
  22.                         bptr[i * 4 + 3] = 255;
  23.                     }
  24.                 }

  25.                 res.UnlockBits(bitmapData);
  26.             }
  27.             catch (Exception ex)
  28.             {
  29.                 res = null;
  30.                 throw ex;
  31.             }
  32.         }
复制代码
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| Evgeniy 发表于 2021-6-15 19:59:25 | 显示全部楼层
Thank you for answer!
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| Evgeniy 发表于 2021-6-15 20:01:44 | 显示全部楼层
Unfourtanely this code is not working.

Checked.
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
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
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| 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?
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| 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 )
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
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!
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
CSDN阿達 发表于 2022-12-13 15:54:38 | 显示全部楼层
thanks a lot
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表