计算机系统应用教程网站

网站首页 > 技术文章 正文

《Learning OpenCV 3》第9章课后练习题5答案

btikc 2024-09-30 13:13:29 技术文章 11 ℃ 0 评论

原题是:

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按钮,下面就可以检验程序了。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表