惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - double64

C++ 安全的拷贝赋值(Copy-and-Swap 惯用法) C++ 基类派生类的 static_cast 和 dynamic_cast 转换的简单测试 visual studio 的 snippet 代码片段模板样式 Windows 右键管理官方小程序Autoruns C++ 结合 enum 按位与或组合检测枚举项 std::unique_ptr 当删除器类型为 无状态的时构造可以不用传删除器示例 两个用来写 CLI - Command-Line Interface 的命令解析库 enum class 类型转换 int 微软输入法中如何输出当前时间 音程知识 C++ 模板引用参数的各种情况 英语 12 种时态 Qt 手动添加 Q_OBJECT 需要添加的地方 C# WPF 绑定 ObservableObject 实现 INotifyPropertyChanged 接口 C++ 标准库 copy_if C++ lambda 和 bind 何时优先使用 Windows 下的 Qt 中 min max 函数冲突 C++ RVO 或 NRVO 可能触发的条件 C++ std::unique_ptr 和 std::shared_ptr 都支持自定义删除器(deleter) C++ 智能指针和动态数组 std::vector 插入另一个 vector 的范围元素 Qt tableWidget QTableWidget 常用一些属性设置 C++ 内部类(嵌套类)是可以访问外部类的私有保护成员的 C++ 宏展开顺序 C++ std::round() 四舍五入 cv::Mat 和 HalconCpp::HImage 生成和保存图像简单测试 简单易用的图像库:stb_image
Halcon HImage 与 Qt QImage 的相互转换
double64 · 2025-04-10 · via 博客园 - double64

.h

#pragma once 
#include "QImage"
#include "Halcon.h"
#include "halconcpp/HalconCpp.h"
#include "halconcpp/HDevThread.h"
 
 
/**
* @brief QImage2HImage 将 Qt QImage 转换为 Halcon 的 HImage
* @param from 输入的 QImage
* @param to 输出的 HImage ,from 和 to 不共享内存数据。 每次都会为 to 重新分配内存。
* @return true 表示转换成功,false 表示转换失败。
*/
bool QImageToHImage(QImage &from, HalconCpp::HImage &to);

/**
* @brief HImage2QImage 将 Halcon 的 HImage 转换为 Qt 的 QImage
* @param from HImage ,暂时只支持 8bits 灰度图像和 8bits 的 3 通道彩色图像
* @param to QImage ,这里 from 和 to 不共享内存。如果 to 的内存大小合适,那么就不用重新分配内存。所以可以加快速度。
* @return  true 表示转换成功,false 表示转换失败
*/
bool HImageToQImage(HalconCpp::HImage &from, QImage &to);

.cpp

#include "stdafx.h"
#include "ConvertQImageHImage.h"

using HalconCpp::HString;
using HalconCpp::HTuple;

// QImage qimg = pixMap.toImage();


