In opencv +, the windows created by cvnamedwindow (char *, int + flag) + display QT performance + or + disable QT Performance

Source: Internet
Author: User


Display the QT performance of the windows created by cvnamedwindow (char *, int flag), for example, display coordinates, pixel values, amplification, pan, and save,

Method 1:Select with_qt when compiling the opencv library.

Method 2: After the regular library of opencv is created (that is, the library compiled by with_qt is not selected), you can go to the original directory of c: \ opencv231 \ opencv \ modules \ highgui \ SRC,
Copy these three files: window_qt.cpp, window_qt.h, window_qt.qrc, and files_qt:

1) Comment out line44 in window_qt.h, for example, "// # include" precomp. HPP "",
And add include such as: # include "cxcore. H"
# Include "cv. H"
# Include "highgui. H ""

2) Comment out line43 in window_qt.cpp, such as: // # If defined (have_qt) "and the last line," // # endif"

3) Open the properties of window_qt.qrc and go:
In custom build step ----> command line:
"$ (Qtdir) \ bin \ rcc.exe"-name window_qt-o ../generatedfiles/qrc_window_qt.cpp ../window_qt.qrc
Custom Build step ----> discription: RCC window_qt.qrc
Custom Build step ----> outputs: ../generatedfiles/qrc_window_qt.cpp
Custom Build step ----> additional dependencies: "$ (qtdir) \ bin \ rcc.exe"; "../window_qt.qrc"

4) Add the compiled "qrc_window_qt.cpp" file to the project.

5) Compile again, OK!

In the preceding three files, QT is used to implement related functions in highgui, such as cvnamedwindow (char *, int flag). This file is included in the project, the call functions in the program are directly found in the three files, and the related functions in the opencv library are blocked.

PS: 1. You may need to add qttestd4.lib to linker ----> input.
2. if the windows created by cvnamedwindow (char *, int flag) are in the Child thread (not in the GUI thread), The opencv library of with_qt (I. e. neither method 1 nor method 2 ). At this time, a solution is to use the cvnamedwindow (char *, int flag) in the common library in the subthread, And the cvnamedwindow_1 (char *, int flag) with the with_qt library in the GUI thread) (renamed by cvnamedwindow (), that is, two types of windows are used in the program.

Method 3: Two types of windows are used in the program. That is to say, on the basis of method 2, rename the functions with the same name as the normal CV library in the window_qt.cpp and window_qt.h files (such as adding the suffix _ 1) to avoid call conflicts!

Cv_impl int cvwaitkey_1 (INT Arg );
Cv_impl int cvnamedwindow_1 (const char * Name, int flags );
Cv_impl void cvdestroywindow_1 (const char * Name );
Cv_impl void cvdestroyallwindows_1 (void );
Cv_impl void cvmovewindow_1 (const char * Name, int X, int y );
Cv_impl void cvresizewindow_1 (const char * Name, int width, int height );

Cv_impl int cvcreatetrackbar_1 (const char * name_bar, const char * window_name, int * value, int count, cvtrackbarcallback on_change );
Cv_impl int cvgettrackbarpos_1 (const char * name_bar, const char * window_name );
Cv_impl void cvsettrackbarpos_1 (const char * name_bar, const char * window_name, int POS );
Cv_impl void cvsetmousecallback_1 (const char * window_name, cvmousecallback on_mouse, void * PARAM );
Cv_impl void cvshowimage_1 (const char * Name, const cvarr * ARR );

A total of 11 functions are modify. These 11 functions can be shared with similar functions in the common CV library. In addition, the function should be partially declared in window_qt.h!

There is no problem with using these 11 functions in the GUI thread,But to use it in subthreads, you must first create a new qwidge subclass instance (11 functions such as cvnamedwindow_1 () in the GUI thread in the implement (CPP file) of the qwidge subclass) ), and then use qwidget: movetothread (& thread_sub) to move the Instance Object of the qwidget subclass into the Child thread. This ensures that the create in the GUI main thread is run in the sub-thread.


