OpenCV数据格式转换成Halcon数据格式HObject

来源:互联网 发布:元胞数组 编辑:程序博客网 时间:2024/06/06 02:40
HObject MatToHImage(Mat  pImage,HObject   &Hobj)
{


if(pImage.channels()==1)
{
int height=pImage.rows;
int width=pImage.cols;
uchar *dataGray=new uchar[width*height];
for(int i=0; i<height; i++)
{
memcpy(dataGray+width*i, pImage.data+pImage.step*i,width);
}
GenImage1(&Hobj,"byte",pImage.cols,pImage.rows,(Hlong)(dataGray));
delete[ ] dataGray;
}
if(pImage.channels()==3)
{
int height=pImage.rows;
int width=pImage.cols;
Mat  ImageRed, ImageGreen, ImageBlue;
ImageRed=Mat(height,width,CV_8UC1);
ImageGreen=Mat(height,width,CV_8UC1);
ImageBlue=Mat(height,width,CV_8UC1);
vector<Mat> ImageChannels;
split(pImage,ImageChannels);


ImageBlue=ImageChannels.at(0);
ImageGreen=ImageChannels.at(1);
ImageRed=ImageChannels.at(2);


uchar*  dataRed=new uchar[pImage.cols*pImage.rows];
uchar*  dataGreen=new uchar[pImage.cols*pImage.rows];
uchar*  dataBlue=new uchar[pImage.cols*pImage.rows];
for(int i=0; i<height; i++)
{
memcpy(dataRed+width*i, ImageRed.data+ImageRed.step*i,width);
memcpy(dataGreen+width*i, ImageGreen.data+ImageGreen.step*i,width);
memcpy(dataBlue+width*i, ImageBlue.data+ImageBlue.step*i,width);
}
GenImage3(&Hobj,"byte",pImage.cols,pImage.rows,(Hlong)(dataRed),(Hlong)(dataGreen),(Hlong)(dataBlue));
delete[ ]  dataRed;
delete[ ]  dataGreen;
delete[ ]  dataBlue;
}
return Hobj;
}
0 0
原创粉丝点击