bool QImageToHImage(QImage &from, HalconCpp::HImage &to)
{
    if (from.isNull()) return false;

    int width = from.width(), height = from.height();
    QImage::Format format = from.format();

    if (format == QImage::Format_RGB32 ||
        format == QImage::Format_ARGB32 ||
        format == QImage::Format_ARGB32_Premultiplied)
    {
        if (from.bytesPerLine() == 4 * width)
        {
            to.GenImageInterleaved(from.bits(), "bgrx", width, height, 0, "byte", width, height, 0, 0, 8, 0);
        }
        else
        {
            to.GenImageInterleaved(from.bits(), "bgrx", width, height, 0, "byte", width, height, 0, 0, 8, 0);
            uchar *R, *G, *B;
            HString Type;
            Hlong Width, Height;
            to.GetImagePointer3(reinterpret_cast<void **>(&R),
                reinterpret_cast<void **>(&G),
                reinterpret_cast<void **>(&B), &Type, &Width, &Height);
            for (int row = 0; row < height; row++)
            {
                QRgb* line = reinterpret_cast<QRgb*>(from.scanLine(row));
                for (int col = 0; col < width; col++)
                {
                    *R = qRed(line[col]);
                    *G = qGreen(line[col]);
                    *B = qBlue(line[col]);
                    ++R;
                    ++G;
                    ++B;
                }
            }
        }
        return true;
    }
    else if (format == QImage::Format_RGB888)
    {
        if (from.bytesPerLine() == 3 * width)
        {
            to.GenImageInterleaved(from.bits(), "rgb", width, height, 0, "byte", width, height, 0, 0, 8, 0);
        }
        else
        {
            to.GenImageInterleaved(from.bits(), "rgb", width, height, 0, "byte", width, height, 0, 0, 8, 0);
            uchar *R, *G, *B;
            HString Type;
            Hlong Width, Height;
            to.GetImagePointer3(reinterpret_cast<void **>(&R),
                reinterpret_cast<void **>(&G),
                reinterpret_cast<void **>(&B), &Type, &Width, &Height);
            for (int row = 0; row < height; row++)
            {
                unsigned char* line = reinterpret_cast<unsigned char *>(from.scanLine(row));
                for (int col = 0; col < width; col++)
                {
                    *R++ = *line++;
                    *G++ = *line++;
                    *B++ = *line++;
                }
            }
        }
        return true;
    }
    else if (format == QImage::Format_Grayscale8 || format == QImage::Format_Indexed8)
    {
        if (from.bytesPerLine() == width)
        {
            to.GenImage1("byte", width, height, from.bits());
        }
        else// 这时说明每行数据之间有填充字节。因此需要重新写数据
        {
            to.GenImageConst("byte", width, height);
            Hlong W, H; HString Type;
            unsigned char * pTo = reinterpret_cast<unsigned char *>(to.GetImagePointer1(&Type, &W, &H));
            for (int row = 1; row < H; row++)
            {
                const unsigned char * pSrc = from.constScanLine(row);
                unsigned char * pDist = pTo + row * W;
                memcpy(pDist, pSrc, static_cast<size_t>(W));
            }
        }
        return true;
    }
    return false;
}


bool HImageToQImage(HalconCpp::HImage &from, QImage &to)
{
    Hlong width;
    Hlong height;
    from.GetImageSize(&width, &height);

    HTuple channels = from.CountChannels();
    HTuple type = from.GetImageType();

    if (strcmp(type[0].S(), "byte")) // 如果不是 byte 类型,则失败
    {
        return false;
    }

    QImage::Format format;
    switch (channels[0].I())
    {
    case 1:
        format = QImage::Format_Grayscale8;
        break;
    case 3:
        format = QImage::Format_RGB888;
        break;
    default:
        return false;
    }

    if (to.width() != width || to.height() != height || to.format() != format)
    {
        to = QImage(static_cast<int>(width),
            static_cast<int>(height),
            format);
    }
    HString Type;
    if (channels[0].I() == 1)
    {
        unsigned char * pSrc = reinterpret_cast<unsigned char *>(from.GetImagePointer1(&Type, &width, &height));
        if (to.bytesPerLine() == width)
        {
            memcpy(to.bits(), pSrc, static_cast<size_t>(width) * static_cast<size_t>(height));
        }
        else
        {
            for (int row = 1; row < height; row++)
            {
                unsigned char * pDistLine = to.scanLine(row);
                const unsigned char * pSrcLine = pSrc + row * width;
                memcpy(pDistLine, pSrcLine, static_cast<size_t>(width));
            }
        }

        return true;
    }
    else if (channels[0].I() == 3)
    {
        uchar *R, *G, *B;
        from.GetImagePointer3(reinterpret_cast<void **>(&R),
            reinterpret_cast<void **>(&G),
            reinterpret_cast<void **>(&B), &Type, &width, &height);

        for (int row = 0; row < height; row++)
        {
            unsigned char * line = reinterpret_cast<unsigned char *>(to.scanLine(row));
            for (int col = 0; col < width; col++)
            {
                *line++ = *R++;
                *line++ = *G++;
                *line++ = *B++;
            }
        }
        return true;
    }

    return false;
}

源自:https://blog.csdn.net/liyuanbhu/article/details/143775925 // 博文链接