1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include<stdio.h> #include<opencv2/opencv.hpp> #include "HalconCpp.h" #include <string>using namespace std; using namespace cv; 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); cout << "Gray" << endl; } 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]; } } cout << "RGB" << endl; } return cv_img; } int main() { string data_path = "1.jpg"; HalconCpp::HImage Image(data_path.c_str()), ImageGray; Rgb1ToGray(Image, &ImageGray); Mat cv_img_gray = HImageToMat(ImageGray); Mat cv_img_rgb = HImageToMat(Image); return 0; }
|