题1
#include <GL/freeglut.h>
#include<stdio.h>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
GLint winWidth = 400, winHeight =400 ;
GLfloat x0 = 0.0, yy = 0.0, z0 = 5.0;
GLfloat xref = 0.0, yref = 0.0, zref = 0.0;
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;
GLfloat xwMin = -1.0, ywMin = -1.0, xwMax = 1.0, ywMax = 1.0;
GLfloat dnear = 1.5, dfar = 20.0;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(x0, yy, z0, xref, yref, zref, Vx, Vy, Vz);
glPushMatrix();
glTranslatef(-2.0f,0.0f,0.0f);
glColor3f (0.0, 0.0, 1.0);
glutSolidCube (1.0);
glPopMatrix();
glPushMatrix();
glColor3f (1.0, 0.0, 0.0);
glutWireCube (1.0);
glPopMatrix();
glPushMatrix();
glTranslatef(2.0f,0.0f,0.0f);
glColor3f (0.0, 1.0, 0.0);
glLineWidth (2.0);
glutWireCube (1.0);
glPopMatrix();
glFlush();
}
void reshape(GLint newWidth, GLint newHeight)
{
glViewport(0, 0, newWidth, newHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar);
glMatrixMode(GL_MODELVIEW);
winWidth = newWidth;
winHeight = newHeight;
}
int main(int argc, char* argv[])
{
GLubyte* pPixelData = (GLubyte*)malloc(800 * 400 * 3);
GLint viewport[4] = { 0 };
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize( 400 , 400 );
glutCreateWindow("三维观察");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoopEvent();
glReadBuffer(GL_FRONT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glGetIntegerv(GL_VIEWPORT, viewport);
glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
cv::Mat img;
std::vector<cv::Mat> imgPlanes;
img.create(400, 400, CV_8UC3);
cv::split(img, imgPlanes);
for (int i = 0; i < 400; i++) {
unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);
unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);
unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);
for (int j = 0; j < 400; j++) {
int k = 3 * (i * 400 + j);
plane2Ptr[j] = pPixelData[k];
plane1Ptr[j] = pPixelData[k + 1];
plane0Ptr[j] = pPixelData[k + 2];
}
}
cv::merge(imgPlanes, img);
cv::flip(img, img, 0);
cv::namedWindow("openglGrab");
cv::imshow("openglGrab", img);
cv::imwrite("../img_step1/test.jpg", img);
return 0;
}
题2
#include <GL/freeglut.h>
#include<stdio.h>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
GLint winWidth = 400, winHeight =400 ;
GLfloat x0 = 0.0, yy = 0.0, z0 = 5.0;
GLfloat xref = 0.0, yref = 0.0, zref = 0.0;
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;
GLfloat xwMin = -1.0, ywMin = -1.0, xwMax = 1.0, ywMax = 1.0;
GLfloat dnear = 1.5, dfar = 20.0;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(1.0, 1.5, 8.0, 0, 0, 0, 0, 1, 0);
glPushMatrix();
glTranslatef(-2.0f,0.0f,0.0f);
glColor3f (0.0, 0.0, 1.0);
glutSolidCube (1.0);
glPopMatrix();
glPushMatrix();
glColor3f (1.0, 0.0, 0.0);
glutWireCube (1.0);
glPopMatrix();
glPushMatrix();
glTranslatef(2.0f,0.0f,0.0f);
glColor3f (0.0, 1.0, 0.0);
glLineWidth (2.0);
glutWireCube (1.0);
glPopMatrix();
glFlush();
}
void reshape(GLint newWidth, GLint newHeight)
{
glViewport(0, 0, newWidth, newHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,1,1,100);
glMatrixMode(GL_MODELVIEW);
winWidth = newWidth;
winHeight = newHeight;
}
int main(int argc, char* argv[])
{
GLubyte* pPixelData = (GLubyte*)malloc(800 * 400 * 3);
GLint viewport[4] = { 0 };
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize( 400 , 400 );
glutCreateWindow("三维观察");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoopEvent();
glReadBuffer(GL_FRONT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glGetIntegerv(GL_VIEWPORT, viewport);
glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
cv::Mat img;
std::vector<cv::Mat> imgPlanes;
img.create(400, 400, CV_8UC3);
cv::split(img, imgPlanes);
for (int i = 0; i < 400; i++) {
unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);
unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);
unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);
for (int j = 0; j < 400; j++) {
int k = 3 * (i * 400 + j);
plane2Ptr[j] = pPixelData[k];
plane1Ptr[j] = pPixelData[k + 1];
plane0Ptr[j] = pPixelData[k + 2];
}
}
cv::merge(imgPlanes, img);
cv::flip(img, img, 0);
cv::namedWindow("openglGrab");
cv::imshow("openglGrab", img);
cv::imwrite("../img_step2/test.jpg", img);
return 0;
}
题3
#include <GL/freeglut.h>
#include<stdio.h>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
GLint winWidth = 400, winHeight =400 ;
GLfloat x0 = 0.0, yy = 0.0, z0 = 5.0;
GLfloat xref = 0.0, yref = 0.0, zref = 0.0;
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;
GLfloat xwMin = -1.0, ywMin = -1.0, xwMax = 1.0, ywMax = 1.0;
GLfloat dnear = 1.5, dfar = 20.0;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(x0, yy, z0, xref, yref, zref, Vx, Vy, Vz);
glPushMatrix();
glTranslatef(-2.0f,0.0f,0.0f);
glColor3f (0.0, 0.0, 1.0);
glutSolidCube (1.0);
glPopMatrix();
glPushMatrix();
glColor3f (1.0, 0.0, 0.0);
glutWireCube (1.0);
glPopMatrix();
glPushMatrix();
glTranslatef(2.0f,0.0f,0.0f);
glRotatef(30.0,1.0,0.0,0.0);
glColor3f (0.0, 1.0, 0.0);
glLineWidth (2.0);
glutWireCube (1.0);
glPopMatrix();
glFlush();
}
void reshape(GLint newWidth, GLint newHeight)
{
glViewport(0, 0, newWidth, newHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar);
glMatrixMode(GL_MODELVIEW);
winWidth = newWidth;
winHeight = newHeight;
}
int main(int argc, char* argv[])
{
GLubyte* pPixelData = (GLubyte*)malloc(800 * 400 * 3);
GLint viewport[4] = { 0 };
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize( 400 , 400 );
glutCreateWindow("三维观察");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoopEvent();
glReadBuffer(GL_FRONT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glGetIntegerv(GL_VIEWPORT, viewport);
glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
cv::Mat img;
std::vector<cv::Mat> imgPlanes;
img.create(400, 400, CV_8UC3);
cv::split(img, imgPlanes);
for (int i = 0; i < 400; i++) {
unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);
unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);
unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);
for (int j = 0; j < 400; j++) {
int k = 3 * (i * 400 + j);
plane2Ptr[j] = pPixelData[k];
plane1Ptr[j] = pPixelData[k + 1];
plane0Ptr[j] = pPixelData[k + 2];
}
}
cv::merge(imgPlanes, img);
cv::flip(img, img, 0);
cv::namedWindow("openglGrab");
cv::imshow("openglGrab", img);
cv::imwrite("../img_step4/test.jpg", img);
return 0;
}
题4
#include <GL/freeglut.h>
#include<stdio.h>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
GLint winWidth = 400, winHeight =400 ;
GLfloat x0 = 0.0, yy = 0.0, z0 = 5.0;
GLfloat xref = 0.0, yref = 0.0, zref = 0.0;
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;
GLfloat xwMin = -1.0, ywMin = -1.0, xwMax = 1.0, ywMax = 1.0;
GLfloat dnear = 1.5, dfar = 20.0;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(x0, yy, z0, xref, yref, zref, Vx, Vy, Vz);
glPushMatrix();
glTranslatef(-2.0f,0.0f,0.0f);
glColor3f (0.0, 0.0, 1.0);
glutSolidCube (1.0);
glPopMatrix();
glPushMatrix();
glColor3f (1.0, 0.0, 0.0);
glutWireCube (1.0);
glPopMatrix();
glPushMatrix();
glTranslatef(2.0f,0.0f,0.0f);
glColor3f (0.0, 1.0, 0.0);
glLineWidth (2.0);
glutWireCube (1.0);
glPopMatrix();
glFlush();
}
void reshape(GLint newWidth, GLint newHeight)
{
glViewport(0, 0, newWidth, newHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( -3.0, 3.0, -3.0, 3.0,-100.0 , 100.0);
glMatrixMode(GL_MODELVIEW);
winWidth = newWidth;
winHeight = newHeight;
}
int main(int argc, char* argv[])
{
GLubyte* pPixelData = (GLubyte*)malloc(800 * 400 * 3);
GLint viewport[4] = { 0 };
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize( 400 , 400 );
glutCreateWindow("三维观察");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoopEvent();
glReadBuffer(GL_FRONT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glGetIntegerv(GL_VIEWPORT, viewport);
glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
cv::Mat img;
std::vector<cv::Mat> imgPlanes;
img.create(400, 400, CV_8UC3);
cv::split(img, imgPlanes);
for (int i = 0; i < 400; i++) {
unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);
unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);
unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);
for (int j = 0; j < 400; j++) {
int k = 3 * (i * 400 + j);
plane2Ptr[j] = pPixelData[k];
plane1Ptr[j] = pPixelData[k + 1];
plane0Ptr[j] = pPixelData[k + 2];
}
}
cv::merge(imgPlanes, img);
cv::flip(img, img, 0);
cv::namedWindow("openglGrab");
cv::imshow("openglGrab", img);
cv::imwrite("../img_step3/test.jpg", img);
return 0;
}
题5
#include <GL/freeglut.h>
#include<stdio.h>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
GLint winWidth = 800, winHeight = 400;
GLfloat x0 = 0.0, yy = 0.0, z0 = 5.0;
GLfloat xref = 0.0, yref = 0.0, zref = 0.0;
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;
GLfloat xwMin = -1.0, ywMin = -1.0, xwMax = 1.0, ywMax = 1.0;
GLfloat dnear = 1.5, dfar = 20.0;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(x0, yy, z0, xref, yref, zref, Vx, Vy, Vz);
glPushMatrix();
glTranslatef(-2.0f,0.0f,0.0f);
glColor3f (0.0, 0.0, 1.0);
glutSolidCube (1.0);
glPopMatrix();
glPushMatrix();
glColor3f (1.0, 0.0, 0.0);
glutWireCube (1.0);
glPopMatrix();
glPushMatrix();
glTranslatef(2.0f,0.0f,0.0f);
glColor3f (0.0, 1.0, 0.0);
glLineWidth (2.0);
glutWireCube (1.0);
glPopMatrix();
glFlush();
}
void reshape(GLint newWidth, GLint newHeight)
{
glViewport(0, 0, newWidth, newHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 45,2 ,1 ,100 );
glMatrixMode(GL_MODELVIEW);
winWidth = newWidth;
winHeight = newHeight;
}
int main(int argc, char* argv[])
{
GLubyte* pPixelData = (GLubyte*)malloc(800 * 400 * 3);
GLint viewport[4] = { 0 };
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize( 800, 400 );
glutCreateWindow("三维观察");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoopEvent();
glReadBuffer(GL_FRONT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glGetIntegerv(GL_VIEWPORT, viewport);
glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
cv::Mat img;
std::vector<cv::Mat> imgPlanes;
img.create(400, 800, CV_8UC3);
cv::split(img, imgPlanes);
for (int i = 0; i < 400; i++) {
unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);
unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);
unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);
for (int j = 0; j < 800; j++) {
int k = 3 * (i * 800 + j);
plane2Ptr[j] = pPixelData[k];
plane1Ptr[j] = pPixelData[k + 1];
plane0Ptr[j] = pPixelData[k + 2];
}
}
cv::merge(imgPlanes, img);
cv::flip(img, img, 0);
cv::namedWindow("openglGrab");
cv::imshow("openglGrab", img);
cv::imwrite("../img_step5/test.jpg", img);
return 0;
}