-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimageutils.h
118 lines (87 loc) · 2.52 KB
/
imageutils.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
#include <opencv/cv.h>
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include <GLFW/glfw3.h>
#include<string>
#include <sstream>
#include <iostream>
using namespace cv;
using namespace std;
struct OpenCVImage
{
GLuint texture;
cv::Mat mat;
bool open;
string name;
OpenCVImage() {
texture = 0;
}
void LoadCVMat(cv::Mat frame, bool change = true) {
if (frame.empty())
return;
if (!texture)
{
mat = frame; // maybe i should copy that frame (clone it)
if (change)
cv::cvtColor(mat, mat, CV_BGR2RGB);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Set texture clamping method
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, mat.cols, mat.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, mat.ptr());
}else
cout << "We HAVE SOMETHING AT TEXTURE" << "\n";
}
void UpdateMat(cv::Mat& frame, bool change_color_order = false) {
// if does not have the same size then i need to recreate the texture from scratch
//could be something like
if (!(frame.size() == mat.size())) {
Clear();
mat.release(); // maybe this is redundant
LoadCVMat(frame);
}
if (change_color_order)
cv::cvtColor(frame, frame, CV_BGR2RGB);
// image must have same size
glBindTexture(GL_TEXTURE_2D, texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, frame.cols, frame.rows, GL_RGB,
GL_UNSIGNED_BYTE, frame.ptr());
}
// clear texture and realease all memory associated with it
void Clear() { glDeleteTextures(1, &texture); texture = 0; }
GLuint * getTexture() { return &texture; }
// return mat if is not empty otherwise return null
cv::Mat* GetMat() {
if (!mat.empty())
{
if (mat.data)
return &mat;
else
return NULL;
} else
return NULL;
}
// this function could be omitted using the above function
// but we had to change the use of it ( the above)
cv::Mat* GetMatDirection() {
return &mat;
}
void switchOpen(){
if (open)
open = false;
else
open = true;
}
bool * getOpen() { return &open; }
void SetName(const char* nme) {
name = std::string(nme);
}
//then GetName
const char * GetName(){
return name.c_str();
}
};