opengl_opencv_example/main.cpp

138 lines
4.4 KiB
C++
Raw Normal View History

#include <opencv2/core.hpp>
#include <opencv2/core/opengl.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/gl3.h>
#include <GLUT/glut.h>
#else
#include <GL/glew.h>
#include <GL/glut.h>
#endif
using namespace cv;
using namespace std;
2024-04-01 18:00:22 +00:00
using namespace std::chrono;
using time_stamp = time_point<system_clock, microseconds>;
GLuint imageTexture;
GLint screenWidth = 0;
GLint screenHeight = 0;
2024-04-01 18:00:22 +00:00
time_stamp ts;
/**
* rows = height
* cols = width
**/
void loadTexture (GLuint& imageTexture1)
{
string image_path = samples::findFile("chart.png");
Mat loaded = imread(image_path, IMREAD_COLOR);
Mat image = Mat::zeros(screenHeight, screenWidth, CV_8UC3);
//Mat resized = Mat::zeros(1080, 1280, CV_8UC3)
cout << "created image" << endl;
if (image.empty()) {
cout << "image empty" << endl;
} else {
cout << "loaded " << image_path << endl;
cout << " dim " << loaded.cols << "x" << loaded.rows << endl;
//resize(loaded, resized, Size(1280, 1080));
cout << " image " << image.cols << "x" << image.rows << endl;
//cout << "resized " << resized.cols << "x" << resized.rows << endl;
//resized.copyTo(image(Rect(0, 0, resized.cols, resized.rows)));
uint32_t offsetX = (image.cols - loaded.cols) >> 1;
uint32_t offsetY = (image.rows - loaded.rows) >> 1;
loaded.copyTo(image(Rect(offsetX, offsetY, loaded.cols, loaded.rows)));
flip(image, image, 0);
#if (CV_VERSION_MAJOR >= 4)
cvtColor(image, image, cv::COLOR_BGR2RGB);
#else
cvtColor(image, image, CV_BGR2RGB);
#endif
glGenTextures(1, &imageTexture1);
glBindTexture(GL_TEXTURE_2D, imageTexture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, // Type of texture
0, // Pyramid level (for mip-mapping) - 0 is the top level
GL_RGB, // Internal colour format to convert to
image.cols, // Image width i.e. 640 for Kinect in standard mode
image.rows, // Image height i.e. 480 for Kinect in standard mode
0, // Border width in pixels (can either be 1 or 0)
GL_RGB, // Input image format (i.e. GL_RGB, GL_RGBA, GL_BGR etc.)
GL_UNSIGNED_BYTE, // Image data type
image.ptr()); // The actual image data itself
cout << "display()" << endl;
}
}
void getScreenDimensions ()
{
if (screenWidth == 0 && screenHeight == 0) {
GLint dims[4] = {0};
glGetIntegerv(GL_VIEWPORT, dims);
screenWidth = dims[2];
screenHeight = dims[3];
cout << screenWidth << "," << screenHeight << endl;
}
2024-04-01 18:00:22 +00:00
}
void display(void)
2024-04-01 18:00:22 +00:00
{
getScreenDimensions();
loadTexture(imageTexture);
ts = time_point_cast<microseconds>(system_clock::now());
2024-04-01 15:00:11 +00:00
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glViewport(0, 0, screenWidth, screenHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glBindTexture(GL_TEXTURE_2D, imageTexture);
glBegin(GL_QUADS); // front face
2024-04-01 22:06:49 +00:00
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f); //bottom right
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); //top right
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f); //top left
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); //bottom left
2024-04-01 15:00:11 +00:00
glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glutSwapBuffers();
2024-04-01 15:00:11 +00:00
//glFlush();
}
int main(int argc, char** argv)
{
if (argc > 1)
{
screenWidth = atoi(argv[1]);
cout << "Set width: " << screenWidth << endl;
}
if (argc > 2)
{
screenHeight = atoi(argv[2]);
cout << "Set height: " << screenHeight << endl;
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
2024-04-01 15:00:11 +00:00
glutCreateWindow("opengl_opencv_example");
glutFullScreen();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}