halcon彩色转QImage彩色

halcon彩色转QImage彩色

#include "win.h"

Win::Win(QWidget *parent)
    : QWidget(parent)
{

    this->resize(500,300);
    QLabel* label=new QLabel("我是中国人",this);
    label->move(10,10);

    HObject  ho_Image;
    ReadImage(&ho_Image, "D:/bb/tu/ma.jpg");

    QImage myImage=HobjectToQImage(ho_Image);  //halcon彩色转QImage彩色
    QTransform tf;  //坐标变换
    tf.rotate(180,Qt::XAxis);
    QImage im3=myImage.transformed(tf,Qt::FastTransformation); //返回旋转后的图像

    QPixmap p;
    p.convertFromImage(im3);  //把QImage转换成QPixmap
    label->setPixmap(p);
    label->adjustSize();



}

Win::~Win()
{
}

cv::Mat Win::HObject2Mat(HObject Hobj)  //HObject类型转Mat类型
{
    HTuple htCh;
        HString cType;
        cv::Mat Image;
        ConvertImageType(Hobj, &Hobj, "byte");
        CountChannels(Hobj, &htCh);
        Hlong wid = 0;
        Hlong hgt = 0;
        if (htCh[0].I() == 1)
        {
            HImage hImg(Hobj);
            void *ptr = hImg.GetImagePointer1(&cType, &wid, &hgt);//GetImagePointer1(Hobj, &ptr, &cType, &wid, &hgt);
            int W = wid;
            int H = hgt;
            Image.create(H, W, CV_8UC1);
            unsigned char *pdata = static_cast<unsigned char *>(ptr);
            memcpy(Image.data, pdata, W*H);
        }
        else if (htCh[0].I() == 3)
        {
            void *Rptr;
            void *Gptr;
            void *Bptr;
            HImage hImg(Hobj);
            hImg.GetImagePointer3(&Rptr, &Gptr, &Bptr, &cType, &wid, &hgt);
            int W = wid;
            int H = hgt;
            Image.create(H, W, CV_8UC3);
            std::vector<cv::Mat> VecM(3);
            VecM[0].create(H, W, CV_8UC1);
            VecM[1].create(H, W, CV_8UC1);
            VecM[2].create(H, W, CV_8UC1);
            unsigned char *R2 = (unsigned char *)Rptr;
            unsigned char *G = (unsigned char *)Gptr;
            unsigned char *B = (unsigned char *)Bptr;
            memcpy(VecM[2].data, R2, W*H);
            memcpy(VecM[1].data, G, W*H);
            memcpy(VecM[0].data, B, W*H);
            cv::merge(VecM, Image);
        }
        return Image;
}

QImage Win::HobjectToQImage(HObject img)  //halcon彩色转QImage彩色
{

    //转换之后图像是倒立的
    HTuple rImage , gImage , bImage , type , hvWidth , hvHeight;
    BYTE* vcPr = NULL;
    BYTE* vcPg = NULL;
    BYTE* vcPb = NULL;
    BYTE* vcPImage = NULL;
    INT32 vcWidth = 0 , vcHeight = 0;
    //ReadImage(&img,"C:/Users/Administrator/Desktop/8.bmp");

    GetImagePointer3(img , &rImage , &gImage , &bImage , &type , &hvWidth , &hvHeight);
    vcWidth = (Hlong)hvWidth;
    vcHeight = (Hlong)hvHeight;
    vcPb = (BYTE*)rImage[0].L();
    vcPg = (BYTE*)gImage[0].L();
    vcPr = (BYTE*)bImage[0].L();
    //计算bmp图片实际的宽度,因为bmp图片宽度必须四字节对齐
    int bmpWidth = (vcWidth*3 + 3)/4*4;
    vcPImage = new BYTE[bmpWidth * vcHeight * 3];//給bmp图片分配内存空间
    //将halcon图象类型HObject中的数据拷贝出来,放到C++变量类型中
    for(int i = vcHeight - 1 ; i > 0 ; i--){
    for(int j = 0 ; j < vcWidth ; j++){
    *(vcPImage + i*bmpWidth + j*3 + 0) = *vcPb;
    *(vcPImage + i*bmpWidth + j*3 + 1) = *vcPg;
    *(vcPImage + i*bmpWidth + j*3 + 2) = *vcPr;
    vcPb++;
    vcPg++;
    vcPr++;
    }
    }
    QImage qimg(vcPImage,vcWidth,vcHeight,QImage::Format_RGB888);

    return qimg;
}

 

 

 

 

halcon彩色转QImage彩色

上一篇:双向链表


下一篇:Web前端 Canvas图片加载、拖拽、缩放、绘制多边形、打点,坐标偏移获取和颜色获取 例程