Halcon与OpenCV图像类型转换基于C++ (附代码)

    视觉图像处理中使用最广泛的两个视觉库一个是开源的OpenCV,另一个是收费的Halcon,它们各有各的优势,这里不做对比评价。


    在使用中我们有时候需要将Halcon和OpenCV中图像类型做转换,所以这里为一些新手朋友们整理一下,使用的时候可以直接复制调用。对于8位图像:包括三通道彩色图像和单通道灰度图像均适用。


(1)Halcon HObject -> OpenCV Mat

Mat HImageToMat(HObject &imgHalcon)
{
    HTuple channels;
    HString cType;
    cv::Mat Image;
    ConvertImageType(imgHalcon, &imgHalcon, "byte");
    CountChannels(imgHalcon, &channels);
    Hlong width = 0;
    Hlong height = 0;
    if (channels[0].I() == 1)
    {
        HImage hImg(imgHalcon);
        void *ptr = hImg.GetImagePointer1(&cType, &width, &height);//GetImagePointer1(Hobj, &ptr, &cType, &wid, &hgt);
        int W = width;
        int H = height;
        Image.create(H, W, CV_8UC1);
        unsigned char *pdata = static_cast<unsigned char *>(ptr);
        memcpy(Image.data, pdata, W*H);
    }
    else if (channels[0].I() == 3)
    {
        void *Rptr;
        void *Gptr;
        void *Bptr;
        HImage hImg(imgHalcon);
        hImg.GetImagePointer3(&Rptr, &Gptr, &Bptr, &cType, &width, &height);
        int W = width;
        int H = height;
        Image.create(H, W, CV_8UC3);
        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 *R = (unsigned char *)Rptr;
        unsigned char *G = (unsigned char *)Gptr;
        unsigned char *B = (unsigned char *)Bptr;
        memcpy(VecM[2].data, R, W*H);
        memcpy(VecM[1].data, G, W*H);
        memcpy(VecM[0].data, B, W*H);
        cv::merge(VecM, Image);
    }
    return Image;
}

(2)OpenCV Mat -> Halcon HObject

HObject MainWindow::MatToHImage(Mat &imgMat)
{
    HObject Hobj = HObject();
    int height = imgMat.rows;
    int width = imgMat.cols;
    int i;
    //  CV_8UC3
    if (imgMat.type() == CV_8UC3)
    {
        vector<cv::Mat> imgchannel;
        split(imgMat, imgchannel);
        cv::Mat imgB = imgchannel[0];
        cv::Mat imgG = imgchannel[1];
        cv::Mat imgR = imgchannel[2];
        uchar* dataR = new uchar[height * width];
        uchar* dataG = new uchar[height * width];
        uchar* dataB = new uchar[height * width];
        for (i = 0; i<height; i++)
        {
            memcpy(dataR + width*i, imgR.data + imgR.step*i, width);
            memcpy(dataG + width*i, imgG.data + imgG.step*i, width);
            memcpy(dataB + width*i, imgB.data + imgB.step*i, width);
        }
        GenImage3(&Hobj, "byte", width, height, (Hlong)dataR, (Hlong)dataG, (Hlong)dataB);
        delete[]dataR;
        delete[]dataG;
        delete[]dataB;
    }
    //  CV_8UCU1
    else if (imgMat.type() == CV_8UC1)
    {
        uchar* data = new uchar[height*width];
        for (i = 0; i<height; i++)
            memcpy(data + width*i, imgMat.data + imgMat.step*i, width);
        GenImage1(&Hobj, "byte", width, height, (Hlong)data);
        delete[] data;
    }
    return Hobj;
}

 

(3)调用后的效果(8位彩色图像和灰度图像)

Halcon与OpenCV图像类型转换基于C++ (附代码)

Halcon与OpenCV图像类型转换基于C++ (附代码)

Halcon与OpenCV图像类型转换基于C++ (附代码)

 

上一篇:Halcon图像的通道转换


下一篇:halcon案例学习之cbm_label_simple