1### opencv to ncnn 2 3* cv::Mat CV_8UC3 -> ncnn::Mat 3 channel + swap RGB/BGR 4 5```cpp 6// cv::Mat a(h, w, CV_8UC3); 7ncnn::Mat in = ncnn::Mat::from_pixels(a.data, ncnn::Mat::PIXEL_BGR2RGB, a.cols, a.rows); 8``` 9 10* cv::Mat CV_8UC3 -> ncnn::Mat 3 channel + keep RGB/BGR order 11 12```cpp 13// cv::Mat a(h, w, CV_8UC3); 14ncnn::Mat in = ncnn::Mat::from_pixels(a.data, ncnn::Mat::PIXEL_RGB, a.cols, a.rows); 15``` 16 17* cv::Mat CV_8UC3 -> ncnn::Mat 1 channel + do RGB2GRAY/BGR2GRAY 18 19```cpp 20// cv::Mat rgb(h, w, CV_8UC3); 21ncnn::Mat inrgb = ncnn::Mat::from_pixels(rgb.data, ncnn::Mat::PIXEL_RGB2GRAY, rgb.cols, rgb.rows); 22 23// cv::Mat bgr(h, w, CV_8UC3); 24ncnn::Mat inbgr = ncnn::Mat::from_pixels(bgr.data, ncnn::Mat::PIXEL_BGR2GRAY, bgr.cols, bgr.rows); 25``` 26 27* cv::Mat CV_8UC1 -> ncnn::Mat 1 channel 28 29```cpp 30// cv::Mat a(h, w, CV_8UC1); 31ncnn::Mat in = ncnn::Mat::from_pixels(a.data, ncnn::Mat::PIXEL_GRAY, a.cols, a.rows); 32``` 33 34* cv::Mat CV_32FC1 -> ncnn::Mat 1 channel 35 36 * **You could construct ncnn::Mat and fill data into it directly to avoid data copy** 37 38```cpp 39// cv::Mat a(h, w, CV_32FC1); 40ncnn::Mat in(a.cols, a.rows, 1, (void*)a.data); 41in = in.clone(); 42``` 43 44* cv::Mat CV_32FC3 -> ncnn::Mat 3 channel 45 46 * **You could construct ncnn::Mat and fill data into it directly to avoid data copy** 47 48```cpp 49// cv::Mat a(h, w, CV_32FC3); 50ncnn::Mat in_pack3(a.cols, a.rows, 1, (void*)a.data, (size_t)4u * 3, 3); 51ncnn::Mat in; 52ncnn::convert_packing(in_pack3, in, 1); 53``` 54 55* std::vector < cv::Mat > + CV_32FC1 -> ncnn::Mat multiple channels 56 57 * **You could construct ncnn::Mat and fill data into it directly to avoid data copy** 58 59```cpp 60// std::vector<cv::Mat> a(channels, cv::Mat(h, w, CV_32FC1)); 61int channels = a.size(); 62ncnn::Mat in(a[0].cols, a[0].rows, channels); 63for (int p=0; p<in.c; p++) 64{ 65 memcpy(in.channel(p), (const uchar*)a[p].data, in.w * in.h * sizeof(float)); 66} 67``` 68 69### ncnn to opencv 70 71* ncnn::Mat 3 channel -> cv::Mat CV_8UC3 + swap RGB/BGR 72 73 * **You may need to call in.substract_mean_normalize() first to scale values from 0..1 to 0..255** 74 75```cpp 76// ncnn::Mat in(w, h, 3); 77cv::Mat a(in.h, in.w, CV_8UC3); 78in.to_pixels(a.data, ncnn::Mat::PIXEL_BGR2RGB); 79``` 80 81* ncnn::Mat 3 channel -> cv::Mat CV_8UC3 + keep RGB/BGR order 82 83 * **You may need to call in.substract_mean_normalize() first to scale values from 0..1 to 0..255** 84 85```cpp 86// ncnn::Mat in(w, h, 3); 87cv::Mat a(in.h, in.w, CV_8UC3); 88in.to_pixels(a.data, ncnn::Mat::PIXEL_RGB); 89``` 90 91* ncnn::Mat 1 channel -> cv::Mat CV_8UC1 92 93 * **You may need to call in.substract_mean_normalize() first to scale values from 0..1 to 0..255** 94 95```cpp 96// ncnn::Mat in(w, h, 1); 97cv::Mat a(in.h, in.w, CV_8UC1); 98in.to_pixels(a.data, ncnn::Mat::PIXEL_GRAY); 99``` 100 101* ncnn::Mat 1 channel -> cv::Mat CV_32FC1 102 103 * **You could consume or manipulate ncnn::Mat data directly to avoid data copy** 104 105```cpp 106// ncnn::Mat in; 107cv::Mat a(in.h, in.w, CV_32FC1); 108memcpy((uchar*)a.data, in.data, in.w * in.h * sizeof(float)); 109``` 110 111* ncnn::Mat 3 channel -> cv::Mat CV_32FC3 112 113 * **You could consume or manipulate ncnn::Mat data directly to avoid data copy** 114 115```cpp 116// ncnn::Mat in(w, h, 3); 117ncnn::Mat in_pack3; 118ncnn::convert_packing(in, in_pack3, 3); 119cv::Mat a(in.h, in.w, CV_32FC3); 120memcpy((uchar*)a.data, in_pack3.data, in.w * in.h * 3 * sizeof(float)); 121``` 122 123* ncnn::Mat multiple channels -> std::vector < cv::Mat > + CV_32FC1 124 125 * **You could consume or manipulate ncnn::Mat data directly to avoid data copy** 126 127```cpp 128// ncnn::Mat in(w, h, channels); 129std::vector<cv::Mat> a(in.c); 130for (int p=0; p<in.c; p++) 131{ 132 a[p] = cv::Mat(in.h, in.w, CV_32FC1); 133 memcpy((uchar*)a[p].data, in.channel(p), in.w * in.h * sizeof(float)); 134} 135``` 136