I have to click any button to make my code work

asked 2015-05-23 17:08:37 -0500

bulbos gravatar image

I make program in which I can use morfological filters and some brushes. I can say that it's something like paint with additional possibility to use erosion and other filters. It works like that I have one window with loaded image and second window with menu. When I click erosion nothing happen I have to click any key in order to get correct result. Similar is with "paint" I must hold pressed any key to get it work. I really don't know why is it so . I have one idea that i caused by waitkey but when I remove it it completely don't work. My code:

using namespace cv;
using namespace std;
int th;
int thr;
int d;
int d2;
int gx=-1000;
int gy=-1000;
int r=0;
int g=0;
int b=0;
int f;
int bx=-1;
int by=-1;
int esa1;
int esa2;
int a;
int a1;
int flagamyszki=0;


void function( int, void* )
{

}
static void onMouse1( int event, int x, int y, int, void* )
{
if( event == EVENT_LBUTTONDOWN )
{
bx = x;
by = y;
}
}
/*lewy przycisk stara wersja
static void onMouse( int event, int x, int y, int, void* )
{
if( event == EVENT_LBUTTONDOWN )
{

cout << "x mouse coordinate:" << x << endl;
cout << "y mouse coordinate:" << y << endl;
gx = x;
gy = y;
}
}
*/
static void onMouse( int event, int x, int y, int, void* )
{
if( event == EVENT_LBUTTONDOWN )
{
flagamyszki=1;
}
if( event == EVENT_LBUTTONUP )
{
flagamyszki=0;
}
if( event == EVENT_MOUSEMOVE && flagamyszki==1)
{
gx = x;
gy = y;
}
if( event == EVENT_RBUTTONDOWN )
{
gx = x;
gy = y;
}
}

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    Mat image1;
    Mat pokazywarka;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    namedWindow( "Trackbar window");// Create a window for display.

    Scalar colour = Scalar(b,g,r);
    pokazywarka=imread("/home/kondzio/Pulpit/pokazywarka.jpg",CV_LOAD_IMAGE_COLOR);
    rectangle(pokazywarka, Point(0,0), Point(500,100), colour, -1, 8, 0);
    imshow("Trackbar window" ,pokazywarka);

    createTrackbar("Canny treshold", "Trackbar window", &th, 500, function );
    createTrackbar("Canny trratio", "Trackbar window", &thr, 500, function );
    createTrackbar("Radius/Halfedge", "Trackbar window", &d, 500,function);
    createTrackbar("2nd Halfedge", "Trackbar window", &d2, 500,function);
    createTrackbar("ellipse 1st axis", "Trackbar window", &esa1, 500,function);
    createTrackbar("ellipse 2nd axis", "Trackbar window", &esa2, 500,function);
    createTrackbar("ellipse angle", "Trackbar window", &a, 360,function);
    createTrackbar("ellipse start angle", "Trackbar window", &a1, 360,function);
    createTrackbar("Filled x-1", "Trackbar window", &f, 500,function);
    createTrackbar("R", "Trackbar window", &r, 255,function);
    createTrackbar("G", "Trackbar window", &g, 255,function);
    createTrackbar("B", "Trackbar window", &b, 255,function);

    Mat kernel = getStructuringElement( MORPH_RECT, Size( 4, 4 ));


    int kernel_size=3;


    setMouseCallback( "Display window", onMouse, 0 );
    setMouseCallback( "Trackbar window", onMouse1, 0 );
    while(1){
        Scalar colour = Scalar(b,g,r);
        //Pokazywarka koloru
        rectangle(pokazywarka, Point(0,0), Point(500,100), colour, -1, 8, 0);
        line(pokazywarka, Point(100,100),Point(100,250) , Scalar(0,0,0), 1, 8, 0);
        line ...
(more)
edit retag flag offensive close merge delete

Comments

Waitkey(0) == 27 will wait infinitely until a key is pressed. Change it to 1, for example. Anyway your while loop is quite weird, I don't understand why you draw again and again rectangles, lines and text...

LorenaGdL gravatar imageLorenaGdL ( 2015-05-24 04:29:13 -0500 )edit