Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

drawing square on image

#include "stdafx.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

volatile int knt=0;

Mat img_save;

void CallBackFunc(int event, int x, int y, int flags, void* userdata){

    if(event == EVENT_LBUTTONDOWN){

        Mat dst;

        if(knt==0){
           dst=imread("Nature.jpg");
           knt=1;
        }

        else if(knt==1)
            dst=img_save.clone();

        img_save=dst;

        int data=50;
        Rect dikdortgen( (x-data/2), (y-data/2), data, data );
        rectangle(dst, dikdortgen, CV_RGB(255,255,255),2);

        imshow("My Window", dst);
     }
}


int main(int argc, char** argv){

     int kontrol=0;
     Mat img = imread("Nature.JPG"); // Read image from file 

     if ( img.empty() )  //if fail to read the image
     { 
          cout << "Error loading the image" << endl;
          return -1; 
     }

     namedWindow("My Window", CV_WINDOW_NORMAL); //Create a window

     setMouseCallback("My Window", CallBackFunc, 0); //set the callback function for any mouse event

     if(kontrol==0){
        imshow("My Window", img); //show the image
        kontrol=1;
     }

     waitKey(0); // Wait until user press some key   
     return 0;
}

when ı run this program ı can draw a square where ı click on the image, but there is problem, ı don't have to see previous drawing square when ı click anywhere else could you help?

drawing square on image

#include "stdafx.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

volatile int knt=0;

Mat img_save;

void CallBackFunc(int event, int x, int y, int flags, void* userdata){

    if(event == EVENT_LBUTTONDOWN){

        Mat dst;

        if(knt==0){
           dst=imread("Nature.jpg");
           knt=1;
        }

        else if(knt==1)
            dst=img_save.clone();

        img_save=dst;

        int data=50;
        Rect dikdortgen( (x-data/2), (y-data/2), data, data );
        rectangle(dst, dikdortgen, CV_RGB(255,255,255),2);

        imshow("My Window", dst);
     }
}


int main(int argc, char** argv){

     int kontrol=0;
     Mat img = imread("Nature.JPG"); // Read image from file 

     if ( img.empty() )  //if fail to read the image
     { 
          cout << "Error loading the image" << endl;
          return -1; 
     }

     namedWindow("My Window", CV_WINDOW_NORMAL); //Create a window

     setMouseCallback("My Window", CallBackFunc, 0); //set the callback function for any mouse event
 
     if(kontrol==0){
        imshow("My Window", img); //show the image
        kontrol=1;
     }

     waitKey(0); // Wait until user press some key   
     return 0;
}

when ı run this program ı can draw a square where ı click on the image, but there is problem, ı don't have to see previous drawing square when ı click anywhere else could you help?