buttonsqt.cpp

Harald Schmidt, 2012-08-24 06:33 pm

Download (9 kB)

 
1
/**
2
 * @file buttonsqt.cpp
3
 * @brief Demo Application to test some new interface functions of HighGUI-Module        
4
 * @author Harald Schmidt
5
 */
6
7
#include "opencv/cv.h"
8
#include "opencv2/imgproc/imgproc.hpp"
9
#include "opencv2/highgui/highgui.hpp"
10
#include <iostream>
11
12
#ifdef _WIN32
13
    #include <io.h>             // _findfirst, _findnext
14
#else
15
    #include <unistd.h>
16
    #include <dirent.h>
17
#endif
18
19
using namespace cv;
20
using namespace std;
21
22
/**
23
 * @function UpdateContent
24
 * @brief Interface-Function to set content in buttonbar
25
 */
26
void UpdateContent( string strpos, string name )
27
{
28
    try {
29
        setButtonBarContent("ColorImg", EMOD_Label, 1, (char *) strpos.c_str());
30
        setButtonBarContent("ColorImg", EMOD_Label, 2, (char *) name.c_str());
31
    }
32
    catch (const std::exception &e)
33
    {
34
        std::cout << "setButtonBarContent Error: " << e.what() << endl;
35
    }
36
    printf("\n");
37
}
38
39
/**
40
 * @function main
41
 * @brief Main function
42
 */
