halcon HObiect和 opencv Mat 转化
来源:互联网 发布:淘宝网hd下载 编辑:程序博客网 时间:2024/06/06 01:21
Mat HImageToIplImage(HObject &Hobj)
{
//get_grayval(Image : : Row, Column : Grayval)
cv::Mat pImage;
HTuple htChannels;
HTuple width, height;
width = height = 0;
//转换图像格式
ConvertImageType(Hobj, &Hobj, "byte");
CountChannels(Hobj, &htChannels);
HTuple cType;
HTuple grayVal;
if (htChannels.I() == 1)
{
GetImageSize(Hobj, &width, &height);
pImage = cv::Mat(height, width, CV_8UC1);
pImage = Mat::zeros(height, width, CV_8UC1);
for (int i = 0; i < height.I(); ++i)
{
for (int j = 0; j < width.I(); ++j)
{
GetGrayval(Hobj, i, j, &grayVal);
pImage.at<uchar>(i, j) = (uchar)grayVal.I();
}
}
}
else if (htChannels.I() == 3)
{
GetImageSize(Hobj, &width, &height);
pImage = cv::Mat(height, width, CV_8UC3);
for (int row = 0; row < height.I(); row++)
{
for (int col = 0; col < width.I(); col++)
{
GetGrayval(Hobj, row, col, &grayVal);
pImage.at<uchar>(row, col * 3) = (uchar)grayVal[2].I();
pImage.at<uchar>(row, col * 3 + 1) = (uchar)grayVal[1].I();
pImage.at<uchar>(row, col * 3 + 2) = (uchar)grayVal[0].I();
}
}
}
return pImage;
}
HObject IplImageToHImage(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];
}
}
GenImage3(&Hobj, "byte", width, height, (Hlong)(dataRed), (Hlong)(dataGreen), (Hlong)(dataBlue));
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;
}
{
//get_grayval(Image : : Row, Column : Grayval)
cv::Mat pImage;
HTuple htChannels;
HTuple width, height;
width = height = 0;
//转换图像格式
ConvertImageType(Hobj, &Hobj, "byte");
CountChannels(Hobj, &htChannels);
HTuple cType;
HTuple grayVal;
if (htChannels.I() == 1)
{
GetImageSize(Hobj, &width, &height);
pImage = cv::Mat(height, width, CV_8UC1);
pImage = Mat::zeros(height, width, CV_8UC1);
for (int i = 0; i < height.I(); ++i)
{
for (int j = 0; j < width.I(); ++j)
{
GetGrayval(Hobj, i, j, &grayVal);
pImage.at<uchar>(i, j) = (uchar)grayVal.I();
}
}
}
else if (htChannels.I() == 3)
{
GetImageSize(Hobj, &width, &height);
pImage = cv::Mat(height, width, CV_8UC3);
for (int row = 0; row < height.I(); row++)
{
for (int col = 0; col < width.I(); col++)
{
GetGrayval(Hobj, row, col, &grayVal);
pImage.at<uchar>(row, col * 3) = (uchar)grayVal[2].I();
pImage.at<uchar>(row, col * 3 + 1) = (uchar)grayVal[1].I();
pImage.at<uchar>(row, col * 3 + 2) = (uchar)grayVal[0].I();
}
}
}
return pImage;
}
HObject IplImageToHImage(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];
}
}
GenImage3(&Hobj, "byte", width, height, (Hlong)(dataRed), (Hlong)(dataGreen), (Hlong)(dataBlue));
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;
}
阅读全文
0 0
- halcon HObiect和 opencv Mat 转化
- Opencv中Mat和IplImage的转化
- OpenCV的Mat和Halcon的HObject类型互相转换
- opencv Mat结构和CImage的转化和显示
- opencv Mat结构和CImage的转化和显示
- opencv cv::MAT 和 QT QImage 相互转化代码
- opencv中Mat、CvMat和IplImage的相互转化
- QT中的QImage、QPixmap和opencv 中的mat转化
- opencv mat 转化成IplImage
- opencv Mat&类型图像复制(P.S.CvArr*和Mat&转化的问题)
- QImage和 Mat 转化
- Halcon13菜鸟 Opencv Mat 转halcon并且显示 vs2013
- OpenCV中IplImage/CvMat/Mat转化关系
- opencv Mat与Iplimage相互转化
- OpenCV中矩阵类详解之三:CvMat,Mat和IplImage之间的转化和拷贝
- OpenCV中矩阵类详解之三:CvMat,Mat和IplImage之间的转化和拷贝
- 学习OpenCV(3)CvMat,Mat和IplImage之间的转化和拷贝
- iOS openCV CvMat,Mat和IplImage之间的转化和拷贝
- json取值
- LeetCode--Minimum Path Sum
- 第三周项目一(1)—顺序表的基本运算
- ++i 与 i++ 区别及踩过的坑
- MOS管开关电路设计
- halcon HObiect和 opencv Mat 转化
- lua基础学习(翻译defsniky (@i小雨生))
- resolv.conf:search、domain、nameserver解释
- 字符编码问题
- 第3周项目1--顺序表的基本运算(1)
- java Maven 配置
- JPA EntityManager的四个主要方法:persist,merge,refresh和remove
- JTA 深度历险
- leetcode Majorty Element