forked from carlren/ORUtils
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImage2cvMat.h
169 lines (156 loc) · 6.61 KB
/
Image2cvMat.h
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#pragma once
#ifdef COMPILE_WITH_OPENCV
#include "Image.h"
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
namespace ORUtils {
class Image2CVShow{
public:
static void show(ORUtils::Image<float> *img, cv::Mat **mat) {
(*mat)->create(img->noDims.y, img->noDims.x, CV_32FC1);
for (int i = 0; i < img->noDims.y; ++i) {
for (int j = 0; j < img->noDims.x; ++j) {
(*mat)->at<float>(i, j) = img->GetDataConst(MEMORYDEVICE_CPU)[i * img->noDims.x + j];
}
}
double min;
double max;
cv::minMaxIdx(*(*mat), &min, &max);
cv::convertScaleAbs(*(*mat), *(*mat), 255 / max);
}
static void show(ORUtils::Image<float> *img, const std::string &name = "image") {
cv::Mat mat(img->noDims.y, img->noDims.x, CV_32FC1);
for (int i = 0; i < img->noDims.y; ++i) {
for (int j = 0; j < img->noDims.x; ++j) {
mat.at<float>(i, j) = img->GetDataConst(MEMORYDEVICE_CPU)[i * img->noDims.x + j];
}
}
double min;
double max;
cv::minMaxIdx(mat, &min, &max);
cv::convertScaleAbs(mat, mat, 255 / max);
imshow(name, mat);
cv::waitKey(0);
}
static void show(ORUtils::Image<unsigned short> *img, const std::string &name = "image") {
cv::Mat mat(img->noDims.y, img->noDims.x, CV_32FC1);
for (int i = 0; i < img->noDims.y; ++i) {
for (int j = 0; j < img->noDims.x; ++j) {
mat.at<float>(i, j) = img->GetDataConst(MEMORYDEVICE_CPU)[i * img->noDims.x + j];
}
}
double min;
double max;
cv::minMaxIdx(mat, &min, &max);
cv::convertScaleAbs(mat, mat, 255 / max);
imshow(name, mat);
cv::waitKey(0);
}
static void show(ORUtils::Image<Vector3<float>> *img, const std::string &name = "image") {
cv::Mat channels[3];
for (size_t c = 0; c < 3; ++c) {
cv::Mat &mat = channels[c];
mat.create(img->noDims.y, img->noDims.x, CV_32FC1);
for (int i = 0; i < img->noDims.y; ++i) {
for (int j = 0; j < img->noDims.x; ++j) {
mat.at<float>(i, j) = img->GetDataConst(MEMORYDEVICE_CPU)[i * img->noDims.x + j][c];
}
}
double min;
double max;
cv::minMaxIdx(mat, &min, &max);
cv::convertScaleAbs(mat, mat, 255 / max);
}
cv::Mat output(img->noDims.y, img->noDims.x, CV_32FC3);
cv::merge(channels, 3, output);
imshow(name, output);
cv::waitKey(0);
}
static void show(ORUtils::Image<Vector4<float>> *img, const std::string &name = "image") {
cv::Mat channels[3];
for (size_t c = 0; c < 3; ++c) {
cv::Mat &mat = channels[c];
mat.create(img->noDims.y, img->noDims.x, CV_32FC1);
for (int i = 0; i < img->noDims.y; ++i) {
for (int j = 0; j < img->noDims.x; ++j) {
mat.at<float>(i, j) = img->GetDataConst(MEMORYDEVICE_CPU)[i * img->noDims.x + j][c];
}
}
double min;
double max;
cv::minMaxIdx(mat, &min, &max);
cv::convertScaleAbs(mat, mat, 255 / max);
}
cv::Mat output(img->noDims.y, img->noDims.x, CV_32FC3);
cv::merge(channels, 3, output);
imshow(name, output);
cv::waitKey(0);
}
static void show(ORUtils::Image<Vector4<unsigned char>> *img, const std::string &name = "image") {
cv::Mat mat(img->noDims.y, img->noDims.x, CV_8UC4);
for (int i = 0; i < img->noDims.y; ++i) {
for (int j = 0; j < img->noDims.x; ++j) {
for (size_t c = 0; c < 4; ++c)
mat.at<cv::Vec<uchar, 4>>(i, j)[c] = img->GetDataConst(MEMORYDEVICE_CPU)[i * img->noDims.x + j][c];
}
}
cv::cvtColor(mat, mat, cv::COLOR_BGRA2RGBA);
// double min;
// double max;
// cv::minMaxIdx(mat, &min, &max);
// cv::convertScaleAbs(mat, mat, 255 / max);
imshow(name, mat);
cv::waitKey(0);
}
static void show(const ORUtils::Image<Vector4<unsigned char >> *img, const std::string &name = "image") {
cv::Mat mat(img->noDims.y, img->noDims.x, CV_8UC4);
for (int i = 0; i < img->noDims.y; ++i) {
for (int j = 0; j < img->noDims.x; ++j) {
for (size_t c = 0; c < 4; ++c)
mat.at<cv::Vec<uchar, 4>>(i, j)[c] = img->GetDataConst(MEMORYDEVICE_CPU)[i * img->noDims.x + j][c];
}
}
double min;
double max;
cv::minMaxIdx(mat, &min, &max);
cv::convertScaleAbs(mat, mat, 255 / max);
imshow(name, mat);
cv::waitKey(0);
}
static void show(ORUtils::Image<unsigned char> *img, const std::string &name = "image") {
cv::Mat mat(img->noDims.y, img->noDims.x, CV_8UC4);
for (int i = 0; i < img->noDims.y; ++i) {
for (int j = 0; j < img->noDims.x; ++j) {
for (size_t c = 0; c < 3; ++c)
mat.at<cv::Vec<uchar, 4>>(i, j)[c] = img->GetDataConst(MEMORYDEVICE_CPU)[i * img->noDims.x + j];
mat.at<cv::Vec<uchar, 4>>(i, j)[3]=255;
}
}
double min;
double max;
cv::minMaxIdx(mat, &min, &max);
cv::convertScaleAbs(mat, mat, 255 / max);
imshow(name, mat);
cv::waitKey(0);
}
};
class Image2CvMat {
public:
static cv::Mat get(ORUtils::Image<Vector4<unsigned char>> *img) {
cv::Mat mat(img->noDims.y, img->noDims.x, CV_8UC4);
for (int i = 0; i < img->noDims.y; ++i) {
for (int j = 0; j < img->noDims.x; ++j) {
for (size_t c = 0; c < 4; ++c)
mat.at<cv::Vec<uchar, 4>>(i, j)[c] = img->GetDataConst(MEMORYDEVICE_CPU)[i * img->noDims.x + j][c];
}
}
// double min;
// double max;
// cv::minMaxIdx(mat, &min, &max);
// cv::convertScaleAbs(mat, mat, 255 / max);
return mat;
}
};
}
#endif