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

[C++] Qt的QImage对象与Halcon的HImage对象相互转换

[复制链接]
绝地武士 发表于 2025-5-7 20:44:07 | 显示全部楼层 |阅读模式
结合网上和Ds的结果修改而成,特此记录:
说明:这里的彩色图像必须是byte类型,否则会出错。
2025/05/23修复RGB转换出来的灰度值部分差异,无法配对转换前的数据


HImage转QImage:
  1. bool convertHImage2QImage(HImage &from, QImage &to)
  2. {
  3.     HTuple hv_Width, hv_Height, hv_Channels, hv_Type;
  4.     GetImageSize(from, &hv_Width, &hv_Height);
  5.     hv_Channels = from.CountChannels();
  6.     hv_Type = from.GetImageType();

  7.     if(strcmp(hv_Type.S(), "byte"))
  8.         return false;

  9.     QImage::Format format;
  10.     switch(hv_Channels.I())
  11.     {
  12.     case 1:
  13.         format = QImage::Format_Grayscale8;
  14.         break;
  15.     case 3:
  16.         format = QImage::Format_RGB32;
  17.         break;
  18.     default:
  19.         return false;
  20.     }

  21.     int nWidth = hv_Width.I();
  22.     int nHeight = hv_Height.I();
  23.     if(to.width() != nWidth || to.height() != nHeight || to.format() != format)
  24.         to = QImage(nWidth, nHeight, format);

  25.     if (1 == hv_Channels.I())
  26.     {
  27.         HTuple pointer = from.GetImagePointer1(&hv_Type, &hv_Width, &hv_Height);
  28.         uchar* pData = reinterpret_cast<uchar*>(pointer.L());
  29.         memcpy(to.bits(), pData, nWidth * nHeight);
  30.         return true;
  31.     }
  32.     else if (3 == hv_Channels.I())
  33.     {
  34.         HTuple ptrR, ptrG, ptrB;
  35.         GetImagePointer3(from, &ptrR, &ptrG, &ptrB, &hv_Type, &hv_Width, &hv_Height);
  36.         uchar* pDataR = reinterpret_cast<uchar*>(ptrR.L());
  37.         uchar* pDataG = reinterpret_cast<uchar*>(ptrG.L());
  38.         uchar* pDataB = reinterpret_cast<uchar*>(ptrB.L());

  39.         for (int i = 0; i < nHeight; i++)
  40.         {
  41.             for (int j = 0; j < nWidth; j++)
  42.             {
  43.                 int src_idx = i * nWidth + j;
  44.                 int dst_idx = src_idx * 4;
  45.                 to.bits()[dst_idx]     = pDataB[src_idx];       // B
  46.                 to.bits()[dst_idx + 1] = pDataG[src_idx];       // G
  47.                 to.bits()[dst_idx + 2] = pDataR[src_idx];       // R
  48.                 to.bits()[dst_idx + 3] = 0xFF;                  // Alpha
  49.             }
  50.         }
  51.         ptrR.Clear();
  52.         ptrG.Clear();
  53.         ptrB.Clear();
  54.         pDataR = nullptr;
  55.         pDataG = nullptr;
  56.         pDataB = nullptr;
  57.         return true;
  58.     }
  59.     return false;
  60. }
复制代码
转换QImage的图像类型必须是RGB32显示,如果用RGB888会出现错位,所在生成阶段直接生成RGB32即可。

QImage转HImage:
  1. bool convertQImage2HImage(QImage &from, HImage &to)
  2. {
  3.    if(from.isNull()) return false;
  4.     int width = from.width(), height = from.height();
  5.     QImage::Format format = from.format();

  6.     if(format == QImage::Format_RGB32 ||
  7.         format == QImage::Format_ARGB32 ||
  8.         format == QImage::Format_ARGB32_Premultiplied)
  9.     {
  10.         to.GenImageInterleaved(from.bits(), "rgbx", width, height, 0, "byte", width, height, 0, 0, 8, 0);
  11.         return true;
  12.     }
  13.     else if(format == QImage::Format_RGB888)
  14.     {
  15.         to.GenImageInterleaved(from.bits(), "rgb", width, height, 0, "byte", width, height, 0, 0, 8, 0);
  16.         return true;
  17.     }else if(format == QImage::Format_Grayscale8)
  18.     {
  19.         to.GenImage1("byte", width, height, from.bits());
  20.         return true;
  21.     }
  22.     return false;
  23. }
复制代码

奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
雾里看花 发表于 2025-5-8 08:25:52 | 显示全部楼层
感谢分享,,,,,,,,,
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
halcon2016 发表于 2025-5-8 08:44:21 | 显示全部楼层
感谢分享,,,,,,,,,
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
halcon2016 发表于 2025-5-8 08:50:14 | 显示全部楼层
没有考虑到4字节对齐的问题
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
1531585571 发表于 2025-5-8 08:58:22 | 显示全部楼层
感谢分享,,,,,,,,,
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
halcon_scholar 发表于 2025-5-9 09:31:11 | 显示全部楼层
感谢分享,研究一下看看。。。
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
uvzybq2013 发表于 2025-5-15 21:31:21 | 显示全部楼层
感谢分享,,,,,,,,,
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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