BS版图形系统 - OpenCV - 第3章笔记
QQ 282397369
3 学习图形用户界面
显示图像结果,处理用户与图像的交互。
3.1 技术要求
3.2 OpenCV用户界面介绍
- 基于原生用户界面的基本界面: GTK
- 基于QT库的略微更高级的界面
3.3 OpenCV的基本图形用户界面
- highgui模块
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// OpenCV includes
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
const int CV_GUI_NORMAL= 0x10;
int main( int argc, const char** argv )
{
// Read images
Mat lena= imread("../lena.jpg");
Mat photo= imread("../photo.jpg");
// Create windows
namedWindow("Lena", WINDOW_NORMAL);
// Checking if Lena image has been loaded
if (!lena.data) {
cout << "Lena image missing!" << endl;
return -1;
}
namedWindow("Photo", WINDOW_AUTOSIZE);
if (!photo.data) {
cout << "Lena image missing!" << endl;
return -1;
}
// Move window
moveWindow("Lena", 10, 10);
moveWindow("Photo", 520, 10);
// show images
imshow("Lena", lena);
imshow("Photo", photo);
// Resize window, only non autosize
resizeWindow("Lena", 512, 512);
// wait for any key press
waitKey(0);
// Destroy the windows
destroyWindow("Lena");
destroyWindow("Photo");
// Create 10 windows
for(int i =0; i< 10; i++)
{
ostringstream ss;
ss << "Photo " << i;
namedWindow(ss.str());
moveWindow(ss.str(), 20*i, 20*i);
imshow(ss.str(), photo);
}
waitKey(0);
// Destroy all windows
destroyAllWindows();
return 0;
}
- 将滑块和鼠标事件添加到界面
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// OpenCV includes
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
// Create a variable to save the position value in track
int blurAmount=15;
// Trackbar call back function
static void onChange(int pos, void* userInput);
//Mouse callback
static void onMouse( int event, int x, int y, int, void* userInput );
int main( int argc, const char** argv )
{
// Read images
Mat lena= imread("../lena.jpg");
// Create windows
namedWindow("Lena");
// create a trackbark
createTrackbar("Lena", "Lena", &blurAmount, 30, onChange, &lena);
setMouseCallback("Lena", onMouse, &lena);
// Call to onChange to init
onChange(blurAmount, &lena);
// wait app for a key to exit
waitKey(0);
// Destroy the windows
destroyWindow("Lena");
return 0;
}
// Trackbar call back function
static void onChange(int pos, void* userInput)
{
if(pos <= 0)
return;
// Aux variable for result
Mat imgBlur;
// Get the pointer input image
Mat* img= (Mat*)userInput;
// Apply a blur filter
blur(*img, imgBlur, Size(pos, pos));
// Show the result
imshow("Lena", imgBlur);
}
//Mouse callback
static void onMouse( int event, int x, int y, int, void* userInput )
{
if( event != EVENT_LBUTTONDOWN )
return;
// Get the pointer input image
Mat* img= (Mat*)userInput;
// Draw circle
circle(*img, Point(x, y), 10, Scalar(0,255,0), 3);
// Call on change to get blurred image
onChange(blurAmount, img);
}
3.4 QT图形用户界面
- 将按钮添加到用户界面
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// OpenCV includes
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
Mat img;
bool applyGray=false;
bool applyBlur=false;
bool applySobel=false;
void applyFilters(){
Mat result;
img.copyTo(result);
if(applyGray){
cvtColor(result, result, COLOR_BGR2GRAY);
}
if(applyBlur){
blur(result, result, Size(5,5));
}
if(applySobel){
Sobel(result, result, CV_8U, 1, 1);
}
imshow("Lena", result);
}
void grayCallback(int state, void* userData)
{
applyGray= true;
applyFilters();
}
void bgrCallback(int state, void* userData)
{
applyGray= false;
applyFilters();
}
void blurCallback(int state, void* userData)
{
applyBlur= (bool)state;
applyFilters();
}
void sobelCallback(int state, void* userData)
{
applySobel= !applySobel;
applyFilters();
}
int main( int argc, const char** argv )
{
// Read images
img= imread("../lena.jpg");
// Create windows
namedWindow("Lena");
// create Buttons
createButton("Blur", blurCallback, NULL, QT_CHECKBOX, 0);
createButton("Gray",grayCallback,NULL,QT_RADIOBOX, 0);
createButton("RGB",bgrCallback,NULL,QT_RADIOBOX, 1);
createButton("Sobel",sobelCallback,NULL,QT_PUSH_BUTTON, 0);
// wait app for a key to exit
waitKey(0);
// Destroy the windows
destroyWindow("Lena");
return 0;
}
3.5 OpenGL支持
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// OpenGL includes
#ifdef __APPLE__
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
// OpenCV includes
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
Mat frame;
GLfloat angle= 0.0;
GLuint texture;
VideoCapture camera;
int loadTexture() {
if (frame.data==NULL) return -1;
glBindTexture( GL_TEXTURE_2D, texture ); //bind the texture to it's array
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, frame.cols, frame.rows,0, GL_BGR, GL_UNSIGNED_BYTE, frame.data);
return 0;
}
void on_opengl(void* param)
{
glLoadIdentity();
// Load Texture
glBindTexture( GL_TEXTURE_2D, texture );
// Rotate plane
glRotatef( angle, 1.0f, 1.0f, 1.0f );
// Create the plate
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
glEnd();
}
int main( int argc, const char** argv )
{
// Open WebCam
camera.open(0);
if(!camera.isOpened()){
camera.open("Recording3.webm");
if(!camera.isOpened())
return -1;
}
// Create new windows
namedWindow("OpenGL Camera", WINDOW_OPENGL);
// Enable texture
glEnable( GL_TEXTURE_2D );
glGenTextures(1, &texture);
setOpenGlDrawCallback("OpenGL Camera", on_opengl);
while(waitKey(30)!='q'){
camera >> frame;
// Create first texture
loadTexture();
updateWindow("OpenGL Camera");
angle =angle+4;
}
// Destroy the windows
destroyWindow("OpenGL Camera");
return 0;
}