Ask Your Question

Revision history [back]

I have to click any button to make my code work

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(pokazywarka, Point(200,100),Point(200,250) , Scalar(0,0,0), 1, 8, 0);
        line(pokazywarka, Point(300,100),Point(300,250) , Scalar(0,0,0), 1, 8, 0);
        line(pokazywarka, Point(400,100),Point(400,250) , Scalar(0,0,0), 1, 8, 0);
        line(pokazywarka, Point(0,150),Point(500,150) , Scalar(0,0,0), 1, 8, 0);
        line(pokazywarka, Point(0,200),Point(500,200) , Scalar(0,0,0), 1, 8, 0);
        putText(pokazywarka, "circle", Point(2,130), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0,0,0), 1);
        putText(pokazywarka, "square", Point(102,130), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0,0,0), 1);
        putText(pokazywarka, "rectangle", Point(202,130), FONT_HERSHEY_SIMPLEX, 0.6, Scalar(0,0,0), 1);
        putText(pokazywarka, "ellipse", Point(302,130), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0,0,0), 1);
        putText(pokazywarka, "exit", Point(402,130), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0,0,0), 1);

        putText(pokazywarka, "orginal", Point(2,180), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0,0,0), 1);
        putText(pokazywarka, "erosion", Point(102,180), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0,0,0), 1);
        putText(pokazywarka, "dilatation", Point(202,180), FONT_HERSHEY_SIMPLEX, 0.6, Scalar(0,0,0), 1);
        putText(pokazywarka, "opening", Point(302,180), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0,0,0), 1);
        putText(pokazywarka, "closing", Point(402,180), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0,0,0), 1);

        putText(pokazywarka, "tophat", Point(2,230), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0,0,0), 1);
        putText(pokazywarka, "edge", Point(102,230), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0,0,0), 1);
        putText(pokazywarka, "grayscale", Point(202,230), FONT_HERSHEY_SIMPLEX, 0.6, Scalar(0,0,0), 1);
        putText(pokazywarka, "save", Point(302,230), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0,0,0), 1);
        putText(pokazywarka, "save as", Point(402,230), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0,0,0), 1);
        imshow("Trackbar window" ,pokazywarka);


        //photoshop
        if(bx>0 && bx<100 && by>100 && by<150){
            circle(image,Point(gx,gy),d,colour,f-1,8);
        }
        if(bx>100 && bx<200 && by<150 && by>100){
            rectangle(image, Point(gx-d,gy-d), Point(gx+d,gy+d), colour, f-1, 8, 0);
        }
        if(bx>200 && bx<300 && by<150 && by>100){
            rectangle(image, Point(gx-d,gy-d2), Point(gx+d,gy+d2), colour, f-1, 8, 0);
        }
        if(bx>300 && bx<400 && by<150 && by>100){
            ellipse(image, Point( gx, gy ), Size( esa1, esa2), a, a1, 360, colour, f-1, 8 );
        }
        gx=-10000;
        gy=-10000;
        imshow( "Display window", image );
        image1=image.clone();

        //filtration
        int flagag=0;
        if (bx>200 && bx<300 && by>200 && by<250 && flagag==0){ //grayscale
            cvtColor(image,image1,CV_RGB2GRAY);
            image=image1.clone();
            imshow( "Display window", image );
            flagag=1;
            bx=-1000;
            by=-1000;
        }
        if (bx>0 && bx<100 && by>150 && by<200){ //orginal
            image = imread(argv[1], CV_LOAD_IMAGE_COLOR);
            image1=image.clone();
            imshow( "Display window", image );
            flagag=0;
            bx=-1000;
            by=-1000;
        }
        if (bx>100 && bx<200 && by>150 && by<200){ //erosion
            morphologyEx(image, image1, 0, kernel );
            image=image1.clone();
            imshow( "Display window", image );
            bx=-1000;
            by=-1000;
        }
        if (bx>200 && bx<300 && by>150 && by<200){ //dilatation
            morphologyEx(image, image1, 1, kernel );
            image=image1.clone();
            imshow( "Display window", image );
            bx=-1000;
            by=-1000;
        }
        if (bx>300 && bx<400 && by>150 && by<200){ //opening
            morphologyEx(image, image1, 2, kernel );
            image=image1.clone();
            imshow( "Display window", image );
            bx=-1000;
            by=-1000;
        }
        if (bx>400 && by<500 && by>150 && by<200){ //closing
            morphologyEx(image, image1, 3, kernel );
            image=image1.clone();
            imshow( "Display window", image );
            bx=-1000;
            by=-1000;
        }
        if (bx>0  && bx<100 && by>200 && by<250){ //tophat
            morphologyEx(image, image1, 5, kernel );
            image=image1.clone();
            imshow( "Display window", image );
            bx=-1000;
            by=-1000;
        }
        if (bx>100 && bx<200 && by>200 && by<250){ //edge
            Canny(image, image1, th, thr, kernel_size );
            morphologyEx(image, image1, 5, kernel );
            image=image1.clone();
            imshow( "Display window", image );
            bx=-1000;
            by=-1000;
        }
        if (bx>300 && bx<400 && by>200 && by<250){ //save
            imwrite("/home/kondzio/Pulpit/modified.jpg",image);
            bx=-1000;
            by=-1000;
        }
        if (bx>400 && bx<500 && by>200 && by<250){ //save as
            imwrite(argv[1],image);
            bx=-1000;
            by=-1000;
        }
        if (bx>400 && bx<500 && by>100 && by<150){ //exit
            destroyAllWindows();
        }





        if(waitKey(0)==27 && bx>400 && by<150){
        break;
        }
    }
}