网站首页 > 技术文章 正文
原题是:
5. Build and run the rotating cube described in Example 9-12. Modify it so that you
have buttons: rotate right, left, up, and down. When you press the buttons, the
cube should rotate.
题中说Example 9-12有相应的描述可能是印错了,例题Example 9-4倒是用得上。
根据题意加上本章的内容,可以用opengl画出立方体,用opencv显示图像。Linux系统IDE用Qt。按钮用opencv的,不是用Qt的。原书224-227页window properties小节以下章节及图Fig9-5介绍了opencv按钮的使用。
由于立方体显示在opencv图像中只能用"WINDOW_OPENGL"建窗口,这时窗口没有窗口属性按钮,而opencv按钮显示在窗口属性之下,所以"WINDOW_AUTOSIZE"或者"WINDOW_NORMAL"再建一个opencv窗口。现在可以用QtCreator建一个控制台应用项目了。具体的略过。不过配置文件截图供参考。
源码如下:
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <opencv2/core/opengl.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
void callbackButton(int state, void* userdata); // 左转回调函数
void callbackButton2(int state, void* userdata); // 右转回调函数
void callbackButton3(int state, void* userdata); // 上移回调函数
void callbackButton4(int state, void* userdata); // 下移回调函数
void on_opengl( void* param ); // opengl回调函数
void help(char *argv[])
{
cout << " Exercise 9-5. Using buttons to move a opengl cube that can rotate left,"
<< "\n rotate right, move upwards or move downwards on an opengl image."
<< "\n By this way, to learn how to use window properties."
<< "\n\nCall: " << argv[0] << " ../dialtar.jpg" << "../mango.jpg" << endl;
}
float rota = 15.0; // cube旋转的角度
float mv = -0.5; // cube上下移动的距离
int state = 0; // -1,0,1三种状态
int main(int argc, char *argv[])
{
if (argc < 3)
{
cout << "Parameters are less three, exit" << endl;
exit(1);
}
void help(int argc, char *argv);
Mat image, image2;
image = imread(argv[1], 1); // Read the opengl image, "../dialtar.jpg"
if (image.empty()) // Check for invalid input
{
cout << "Could not open or find the gl image" << endl;
return -1;
}
image2 = imread(argv[2], 1); // Read buttons to be operated image, "../mango.jpg"
if (image2.empty()) // Check for invalid input
{
cout << "Could not open or find the go image2" << endl;
return -1;
}
cv::resize(image2, image2, cv::Size(image2.cols / 3, image2.rows / 3)); // 调整图像大小
namedWindow("Exercise 9-5", WINDOW_OPENGL); // Create a window for opengl display.
cv::resizeWindow( "Exercise 9-5", image.cols, image.rows );
namedWindow("Exercise 9-5 Go", WINDOW_AUTOSIZE | WINDOW_NORMAL); // Create a window for buttons to operate
createButton("左旋", callbackButton, NULL, QT_PUSH_BUTTON, 1); // 创建按钮
createButton("右旋", callbackButton2, NULL, QT_PUSH_BUTTON, 1);
createButton("上移", callbackButton3, NULL, QT_PUSH_BUTTON, 1);
createButton("下移", callbackButton4, NULL, QT_PUSH_BUTTON, 1);
cv::ogl::Texture2D backgroundTex(image);
cv::setOpenGlDrawCallback( "Exercise 9-5", on_opengl, &backgroundTex );
cv::updateWindow( "Exercise 9-5" );
imshow("Exercise 9-5 Go", image2); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
cv::setOpenGlDrawCallback( "Exercise 9-5", 0, 0 );
cv::destroyAllWindows();
return 0;
}
运行结果:
图3箭头所指是窗口属性按钮,点它显示opencv按钮,下面就可以检验程序了。
猜你喜欢
- 2024-09-30 OPENCV-python 第一天 python opencv教程
- 2024-09-30 Python帮您十步搞定人脸检测 人脸检测 python
- 2024-09-30 OpenCV学习笔记(一)之图像金字塔-上采样与降采样与DOG
- 2024-09-30 Ubuntu18.04LTS下OpenCV的配置 ubuntu opencv4
- 2024-09-30 计算机视觉之Opencv(1)——基本操作
- 2024-09-30 OpenCV系列教程_03 opencv官方教程
- 2024-09-30 CV之 HOG特征描述算子-行人检测 卜算子 黄州定慧院寓居作
- 2024-09-30 OpenCV SURF特征点检测和匹配 opencv特征提取方法
- 2024-09-30 Opencv从零开始 - [启蒙篇] - 读取、几何变换
- 2024-09-30 密码忘记了?没事,我早就用Python给你监听了
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- oraclesql优化 (66)
- 类的加载机制 (75)
- feignclient (62)
- 一致性hash算法 (71)
- dockfile (66)
- 锁机制 (57)
- javaresponse (60)
- 查看hive版本 (59)
- phpworkerman (57)
- spark算子 (58)
- vue双向绑定的原理 (68)
- springbootget请求 (58)
- docker网络三种模式 (67)
- spring控制反转 (71)
- data:image/jpeg (69)
- base64 (69)
- java分页 (64)
- kibanadocker (60)
- qabstracttablemodel (62)
- java生成pdf文件 (69)
- deletelater (62)
- com.aspose.words (58)
- android.mk (62)
- qopengl (73)
- epoch_millis (61)
本文暂时没有评论,来添加一个吧(●'◡'●)