-
Notifications
You must be signed in to change notification settings - Fork 0
/
opencv2.cpp
35 lines (25 loc) · 807 Bytes
/
opencv2.cpp
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
#include "opencv2.h"
using namespace byteimage;
cv::Mat byteimage::toMat(const ByteImage& img) {
if (img.nchannels != 3) return toMat(img.toColor());
std::vector<cv::Mat> mat(3);
for (int ch = 0; ch < 3; ch++) {
mat[2 - ch] = cv::Mat(img.nr, img.nc, CV_8UC1);
for (int r = 0; r < img.nr; r++)
for (int c = 0; c < img.nc; c++)
mat[2 - ch].at<unsigned char>(r, c) = img.at(r, c, ch);
}
cv::Mat mimg;
merge(mat, mimg);
return mimg;
}
ByteImage byteimage::toByteImage(const cv::Mat& mat) {
ByteImage img(mat.rows, mat.cols, 3);
cv::Mat channels[3];
cv::split(mat, channels);
for (int ch = 0; ch < 3; ch++)
for (int r = 0; r < img.nr; r++)
for (int c = 0; c < img.nc; c++)
img.at(r, c, ch) = channels[2 - ch].at<unsigned char>(r, c);
return img;
}