opencv和halcon图像数据类型相互转换

2020-06-19  本文已影响0人  卡拉肖克_潘

halcon转opencv需要1.7ms,opencv转halcon需要30ms。

#include "HalconCpp.h"  
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
using namespace HalconCpp;


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

Mat HObject2Mat(HObject Hobj)
{
    HTuple htCh = HTuple();
    HTuple cType;
    Mat Image;
    ConvertImageType(Hobj, &Hobj, "byte");
    CountChannels(Hobj, &htCh);
    HTuple wid;
    HTuple hgt;
    int W, H;
    if (htCh[0].I() == 1)
    {
        HTuple ptr;
        GetImagePointer1(Hobj, &ptr, &cType, &wid, &hgt);
        W = (Hlong)wid;
        H = (Hlong)hgt;
        Image.create(H, W, CV_8UC1);
        uchar* pdata = (uchar*)(Hlong)ptr[0].I();
        memcpy(Image.data, pdata, W*H);
    }
    else if (htCh[0].I() == 3)
    {
        HTuple ptrR, ptrG, ptrB;
        GetImagePointer3(Hobj, &ptrR, &ptrG, &ptrB, &cType, &wid, &hgt);
        W = (Hlong)wid;
        H = (Hlong)hgt;
        Image.create(H, W, CV_8UC3);
        vector<Mat> vecM(3);
        vecM[2].create(H, W, CV_8UC1);
        vecM[1].create(H, W, CV_8UC1);
        vecM[0].create(H, W, CV_8UC1);
        uchar* pr = (uchar*)(Hlong)ptrR[0];
        uchar* pg = (uchar*)(Hlong)ptrG[0];
        uchar* pb = (uchar*)(Hlong)ptrB[0];
        memcpy(vecM[2].data, pr, W*H);
        memcpy(vecM[1].data, pg, W*H);
        memcpy(vecM[0].data, pb, W*H);
        merge(vecM, Image);
    }
    return Image;
}
HObject IplImageToHImage(cv::Mat& pImage)
{
    HObject Hobj;
    if (3 == pImage.channels())
    {
        cv::Mat pImageRed, pImageGreen, pImageBlue;
        std::vector<cv::Mat> sbgr(3);
        cv::split(pImage, sbgr);

        int length = pImage.rows * pImage.cols;
        uchar *dataBlue = new uchar[length];
        uchar *dataGreen = new uchar[length];
        uchar *dataRed = new uchar[length];

        int height = pImage.rows;
        int width = pImage.cols;
        for (int row = 0; row < height; row++)
        {
            uchar* ptr = pImage.ptr<uchar>(row);
            for (int col = 0; col < width; col++)
            {
                dataBlue[row * width + col] = ptr[3 * col];
                dataGreen[row * width + col] = ptr[3 * col + 1];
                dataRed[row * width + col] = ptr[3 * col + 2];
            }
        }
        double t = (double)getTickCount();
        GenImage3(&Hobj, "byte", width, height, (Hlong)(dataRed), (Hlong)(dataGreen), (Hlong)(dataBlue));
        t = (double)getTickCount() - t;
        t = t*1000. / cv::getTickFrequency();
        cout << t << endl;
        
        delete[] dataRed;
        delete[] dataGreen;
        delete[] dataBlue;
    }
    else if (1 == pImage.channels())
    {
        int height = pImage.rows;
        int width = pImage.cols;
        uchar *dataGray = new uchar[width * height];
        memcpy(dataGray, pImage.data, width * height);
        GenImage1(&Hobj, "byte", width, height, (Hlong)(dataGray));
        delete[] dataGray;
    }

    return Hobj;
}

int main()
{
    /*Hobj-->Mat*/
    HImage img;
    ReadImage(&img, "D:\\WorkSpace\\123.png");
    Mat cvImg;
    double t = (double)getTickCount();
    cvImg = HObject2Mat(img);
    t = (double)getTickCount() - t;
    t = t*1000. / cv::getTickFrequency();
    cout << t << endl;
    imshow("cvWindow", cvImg);
    //system("pause");
    waitKey();
    /**/


    /*Mat-->hobj*
    Mat cvImg=imread("D:\\WorkSpace\\123.png",0);
    
    HImage img;
    double t = (double)getTickCount();
    img = Mat2HObject(cvImg);
//  img = IplImageToHImage(cvImg);
    t = (double)getTickCount() - t;
    t = t*1000. / cv::getTickFrequency();
    cout << t << endl;
    Hlong width, height;
    img.GetImageSize(&width, &height);
    HWindow w(0, 0, 0.5*width, 0.5*height);
    img.DispImage(w);
    w.Click();
    w.ClearWindow();
    /**/
    return 0;
    
}
上一篇下一篇

猜你喜欢

热点阅读