43
44
int main( int argc, char** argv )
45
{
46
    Mat img;
47
    Mat imgArray[5];
48
    string nameVec[5];
49
    string missing = "";
50
51
    double ticks = (double) getTickCount();
52
    int sec = ticks / getTickFrequency();
53
    int randomcode = (sec %2);  
54
55
    int WinMode = CV_WINDOW_NORMAL;
56
    if ( randomcode == 0 ) 
57
    { 
58
       WinMode = CV_WINDOW_AUTOSIZE;
59
       printf("\nstartUpMode: CV_WINDOW_AUTOSIZE\n" );
60
    } else {
61
       printf("\nstartUpMode: CV_WINDOW_NORMAL\n" );
62
    }
63
64
    cv::namedWindow("ColorImg", WinMode);
65
66
    nameVec[0] = "baboon.jpg";
67
    nameVec[1] = "board.jpg";
68
    nameVec[2] = "building.jpg";
69
    nameVec[3] = "fruits.jpg";
70
    nameVec[4] = "lena.jpg";
71
72
    int cnt =0;
73
    for ( int i=0; i < 5 ; i++ )
74
    {
75
        img = cv::imread(nameVec[i]);
76
        if ( img.empty() ) {
77
            missing += nameVec[i] + "\n";
78
        } else {
79
            imgArray[cnt] = img;
80
            cnt++;
81
        }
82
    }
83
84
    if ( cnt < 5 )
85
    {
86
        string info = "Sorry, missing images !\nPlease copy following images to executable dir:\n";
87
        info += missing;
88
                printf("------\n%s------", info.c_str() );
89
        cv::dispInfoBox( "ColorImg", "New Function Info", info );
90
        randomcode = 2;
91
        if ( cnt <= 0 ) return 0;
92
    }
93
94
        
95
96
    bool USE_hms   = false;
97
    int iKey = 999;
98
    int iSaveQ = 0;
99
100
    if ( randomcode == 0 )
101
    {
102
       setButtonBarContent("ColorImg", EMOD_Label, 3, "<font color=blue>AUTOSIZE</font>");
103
    }
104
    if ( randomcode == 1 )
105
    {
106
       setButtonBarContent("ColorImg", EMOD_Label, 3, "<font color=green>NORMAL</font>");
107
    } 
108
    if ( randomcode == 2 )
109
    {
110
       setButtonBarContent("ColorImg", EMOD_Label, 3, "<font color=red>wrong FILENAMES</font>");
111
    }
112
113
    //--------------------------------------------
114
    std::vector<std::string> stringVec;
115
116
    // call with two parameters => fetch field content only
117
    //             cv::getCommandVec("ColorImg", stringVec )
118
    // call with three parameters => fetch field content + command 
119
    //             cv::getCommandVec("ColorImg", stringVec, csBuffer )
120
121
    if ( cv::getCommandVec("ColorImg", stringVec ) )
122
    {
123
        printf("\n\ninitial values:" );
124
        for ( int j=0; j < stringVec.size() ; j++ )
125
        {
126
            printf("\n  <%s>", stringVec[j].c_str() );
127
            // may me you have activated checkbox in *.cfg ....
128
            if ( stringVec[j] == "h:m:s|1"   ) USE_hms  = true;
129
        }
130
    }
131
132
    // move window to x1=25% of screen width on startup
133
    // window width=50% of screen is ignored in case of CV_WINDOW_AUTOSIZE
134
    cv::adjustWindowPos( "ColorImg",25,50, 0, 90 );
135
136
    int idx = 0;
137
138
    //---------------------------  Start of processing loop
139
    while (iKey != 27)
140
    {
141
        char csBuffer[255];
142
        csBuffer[0] = 0;
143
        if ( iKey > 0 )
144
        {
145
            cv::imshow("ColorImg", imgArray[idx]);
146
            char csTmp[255];
147
            sprintf(csTmp,"%d/%d", idx+1, cnt );
148
            string strPos = string(csTmp);
149
            UpdateContent( strPos, nameVec[idx] );
150
        }
151
152
        iKey = cv::waitKey(5);
153
154
        //------------------------ some events from ColorImg  ?
155
        cv::getCommandVec("ColorImg", stringVec,  csBuffer );
156
        if ( strlen(csBuffer) > 0  )
157
        {
158
            printf("\ngetCommandVec(ColorImg)->[%s]\n  ", csBuffer );
159
            string strCmd = string(csBuffer);
160
            for ( int j=0; j < stringVec.size() ; j++ )
161
            {
162
                printf("<%s>", stringVec[j].c_str() );
163
164
                if ( stringVec[j] == "h:m:s|1"  ) USE_hms  = true;
165
                if ( stringVec[j] == "h:m:s|0"  ) USE_hms  = false;
166
167
                // ------------- content of SpinField :
168
                int ipos = stringVec[j].find("JPGQ");
169
                if ( ipos >= 0 )
170
                {
171
                    ipos = stringVec[j].find("|");
172
                    iSaveQ = atoi(&stringVec[j][ipos+1]);
173
                    iKey = '*';
174
                }
175
            }
176
177
            if (strCmd == "LoadImg") iKey = 'l';
178
            if (strCmd == "SaveImg") iKey = 's';
179
            if (strCmd == "PrevImg") iKey = 'p';
180
            if (strCmd == "NextImg") iKey = 'n';
181
            if (strCmd == "Help_EMOD_PushText")  iKey = 'h';
182
            if (strCmd == "h:m:s_EMOD_CheckBox")
183
            {
184
                printf("\n use_hms=%d ", (int) USE_hms );
185
            }
186
        }
187
188
        if ( iKey == ' ') iKey = 'n';
189
        if ( iKey == 'p')
190
        {
191
            idx--;
192
            if (idx < 0) idx = 0;
193
        }
194
        if ( iKey == 'n')
195
        {
196
            idx++;
197
            if (idx >= cnt) idx = 0;
198
        }
199
200
        if ( iKey == 's')
201
        {
202
            char szParam[64];
203
            sprintf(szParam,"_Q%02d", iSaveQ  );
204
            vector<int> ParamV;
205
            ParamV.resize(2);
206
            ParamV[0] = CV_IMWRITE_JPEG_QUALITY;
207
            ParamV[1] = iSaveQ;
208
209
            string filename = "SaveImg";
210
            if ( USE_hms ) {
211
                int tick =  0.001 * getTickCount() ;
212
                sprintf(csBuffer,"%010d", tick);
213
                filename += string(csBuffer);
214
                filename += string(szParam) + ".jpg";
215
            } else {
216
                string savename = nameVec[idx];
217
                int ipos = savename.find_last_of(".");
218
                filename = savename.substr(0,ipos);
219
                filename += string(szParam) + ".jpg";
220
            }
221
            cv::imwrite(filename,imgArray[idx],ParamV);
222
            printf("\nfile [%s] written ", filename.c_str() );
223
        }
224
225
        if ( iKey == 'h')
226
        {
227
            printf("\n >>>> demo for use of buttons from *.cfg <<<<");
228
            string info = "Please open the *.cfg file fitting to the *.exe you are working on.\nThe HighGUI-DLL has interpreted this *.cfg file ";
229
            info += "and if yo want so see,\nwhat the DLL does with the *.cfg file, one approch is to activate \n     $verbose\nby taking ";
230
            info += "the comment char '#' in the fitting line away. Thus you will get a \nconsole logging of the DLL activities.";
231
            cv::dispInfoBox( "ColorImg", "New Function Info", info );
232
233
            info  = "The simplest way to insert a button ist just one line :\n My simple Button \n";
234
            info += "In such a case no name for the button is needed and the HighGui-DLL stores \ninternal that you have pressed this button. ";
235
            info += "Your App says then to the DLL: \n   Give me the name of the last clicked button.\nIn your sorce code you just call:\n";
236
            info += "     cvGetCommandVec(\"ColorImg\", stringVec, csBuffer );\n";
237
            info += "And csBuffer will be will filled with the last command.";
238
            cv::dispInfoBox( "ColorImg", "New Function Info", info );
239
240
            info = "If <LanguageTransTab> is defined, you can activate\n   $applyLanguage \nto get some button names translated.";
241
            info += "This makes sense because getCommandVec gives you the original name back.";
242
            cv::dispInfoBox( "ColorImg", "New Function Info", info );
243
244
            info =  "All buttons without frame (QToolButton) appear in the context menu too.\nFor this type of button only one word (without leading $) is necessary in *.cfg\n";
245
            cv::dispInfoBox( "ColorImg", "New Function Info", info );
246
247
            info = "There is no keyword 'emit' in the definition:\n  $Spin JPGQ 0,100,75 \nWe dont want to have any action until 'SaveImg' is pressed.";
248
            cv::dispInfoBox( "ColorImg", "New Function Info", info );
249
250
            char msg4[] = "If you see 'SaveImg' and '$SaveImg' in different lines of *.cfg \nkeep in mind '$SaveImg' to be the floppy disk icon-button  ";
251
            cv::dispInfoBox( "ColorImg", "New Function Info", msg4 );
252
            char msg5[] = "This box is internal displayed by\nQMessageBox::information( this, pCaption,pInfo )\nas part of the opencv_highgui24*.dll";
253
            cv::dispInfoBox( "ColorImg", "New Function Info", msg5 );
254
        }
255
256
    } // waitKey loop
257
    
258
    destroyAllWindows();        
259
260
    return 0;
261
}