halcon-HObject类型转Mat类型
作者:互联网
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; }
标签:wid,Mat,hgt,VecM,unsigned,halcon,char,HObject,Hobj 来源: https://www.cnblogs.com/liming19680104/p/15854295.html