简介:opencv开源的计算机视觉库,halcon是商业的视觉库,opencv的一些算子在一般的生产环境下是够用的,但是在对算法速度、精度都要求更高的环境下就有些吃力了。最近在研究halcon,抽几个算子粗略测试一下,效果和速度确实很好,以后可能会在对算法速度要求比较高的地方部署,这里先记录一下opencv和halcon图像数据互转的方法。
参考:
#include "HalconCpp.h"
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
//using namespace cv;
using namespace std;
//using namespace HalconCpp;
cv::Mat HImageToMat(HalconCpp::HObject &H_img)
{
cv::Mat cv_img;
HalconCpp::HTuple channels, w, h;
HalconCpp::ConvertImageType(H_img, &H_img, "byte");
HalconCpp::CountChannels(H_img, &channels);
if (channels.I() == 1)
{
HalconCpp::HTuple pointer;
GetImagePointer1(H_img, &pointer, nullptr, &w, &h);
int width = w.I(), height = h.I();
int size = width * height;
cv_img = cv::Mat::zeros(height, width, CV_8UC1);
memcpy(cv_img.data, (void*)(pointer.L()), size);
}
else if (channels.I() == 3)
{
HalconCpp::HTuple pointerR, pointerG, pointerB;
HalconCpp::GetImagePointer3(H_img, &pointerR, &pointerG, &pointerB, nullptr, &w, &h);
int width = w.I(), height = h.I();
int size = width * height;
cv_img = cv::Mat::zeros(height, width, CV_8UC3);
uchar* R = (uchar*)(pointerR.L());
uchar* G = (uchar*)(pointerG.L());
uchar* B = (uchar*)(pointerB.L());
for (int i = 0; i < height; ++i)
{
uchar *p = cv_img.ptr<uchar>(i);
for (int j = 0; j < width; ++j)
{
p[3 * j] = B[i * width + j];
p[3 * j + 1] = G[i * width + j];
p[3 * j + 2] = R[i * width + j];
}
}
}
return cv_img;
}
HalconCpp::HObject MatToHImage(cv::Mat& cv_img)
{
HalconCpp::HObject H_img;
if (cv_img.channels() == 1)
{
int height = cv_img.rows, width = cv_img.cols;
int size = height * width;
uchar *temp = new uchar[size];
memcpy(temp, cv_img.data, size);
HalconCpp::GenImage1(&H_img, "byte", width, height, (Hlong)(temp));
delete[] temp;
}
else if (cv_img.channels() == 3)
{
int height = cv_img.rows, width = cv_img.cols;
int size = height * width;
uchar *B = new uchar[size];
uchar *G = new uchar[size];
uchar *R = new uchar[size];
for (int i = 0; i < height; i++)
{
uchar *p = cv_img.ptr<uchar>(i);
for (int j = 0; j < width; j++)
{
B[i * width + j] = p[3 * j];
G[i * width + j] = p[3 * j + 1];
R[i * width + j] = p[3 * j + 2];
}
}
HalconCpp::GenImage3(&H_img, "byte", width, height, (Hlong)(R), (Hlong)(G), (Hlong)(B));
delete[] R;
delete[] G;
delete[] B;
}
return H_img;
}
int main()
{
HalconCpp::HImage H_img("claudia"); //从halcon的自带资源中读入一张图片,用于测试3通道图的转换时间
HalconCpp::HImage H_gray;
HalconCpp::Rgb1ToGray(H_img, &H_gray); //转灰度图,可用于测试单通道图的转换时间
double start, seconds = 0;
start = (double)cv::getTickCount(); //计时开始
cv::Mat cv_img = HImageToMat(H_gray);
seconds = ((double)cv::getTickCount() - start) / cv::getTickFrequency() * 1000;
cout << "HImage to Mat time: " << seconds << " ms" << endl;
start = (double)cv::getTickCount(); //计时开始
HalconCpp::HImage H_img_new = MatToHImage(cv_img);
seconds = ((double)cv::getTickCount() - start) / cv::getTickFrequency() * 1000;
cout << "Mat to HImage time: " << seconds << " ms" << endl;
Hlong width, height;
//显示初始图片,HImage类型
H_img.GetImageSize(&width, &height);
HalconCpp::HWindow w1(0, 0, width, height);
H_img.DispObj(w1);
//显示从HImage转到Mat的图片
cv::imshow("opencv_img", cv_img);
//显示从Mat转到HImage的图片
H_img_new.GetImageSize(&width, &height);
HalconCpp::HWindow w2(0, 0, width, height);
H_img_new.DispObj(w2);
w2.Click();
w2.ClearWindow();
}
网友评论