Halcon 与 OpenCV 图像数据类型转换

在之前大神的Halcon 与 OpenCV 图像数据类型的转换过程中,使用了C 接口的 Halcon 函数,如 read_image()在C++接口中则为ReadImage();

由Help可知,Halcon 中 C 接口与 C++ 接口支持的数据类型并不完全一样,因为,本人需要在C++环境下开发,所以,之前的代码,许多地方不能

使用,所以,在前人的基础上,对代码进行修改,修改后进行测试,发现通过读取灰度值的方式与直接用指针操作,时间消耗差别不大,以代码中

的样本图片为例,以指针的形式进行访问,在 Halcon 转 OpenCV 的时间消耗为:600ms 左右,以读取灰度值的方式,也在600 ms 左右;

但由OpenCV 转 Halcon 时间消耗很少,大约只有 10 几 ms;

代码如下所示:

[cpp]  view plain  copy
 print ?
  1.     
  2. #include <HalconCpp.h>    
  3. #include <opencv2/opencv.hpp>  
  4. #include <iostream>  
  5. #include <windows.h>  
  6.   
  7. using namespace std;  
  8. using namespace HalconCpp;  
  9. using namespace cv;  
  10.   
  11. HObject IplImageToHImage(cv::Mat& pImage);  
  12. cv::Mat HImageToIplImage(HObject &Hobj);  
  13.   
  14. //IplImage* HImageToIplImage(HObject &Hobj);  
  15. int main(int argc, char* argv[])  
  16. {  
  17.       
  18.     //Hobject Image, GrayImage;  
  19.     HObject Image, GrayImage;  
  20.     ReadImage(&Image, "lena.jpg");  
  21.     Rgb1ToGray(Image, &GrayImage);  
  22.   
  23.     //Mat img = imread("lena.jpg");  
  24.   
  25.     SYSTEMTIME tm;  
  26.     GetLocalTime(&tm);  
  27.   
  28.     int t1 = tm.wSecond * 1000 + tm.wMilliseconds;  
  29.   
  30.     //HObject hObj = IplImageToHImage(img);  
  31.     cv::Mat opencvImg = HImageToIplImage(GrayImage);  
  32.   
  33.     GetLocalTime(&tm);  
  34.     int t2 = tm.wSecond * 1000 + tm.wMilliseconds;  
  35.   
  36.     int t = t2 - t1;  
  37.   
  38.     cout << "时间消耗:" << t << "ms" << endl;  
  39.   
  40. //  HTuple winHandle;  
  41. //  HTuple w, h;  
  42. //  GetImageSize(hObj, &w, &h);  
  43. //  OpenWindow(0, 0, w, h, 0, "", "", &winHandle);  
  44. //  DispObj(hObj, winHandle);  
  45.   
  46.   
  47. //  int t1 = tm.wSecond * 1000 + tm.wMilliseconds;  
  48. //   
  49. //  cv::Mat opencvImg = HImageToIplImage(Image);  
  50. //   
  51. //  GetLocalTime(&tm);  
  52. //  int t2 = tm.wSecond * 1000 + tm.wMilliseconds;  
  53. //   
  54. //  int t = t2 - t1;  
  55. //   
  56. //  cout << "时间消耗为:" << t << "ms" << endl;  
  57.   
  58.     namedWindow("img", 1);  
  59.     imshow("img", opencvImg);  
  60.     waitKey();  
  61.     system("pause");  
  62.   
  63.     return 0;  
  64. }  
  65.   
  66. cv::Mat HImageToIplImage(HObject &Hobj)  
  67. {  
  68.     //get_grayval(Image : : Row, Column : Grayval)  
  69.   
  70.     cv::Mat pImage;  
  71.     HTuple htChannels;  
  72.     HTuple     width, height;  
  73.     width = height = 0;  
  74.     //转换图像格式    
  75.     ConvertImageType(Hobj, &Hobj, "byte");  
  76.     CountChannels(Hobj, &htChannels);  
  77.     HTuple cType;  
  78.     HTuple grayVal;  
  79.   
  80.     if (htChannels.I() == 1)  
  81.     {  
  82.         GetImageSize(Hobj, &width, &height);  
  83.   
  84.         pImage = cv::Mat(height, width, CV_8UC1);  
  85.         pImage = Mat::zeros(height, width, CV_8UC1);  
  86.   
  87.         for (int i = 0; i < height.I(); ++i)  
  88.         {  
  89.             for (int j = 0; j < width.I(); ++j)  
  90.             {  
  91.                 GetGrayval(Hobj, i, j, &grayVal);  
  92.                 pImage.at<uchar>(i, j) = (uchar)grayVal.I();  
  93.             }  
  94.               
  95.         }  
  96.   
  97.     }  
  98.     else if (htChannels.I() == 3)  
  99.     {  
  100.         GetImageSize(Hobj, &width, &height);  
  101.         pImage = cv::Mat(height, width, CV_8UC3);  
  102.         for (int row = 0; row < height.I(); row++)  
  103.         {  
  104.             for (int col = 0; col < width.I(); col++)  
  105.             {  
  106.                 GetGrayval(Hobj, row, col, &grayVal);  
  107.   
  108.                 pImage.at<uchar>(row, col * 3) = (uchar)grayVal[2].I();  
  109.                 pImage.at<uchar>(row, col * 3 + 1) = (uchar)grayVal[1].I();  
  110.                 pImage.at<uchar>(row, col * 3 + 2) = (uchar)grayVal[0].I();  
  111.   
  112.             }  
  113.         }  
  114.   
  115.     }  
  116.   
  117.     return pImage;  
  118. }  
  119.   
  120. HObject IplImageToHImage(cv::Mat& pImage)  
  121. {  
  122.     HObject Hobj;  
  123.     if (3 == pImage.channels())  
  124.     {  
  125.         cv::Mat pImageRed, pImageGreen, pImageBlue;  
  126.         std::vector<cv::Mat> sbgr(3);  
  127.         cv::split(pImage, sbgr);  
  128.   
  129.         int length = pImage.rows * pImage.cols;  
  130.         uchar *dataBlue = new uchar[length];  
  131.         uchar *dataGreen = new uchar[length];  
  132.         uchar *dataRed = new uchar[length];  
  133.   
  134.         int height = pImage.rows;  
  135.         int width = pImage.cols;  
  136.         for (int row = 0; row < height; row++)  
  137.         {  
  138.             uchar* ptr = pImage.ptr<uchar>(row);  
  139.             for (int col = 0; col < width; col++)  
  140.             {  
  141.                 dataBlue[row * width + col] = ptr[3 * col];  
  142.                 dataGreen[row * width + col] = ptr[3 * col + 1];  
  143.                 dataRed[row * width + col] = ptr[3 * col + 2];  
  144.             }  
  145.         }  
  146.   
  147.         GenImage3(&Hobj, "byte", width, height, (Hlong)(dataRed), (Hlong)(dataGreen), (Hlong)(dataBlue));  
  148.         delete[] dataRed;  
  149.         delete[] dataGreen;  
  150.         delete[] dataBlue;  
  151.     }  
  152.     else if (1 == pImage.channels())  
  153.     {  
  154.         int height = pImage.rows;  
  155.         int width = pImage.cols;  
  156.         uchar *dataGray = new uchar[width * height];  
  157.         memcpy(dataGray, pImage.data, width * height);  
  158.         GenImage1(&Hobj, "byte", width, height, (Hlong)(dataGray));  
  159.         delete[] dataGray;  
  160.     }  
  161.   
  162.     return Hobj;  
  163. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值