Ask Your Question
0

mouse click and draw circle

asked 2015-07-19 05:33:39 -0600

coldplay gravatar image

updated 2015-07-19 14:55:46 -0600

Hi My topic is draw circle with mouse click method I click 3 points on the image and draw circle I have got a code for drawing circle with given three points but my code is not working this code for draw circel wiht 3 points

    Mat cember_denklemi(Point2f A,Point2f B,Point2f C) {
    double W[3][3]={{A.x,A.y,1},
                    {B.x,B.y,1},
                    {C.x,C.y,1}};

    double T[3][1]={-(A.x*A.x+A.y*A.y),
                    -(B.x*B.x+B.y*B.y),
                    -(C.x*C.x+C.y*C.y)};
    Mat M=Mat(3,3,CV_64F,W);
    Mat N=Mat(3,1,CV_64F,T);
    Mat L=M.inv()*N;

    return L;
}

and this is my code,

    #include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include <iostream>
using namespace std;
using namespace cv;
static int mouse_x = -1;
static int mouse_y = -1;
Mat circle_eq(Point2f A,Point2f B,Point2f C)
{
    double W[3][3]={{A.x,A.y,1},
                    {B.x,B.y,1},
                    {C.x,C.y,1}};

    double T[3][1]={-(A.x*A.x+A.y*A.y),
                    -(B.x*B.x+B.y*B.y),
                    -(C.x*C.x+C.y*C.y)};
    Mat M=Mat(3,3,CV_64F,W);
    Mat N=Mat(3,1,CV_64F,T);
    Mat L=M.inv()*N;

    return L;
}
void mouseKordinat(int evt, int x, int y, int flags, void* ptr)
{

if(evt == CV_EVENT_MOUSEMOVE)
  {
    mouse_x = x;
    mouse_y = y;
  }
int tik;
  tik = tik + 1;
}

void getPixelValue(Mat img, int x, int y);
Mat image;

int main()
{
 image = imread("c:/qwz/1q.jpg");
 imshow("MyWindow", image);
 setMouseCallback("MyWindow", mouseKordinat, 0 );
tik = tik%=3;

if(tik==0)
{   
int merkez=circle_eq(nokta(0), nokta(1), nokta(2));
circle(image,merkez,3, Scalar(255,255,255),-1,8, nokta(0));
}
if(tik>3)
{
    int nokta[0][0]=0;


}
  waitKey(0);
 return 0;
}

void mouseKordinat(int evt, int c, int r, int flags, void* ptr)
{
 if(evt==CV_EVENT_LBUTTONDOWN)
 {
 getPixelValue(image,r,c);

 }
}

void getPixelValue(Mat img, int r, int c)
{
 Vec3b pix=img.at<Vec3b>(r,c);
 int B = pix.val[0];
 int G = pix.val[1];
 int R = pix.val[2];
 cout<<"Row:"<<r<<" "<<"Column:"<<c<<" - "; 
 cout<<"B:"<<B<<" "<<"G:"<<G<<" "<<"R:"<<R<<"\n"; 

}
edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
1

answered 2015-07-19 07:02:00 -0600

updated 2015-07-19 11:52:56 -0600

here a sample code ( modified version of opencv_annotation.cpp ) i hope it will be a referance to you.

//modified from https://github.com/Itseez/opencv/blob/master/apps/annotation/opencv_annotation.cpp
// move the mouse on the window and try left click and right click 
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>


using namespace std;
using namespace cv;

// Function prototypes
void on_mouse(int, int, int, int, void*);


// Public parameters
Mat image(600, 800, CV_8UC3, Scalar(220, 220, 220));
vector<Point> points;
int roi_x0 = 0, roi_y0 = 0, roi_x1 = 0, roi_y1 = 0;
bool start_draw = false;

// Window name for visualisation purposes
const string window_name = "OpenCV Mouse Event Demo";

// FUNCTION : Mouse response for selecting objects in images
// If left button is clicked, start drawing a rectangle and circle as long as mouse moves
//If right button is clicked, look what will happen !
 // Stop drawing once a new left click is detected by the on_mouse function
void on_mouse(int event, int x, int y, int, void *)
{
    // Action when right button is clicked
    if (event == EVENT_RBUTTONDOWN)
    {
        points.push_back(Point(x, y));
        circle(image, Point(x, y), 5, Scalar(255, 255, 255), 1, 8);


        if (points.size() == 3)
        {
            // Find the minimum area enclosing circle
            Point2f center, vtx[4];
            float radius = 0;
            minEnclosingCircle(Mat(points), center, radius);

            circle(image, center, cvRound(radius), Scalar(0, 255, 255), 1, LINE_AA);
            points.clear();
        }

        imshow(window_name, image);
    }

    // Action when left button is clicked
    if (event == EVENT_LBUTTONDOWN)
    {
        if (!start_draw)
        {
            roi_x0 = x;
            roi_y0 = y;
            start_draw = true;
        }
        else {
            roi_x1 = x;
            roi_y1 = y;
            start_draw = false;
        }
    }
    // Action when mouse is moving
    if ((event == EVENT_MOUSEMOVE) && start_draw)
    {
        // Redraw bounding box and rectangle
        Mat current_view;
        image.copyTo(current_view);
        rectangle(current_view, Point(roi_x0, roi_y0), Point(x, y), Scalar(0, 0, 255));
        int radius = max(abs(roi_x0 - x), abs(roi_y0 - y));
        circle(current_view, Point(roi_x0, roi_y0), radius, Scalar(255, 0, 0), 1, 8);
        imshow(window_name, current_view);
    }
}


