-
Notifications
You must be signed in to change notification settings - Fork 1
/
texture2grayimageconvertor.cpp
229 lines (218 loc) · 10.4 KB
/
texture2grayimageconvertor.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "texture2grayimageconvertor.h"
#include <cmath>
#include <QtMath>
#include <QMap>
#include <opencv2/imgproc.hpp>
#include "gl/gl_view.h"
Texture2GrayImageConvertor::Texture2GrayImageConvertor()
{
QSharedPointer<QOpenGLShaderProgram> programPtr = GL_ViewRenderer::loadShader(":/shaders/texture.vsh",
":/shaders/texture.fsh");
m_materialColor = GL_ShaderMaterialPtr::create(programPtr,
QVariantMap({ { "matrixMVP", _getMatrixMVP(0, false) } }),
QMap<QString, GLuint>({ { "main_texture", 0 } }));
programPtr = GL_ViewRenderer::loadShader(":/shaders/color.vsh",
":/shaders/packed_gray_image.fsh");
m_materialPackedGray = GL_ShaderMaterialPtr::create(programPtr,
QVariantMap({ { "matrixMVP", _getMatrixMVP(0, false) },
{ "offsets[0]", 0 },
{ "offsets[1]", 1 },
{ "offsets[2]", 2 },
{ "offsets[3]", 3 } }),
QMap<QString, GLuint>({ { "main_texture", 0 } }));
m_quad = GL_MeshPtr::create(GL_Mesh::createQuad());
}
cv::Mat Texture2GrayImageConvertor::read(QOpenGLFunctions * gl,
GLuint textureId,
QSize textureSize,
const QSize & maxImageSize,
int orientation, bool flipHorizontally)
{
orientation = ((orientation / 90) % 4) * 90;
if ((orientation == 90) || (orientation == 270))
textureSize = QSize(textureSize.height(), textureSize.width());
QSize imageSize = _getImageSize(textureSize, maxImageSize, true);
if (!m_fboColor ||
(m_fboColor->width() < imageSize.width()) ||
(m_fboColor->height() < imageSize.height()))
{
m_fboColor = QSharedPointer<QOpenGLFramebufferObject>::create(imageSize);
m_fboPackedGray = QSharedPointer<QOpenGLFramebufferObject>::create(imageSize.width() / 4,
imageSize.height());
}
m_materialColor->setTexture("main_texture", textureId);
m_materialColor->setValue("matrixMVP", _getMatrixMVP(orientation, flipHorizontally));
m_fboColor->bind();
gl->glViewport(0, 0, imageSize.width(), imageSize.height());
gl->glDisable(GL_BLEND);
gl->glDisable(GL_DEPTH_TEST);
gl->glDepthMask(GL_FALSE);
m_quad->draw(gl, *m_materialColor);
/*m_fboPackedGray->bind();
gl->glViewport(0, 0, imageSize.width() / 4, imageSize.height());
gl->glEnable(GL_BLEND);
gl->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gl->glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl->glClear(GL_COLOR_BUFFER_BIT);
m_materialPackedGray->setTexture("main_texture", m_fboColor->texture());
m_materialPackedGray->setValue("offsets[3]", 30);
m_materialPackedGray->setValue("offsets[2]", 2);
m_materialPackedGray->setValue("offsets[1]", 1);
m_materialPackedGray->setValue("offsets[0]", 0);
m_quad->draw(gl, *m_materialPackedGray);
cv::Mat image(imageSize.height(), imageSize.width(), CV_8UC1);
gl->glReadPixels(0, 0, imageSize.width() / 4, imageSize.height(), GL_RGBA, GL_UNSIGNED_BYTE, image.data);*/
cv::Mat image(imageSize.height(), imageSize.width(), CV_8UC4);
gl->glReadPixels(0, 0, imageSize.width(), imageSize.height(), GL_RGBA, GL_UNSIGNED_BYTE, image.data);
cv::cvtColor(image, image, cv::COLOR_RGBA2GRAY);
gl->glDisable(GL_BLEND);
m_fboPackedGray->release();
return image;
}
std::tuple<cv::Mat, QVector2D> Texture2GrayImageConvertor::read_cropped(QOpenGLFunctions * gl,
GLuint textureId,
QSize textureSize,
const QSize & maxImageSize,
float viewAspect,
int orientation,
bool flipHorizontally)
{
orientation = ((orientation / 90) % 4) * 90;
if ((orientation == 90) || (orientation == 270))
textureSize = QSize(textureSize.height(), textureSize.width());
QSize imageSize = _getImageSize(textureSize, maxImageSize, false);
QVector2D scale(1.0f, 1.0f);
{
float width = imageSize.height() * viewAspect, height = static_cast<float>(imageSize.height());
if (width > static_cast<float>(imageSize.width()))
{
width = static_cast<float>(imageSize.width());
height = imageSize.width() / viewAspect;
}
QSize imageSize1(static_cast<int>(ceil(width)), static_cast<int>(ceil(height)));
if (imageSize1.width() % 4 != 0)
{
int w = (imageSize1.width() / 4) * 4;
float scale = w / static_cast<float>(imageSize1.width());
imageSize1 = QSize(w, static_cast<int>(imageSize1.height() * scale));
}
scale = QVector2D(imageSize1.width() / static_cast<float>(imageSize.width()),
imageSize1.height() / static_cast<float>(imageSize.height()));
imageSize = imageSize1;
if (flipHorizontally)
scale.setX(- scale.x());
}
if (!m_fboColor ||
(m_fboColor->width() < imageSize.width()) ||
(m_fboColor->height() < imageSize.height()))
{
m_fboColor = QSharedPointer<QOpenGLFramebufferObject>::create(imageSize);
m_fboPackedGray = QSharedPointer<QOpenGLFramebufferObject>::create(imageSize.width() / 4,
imageSize.height());
}
m_materialColor->setTexture("main_texture", textureId);
m_materialColor->setValue("matrixMVP", _getViewMatrix(scale.x(), scale.y()) * _getRotation(orientation));
m_fboColor->bind();
gl->glViewport(0, 0, imageSize.width(), imageSize.height());
gl->glDisable(GL_BLEND);
gl->glDisable(GL_DEPTH_TEST);
gl->glDisable(GL_CULL_FACE);
gl->glDepthMask(GL_FALSE);
m_quad->draw(gl, *m_materialColor);
/*m_fboPackedGray->bind();
gl->glViewport(0, 0, imageSize.width() / 4, imageSize.height());
gl->glEnable(GL_BLEND);
gl->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gl->glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl->glClear(GL_COLOR_BUFFER_BIT);
m_materialPackedGray->setTexture("main_texture", m_fboColor->texture());
m_materialPackedGray->setValue("offsets[0]", 0);
m_materialPackedGray->setValue("offsets[1]", 1);
m_materialPackedGray->setValue("offsets[2]", 2);
m_materialPackedGray->setValue("offsets[3]", 3);
m_quad->draw(gl, *m_materialPackedGray);
cv::Mat image(imageSize.height(), imageSize.width(), CV_8UC1);
gl->glReadPixels(0, 0, imageSize.width() / 4, imageSize.height(), GL_RGBA, GL_UNSIGNED_BYTE, image.data);*/
cv::Mat image(imageSize.height(), imageSize.width(), CV_8UC4);
gl->glReadPixels(0, 0, imageSize.width(), imageSize.height(), GL_RGBA, GL_UNSIGNED_BYTE, image.data);
cv::cvtColor(image, image, cv::COLOR_RGBA2GRAY);
gl->glDisable(GL_BLEND);
gl->glEnable(GL_CULL_FACE);
m_fboPackedGray->release();
return std::make_tuple(image, scale);
}
QSize Texture2GrayImageConvertor::_getImageSize(const QSize & textureSize,
QSize maxImageSize, bool k4fixFlag) const
{
if ((textureSize.width() < maxImageSize.width()) && (textureSize.height() < maxImageSize.height()))
{
if (!k4fixFlag || (textureSize.width() % 4 == 0))
return textureSize;
if (k4fixFlag)
maxImageSize.setWidth((maxImageSize.width() / 4) * 4);
QSize imageSize;
imageSize.setWidth((textureSize.width() / 4) * 4);
float scale = imageSize.width() / static_cast<float>(textureSize.width());
imageSize.setHeight(static_cast<int>(textureSize.height() * scale));
return imageSize;
}
float aspect = textureSize.height() / static_cast<float>(textureSize.width());
QSize imageSize(maxImageSize.width(), static_cast<int>(maxImageSize.width() * aspect));
if (imageSize.height() > maxImageSize.height())
imageSize = QSize(static_cast<int>(maxImageSize.height() / aspect), maxImageSize.height());
if (k4fixFlag)
{
int width = (imageSize.width() / 4) * 4;
float scale = width / static_cast<float>(imageSize.width());
imageSize.setWidth(width);
imageSize.setHeight(static_cast<int>(imageSize.height() * scale));
}
return imageSize;
}
QMatrix4x4 Texture2GrayImageConvertor::_getRotation(int orientation) const
{
QMatrix4x4 rotation;
switch (orientation) {
case 90:
rotation(0, 0) = 0.0f;
rotation(0, 1) = -1.0f;
rotation(0, 3) = 1.0f;
rotation(1, 0) = 1.0f;
rotation(1, 1) = 0.0f;
rotation(1, 3) = 0.0f;
break;
case 180:
rotation(0, 0) = -1.0f;
rotation(0, 1) = 0.0f;
rotation(0, 3) = 1.0f;
rotation(1, 0) = 0.0f;
rotation(1, 1) = -1.0f;
rotation(1, 3) = 1.0f;
break;
case 270:
rotation(0, 0) = 0.0f;
rotation(0, 1) = 1.0f;
rotation(0, 3) = 0.0f;
rotation(1, 0) = -1.0f;
rotation(1, 1) = 0.0f;
rotation(1, 3) = 1.0f;
break;
}
return rotation;
}
QMatrix4x4 Texture2GrayImageConvertor::_getViewMatrix(float scaleX, float scaleY) const
{
QMatrix4x4 viewMatrix;
viewMatrix(0, 0) = 2.0f / scaleX;
viewMatrix(0, 3) = - 1.0f / scaleX;
viewMatrix(1, 1) = - 2.0f / scaleY;
viewMatrix(1, 3) = 1.0f / scaleY;
viewMatrix(2, 2) = 1.0f;
viewMatrix(2, 3) = 0.0f;
viewMatrix(3, 3) = 1.0f;
return viewMatrix;
}
QMatrix4x4 Texture2GrayImageConvertor::_getMatrixMVP(int orientation, bool flipHorizontally) const
{
return _getViewMatrix(flipHorizontally ? -1.0f : 1.0f, 1.0f) * _getRotation(orientation);
}