OpenCV Tutorial 3 - Chapter 4

 Author: Noah Kuntz (2009)
Contact: [email protected]

Keywords: OpenCV, computer vision, highgui, interface, gui

My Vision Tutorials Index

This tutorial assumes the reader:
(1) Has a basic knowledge of Visual C++
(2) Has some familiarity with computer vision concepts
(3) Has read the previous tutorials in this series

The rest of the tutorial is presented as follows:

  • Step 1: HighGUI Overview
  • Step 2: Using Mouse Events
  • Step 3: Using Sliders for Buttons
  • Final Words

Important Note!

More information on the topics of these tutorials can be found in this book: Learning OpenCV: Computer Vision with the OpenCV Library

Step 1: HighGUI Overview(HighGUI概述)

Chapter 4 covers the HighGUI library(第4章涵盖了HighGUI库). Some of this material is repeated from the initial tutorial, including the basics of displaying images, and reading and writing video(有些材料是与最初的教程重复,包括显示图像的基础知识,阅读和写作视频). These topics are covered in greater detail in the text(这些主题包括更详细的文字). For this tutorial two examples are presented for using mouse events and sliders, both for creating interfaces(在本教程中介绍的两个例子是使用鼠标方法和滑块,都是为创建接口).

Step 2: Using Mouse Events(使用鼠标方法)


Squares drawn with the mouse(用鼠标画方)


One simple graphical interface is using mouse clicks to draw(一个简单的图形界面用鼠标点击画). This program creates a rectangle when you click the mouse on the window, and stretches the rectangle until you release the mouse(这个程序创建一个矩形,当你点击窗口上的鼠标,舒展矩形,直到你松开鼠标). This requires the creation of a mouse callback(这就需要创造一个鼠标回调). For the main function we use a while loop that keeps redrawing each box as its created(对于主函数,我们使用一个while循环,不断重新划分为创建每个方块). Boxes are drawn and windows are created as learned in previous tutorials(为在前面的教程学习中,块被制定和窗口被创建). The main new thing is the creation of the mouse callback(现在主要的新事情是鼠标回调的创建). This is initialized with cvSetMouseCallback(这个用cvSetMouseCallback进行初始化). The callback itself is an arbitrarily named function of the form my_mouse_callback( int event, int x, int y, an int flags, void* param) where x and y are the mouse position and event is a code representing what mouse action occurred(回调本身是一个任意命名的形式my_mouse_callback( int event, int x, int y, an int flags, void* param),其中x和y是鼠标的位置, event是一个代码代表鼠标操作产生什么的一个事件). The callback then uses a switch statement to perform the actions needed for this program when the mouse is moved, clicked, or released as specified by the event(回调然后使用switch语句来执行鼠标的移动、点击或释放指定的事件时所需的行动计划). Here is the code(以下是代码):


 

void my_mouse_callback( int event, int x, int y, int flags, void* param );

 

CvRect box;

bool drawing_box = false;

 

void draw_box( IplImage* img, CvRect rect ){

        cvRectangle( img, cvPoint(box.x, box.y), cvPoint(box.x+box.width,box.y+box.height),

                               cvScalar(0xff,0x00,0x00) );

}

 

// Implement mouse callback

void my_mouse_callback( int event, int x, int y, int flags, void* param ){

        IplImage* image = (IplImage*) param;

 

        switch( event ){

               case CV_EVENT_MOUSEMOVE:

                       if( drawing_box ){

                               box.width = x-box.x;

                               box.height = y-box.y;

                       }

                       break;

 

               case CV_EVENT_LBUTTONDOWN:

                       drawing_box = true;

                       box = cvRect( x, y, 0, 0 );

                       break;

 

               case CV_EVENT_LBUTTONUP:

                       drawing_box = false;

                       if( box.width < 0 ){

                               box.x += box.width;

                               box.width *= -1;

                       }

                       if( box.height < 0 ){

                               box.y += box.height;

                               box.height *= -1;

                       }

                       draw_box( image, box );

                       break;

        }

}

 

int _tmain(int argc, _TCHAR* argv[])

{

        const char* name = "Box Example";

        box = cvRect(-1,-1,0,0);

 

        IplImage* image = cvLoadImage( "MGC.jpg" );

        cvZero( image );

        IplImage* temp = cvCloneImage( image );

 

        cvNamedWindow( name );

 

        // Set up the callback

        cvSetMouseCallback( name, my_mouse_callback, (void*) image);

 

        // Main loop

        while( 1 ){

               cvCopyImage( image, temp );

               if( drawing_box )

                       draw_box( temp, box );

               cvShowImage( name, temp );

 

               if( cvWaitKey( 15 )==27 )

                       break;

        }

 

        cvReleaseImage( &image );

        cvReleaseImage( &temp );

        cvDestroyWindow( name );

 

        return 0;

}

 


Step 3: Using Sliders for Buttons(使用滑块按钮)


Using a switch to change the color of a circle(使用一个开关来改变圆的颜色)


Another basic interface element is creating buttons(另一个基本的界面元素是创建按钮). The cvCreateTrackbar function can be used to create a basic interface in the form of a slider with as many possible positions as is required(该cvCreateTrackbar功能可用于在许多可能需要界面的位置创建一个基本界面接口). With just two positions it works like a basic flip switch(有这两个位置,它就像一个基本的轻触开关). In this example a callback is created similiar to the use of the mouse, but somewhat simpler(在这个例子中创建一个类似使用鼠标的回调,但有些简单). A loop is used to change the color of a drawn circle each time the switch is toggled(每次切换开关,一个循环是用来改变一个绘制圆的颜色). Here is the code(以下是代码):


 

int g_switch_value = 0;

int colorInt = 0;

 

// Trackbar/switch callback

void switch_callback( int position ){

        if( position == 0 ){

                colorInt = 0;

        }else{

                colorInt = 1;

        }

}

 

int _tmain(int argc, _TCHAR* argv[])

{

        const char* name = "Demo Window";

        int radius = 30;

        int thickness = 2;

        int connectivity = 8;

        CvScalar green = CV_RGB(0,250,0);

        CvScalar orange = CV_RGB(250,150,0);

 

        IplImage* src1 = cvLoadImage( "MGC.jpg" );

        CvPoint pt2 = cvPoint(405,195);

 

        cvNamedWindow( name, 1 );

        cvShowImage(name, src1);

       

 

        // Create trackbar

        cvCreateTrackbar( "Switch", name, &g_switch_value, 1, switch_callback );

 

        // Loop to update the circle color

        while( 1 ) {

               if( colorInt == 0)

                       cvCircle(src1,pt2,radius,green,thickness,connectivity);

               else

                       cvCircle(src1,pt2,radius,orange,thickness,connectivity);

               cvShowImage(name, src1);

               if( cvWaitKey( 15 ) == 27 )

                       break;

        }

 

        cvReleaseImage( &src1 );

        cvDestroyWindow( name );

 

        return 0;

}

 


 

Final Words(结束语)

This tutorial's objective was to show how to use some additional HighGUI functions(本教程的目标是展示如何使用一些额外的HighGUI功能). You should be able to extend these functions to create basic interfaces for OpenCV programs(你应该能够扩展这些功能来创建OpenCV的程序的基本接口).

Click here to email me.
Click here to return to my Tutorials page.

 

你可能感兴趣的:(计算机视觉)