Ask Your Question
0

drawing square on image

asked 2015-10-12 03:36:45 -0600

Berkay gravatar image

updated 2015-10-12 03:37:15 -0600

#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 

     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?

edit retag flag offensive close merge delete

Comments

Change Mat img_save; in Mat img_save,dst; and delete Mat dst in you callback

LBerger gravatar imageLBerger ( 2015-10-12 03:59:09 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2015-10-12 03:56:35 -0600

Mat img;

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

if(event == EVENT_LBUTTONDOWN){

    Mat dst;

    dst = img.clone();

    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;
 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 

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

 waitKey(0); // Wait until user press some key   
 return 0;
}
edit flag offensive delete link more

Comments

thank you...

Berkay gravatar imageBerkay ( 2015-10-12 04:48:00 -0600 )edit

@Berkay - please, mark as solved every correct/helpful answer. It will help to keep the forum clean and useful for other users

LorenaGdL gravatar imageLorenaGdL ( 2015-10-12 05:16:41 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-10-12 03:36:45 -0600

Seen: 306 times

Last updated: Oct 12 '15