It cannot be the inheritance class of qobject, but only the inheritance class of qwidget !!!


For example:

Two classes are defined in mainwindow. h.

//************************************** *******

Class dispthreadobject: PublicQwidget// Qobject // this field can only be a subclass of qwidget, but cannot be a subclass of qobject !!! Otherwise, the above error occurs.




Dispthreadobject ();

~ Dispthreadobject ();

Void setusedisparity (INT usedisparity );

Sequence seq1;

Sequence seq2;

Cvstereobmstate * bmstate;

CV: stereosgbm sgbm;

Cimageprocessingthread * thread_a;

Cimageprocessingthread * thread_ B;

Bool mm_break;

Public slots:

Void slotdisptreadobject ();



Int m_usedisparity;



// %

Gui main class

Class mymainwindow: Public qmainwindow



Public :.....


Qthread dispthread;

Dispthreadobject dispobject;




In mainwindow. cpp

//************************************** *******


Void mymainwindow: slotdisparityonline_thread ()



Connect (& dispthread, signal (started (), & dispobject, slot (slotdisptreadobject ()));

Dispobject. movetothread (& dispthread );

Dispthread. Start ();



// %

Void dispthreadobject: slotdisptreadobject ()


Int numsaved = 0;

STD: String filename;


Int width = 782;

Int Height = 582;

Iplimage * imagel = cvcreateimage (cvsize (width, height), ipl_depth_8u, 1 );

Iplimage * imager = cvcreateimage (cvsize (width, height), ipl_depth_8u, 1 );

Iplimage * disparity = cvcreateimage (cvsize (width, height), ipl_depth_8u, 1 );

Cvnamedwindow_1("Display disparity_sgbm", 1 );


While (numsaved <m_usedisparity ){

If (mm_break ){




If (! (Thread_a-> imagequeue. Empty ())&&! (Thread_ B-> imagequeue. Empty ())){ = (thread_a-> imagequeue. Front (). Name;

Seq1.frame = (thread_a-> imagequeue. Front (). frame;

//********* = (thread_ B-> imagequeue. Front (). Name;

Seq2.frame = (thread_ B-> imagequeue. Front (). frame;


// Production and consumption model to queue

Thread_a-> usedbytes. Acquire ();

Thread_a-> imagequeue. Pop (); // popping an used element

Thread_a-> freebytes. Release ();

Thread_ B-> usedbytes. Acquire ();

Thread_ B-> imagequeue. Pop ();

Thread_ B-> freebytes. Release ();


// Decide left or right image.

If ( Compare (0, 1, "L ")){

// Gray Image

Cvcvtcolor (seq1.frame, imagel, cv_bgr2gray );

Cvcvtcolor (seq2.frame, imager, cv_bgr2gray );

} Else if ( Compare (0, 1, "L ")){

Cvcvtcolor (seq2.frame, imagel, cv_bgr2gray );

Cvcvtcolor (seq1.frame, imager, cv_bgr2gray );




// Combine file name

Char CH = At ( Size ()-5 );

Filename = "disp ";

Filename. append (1, CH );

Filename. append (". PNG ");


// Int runtime = disparityalgorithm: stereosgbm (imagel, imager, disparity, sgbm );//

Int runtime = disparityalgorithm: stereobm (imagel, imager, disparity, bmstate );

Cvshowimage_1("Display disparity_sgbm", disparity );

Cvwaitkey_1(1 );


// Cvsaveimage (filename. c_str (), disparity );


// Release space

Cvreleaseimage (& seq1.frame );

Cvreleaseimage (& seq2.frame );

Filename. Clear ();

++ Numsaved;



If (0/* Stop push_button */){







Cvreleaseimage (& imagel );

Cvreleaseimage (& imager );


Cvdestroywindow_1("Display disparity_sgbm ");






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.