int main(int argc, const char** argv)
{
    // Init window interface and couple mouse actions
    namedWindow(window_name, WINDOW_AUTOSIZE);
    setMouseCallback(window_name, on_mouse);

    imshow(window_name, image);

    int key_pressed = 0;

    do
    {
        // Keys for processing
        // Based on the universal ASCII code of the keystroke: http://www.asciitable.com/
        //      c = 99          add rectangle to current image
        //      <ESC> = 27      exit program
        key_pressed = 0xFF & waitKey(0);
        if (key_pressed==99)
        {
            // draw a rectangle and a circle on the image

            int radius = max(abs(roi_x0 - roi_x1), abs(roi_y0 - roi_y1));
            circle(image, Point(roi_x0, roi_y0), radius, Scalar(0, 0, 255), 1, 8);
            rectangle(image, Point(roi_x0, roi_y0), Point(roi_x1, roi_y1), Scalar(255, 0, 0), 1);
            imshow(window_name, image);
        }
    }
    // Continue as long as the <ESC> key has not been pressed
    while (key_pressed != 27);

    // Close down the window
    destroyWindow(window_name);
    return 0;
}
edit flag offensive delete link more

Comments

Thanks for hijacking the code and adapting it :D

StevenPuttemans gravatar imageStevenPuttemans ( 2015-07-29 02:54:44 -0600 )edit
0

answered 2015-07-28 15:59:04 -0600

coldplay gravatar image

Hi I look into given code and I improve my code But I have a problem because I can't declare my variable 'nokta' ANd this is my code

    #include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include <iostream>
using namespace std;
using namespace cv;
static int mouse_x = -1;
static int mouse_y = -1;
static int  tik=0;
Mat cember_denklemi(Point2f A,Point2f B,Point2f C)
{
    double W[3][3]={{A.x,A.y,1},
                    {B.x,B.y,1},
                    {C.x,C.y,1}};

    double T[3][1]={-(A.x*A.x+A.y*A.y),
                    -(B.x*B.x+B.y*B.y),
                    -(C.x*C.x+C.y*C.y)};
    Mat M=Mat(3,3,CV_64F,W);
    Mat N=Mat(3,1,CV_64F,T);
    Mat L=M.inv()*N;

    return L;
}
void mouseKordinat(int evt, int x, int y, int flags, void* ptr)
{

if(evt == CV_EVENT_MOUSEMOVE)
  {
    mouse_x = x;
    mouse_y = y;

   tik = tik+1;
  }

}

void getPixelValue(Mat img, int x, int y);
Mat image;

int main()
{
 image = imread("c:/opencv2.4.6./15.jpg");
 imshow("MyWindow", image);
 setMouseCallback("MyWindow", mouseKordinat, 0 );
tik = tik%=3;//burda mod demek istedim3 noktada bir çember çizdirecek ya
int nokta;
if(tik==0)
{   
int merkez=cember_denklemi(nokta(0), nokta(1), nokta(2));
circle(image,merkez,3, Scalar(0,0,255),-1,8, nokta(0));
}
if(tik>3)
{

}
  waitKey(0);
 return 0;
}



void getPixelValue(Mat img, int r, int c)
{
 Vec3b pix=img.at<Vec3b>(r,c);
 int B = pix.val[0];
 int G = pix.val[1];
 int R = pix.val[2];
 cout<<"Row:"<<r<<" "<<"Column:"<<c<<" - "; // mouse kordinatlari
 cout<<"B:"<<B<<" "<<"G:"<<G<<" "<<"R:"<<R<<"\n"; // kordinatın pixel değerleri

}

I wait your helps,Thank you

edit flag offensive delete link more

Comments

try my code again and click right button!

sturkmen gravatar imagesturkmen ( 2015-07-28 18:58:59 -0600 )edit

Let me mark out that this is an English based forum, so try avoid placing your comments and variable names in another language ... that makes help just far more difficult. Like @sturkmen said, use his code, it works!

StevenPuttemans gravatar imageStevenPuttemans ( 2015-07-29 02:55:35 -0600 )edit

OK! thank you so much

coldplay gravatar imagecoldplay ( 2015-07-29 03:29:33 -0600 )edit

But I want to ask a questions Code is working it is true But I want to draw a lot of circle(7-8) on the same image But code is drawing only one circle What should I do for this problem

coldplay gravatar imagecoldplay ( 2015-07-29 03:38:33 -0600 )edit

that is because @sturkmen copied not enough of the code :D he forgot the save option for saving a circle. Can you adapt your code?

StevenPuttemans gravatar imageStevenPuttemans ( 2015-07-29 04:04:02 -0600 )edit

correction : "try my code again and click right button!" try the code that i gave on my answer. for best understanding look https://github.com/Itseez/opencv/blob...

PS: many thanks to @StevenPuttemans

sturkmen gravatar imagesturkmen ( 2015-07-29 07:59:13 -0600 )edit

Ok! I have created my code Code is working, but I have little proplem because My circle function is wrong

    Mat circle_eq(Point2f A,Point2f B,Point2f C)
{
    double W[3][3]={{A.x,A.y,1},
                    {B.x,B.y,1},
                    {C.x,C.y,1}};

    double T[3][1]={-(A.x*A.x+A.y*A.y),
                    -(B.x*B.x+B.y*B.y),
                    -(C.x*C.x+C.y*C.y)};
    Mat M=Mat(3,3,CV_64F,W);
    Mat N=Mat(3,1,CV_64F,T);
    Mat L=M.inv()*N;

    return L;
}

How can I do correctly

coldplay gravatar imagecoldplay ( 2015-07-30 15:26:28 -0600 )edit

Please if it is for drawing only, then why do you want to make your circle yourself. Like suggested by @sturkmen, using the built-in circle function does all you want.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-07-31 02:17:19 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-07-19 05:32:03 -0600

Seen: 2,816 times

Last updated: Jul 28 '15