zoukankan      html  css  js  c++  java
  • opencv Mat与halcon HObject类型的相互转换

    //Mat与halcon类型的转换
    cv::Mat HObject2Mat(HObject Hobj)
    {
      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);
        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;
    }

    HObject Mat2HObject(const cv::Mat &image)
    {
      HObject Hobj = HObject();
      int hgt = image.rows;
      int wid = image.cols;
      int i;
      // CV_8UC3
      if (image.type() == CV_8UC3)
      {
        vector<cv::Mat> imgchannel;
        split(image, imgchannel);
        cv::Mat imgB = imgchannel[0];
        cv::Mat imgG = imgchannel[1];
        cv::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 = nullptr;
        dataG = nullptr;
        dataB = nullptr;
      }
      // 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 = nullptr;
      }
      return Hobj;
    }

  • 相关阅读:
    经典测试工程师面试题(一)
    python面试题2
    python面试题1
    软件测试面试题2
    软件测试面试题1
    python读写csv文件
    死亡还是合并?和李开复聊聊走投无路创业公司(转)
    项目如何开始:怎样和客户谈需求(转)
    [转载]给IT人员支招:如何跟业务部门谈需求分析?
    VS2013的一些常用快捷键
  • 原文地址:https://www.cnblogs.com/DoublearmyLi/p/14283206.html
Copyright © 2011-2022 走看看