0

I want to get the position of a mouse click on an IplImage. I searched for it but I couldn't find anything. There is no source for get mouse position in c# using openCVsharp methods. So I tried something. When the code is started it is waiting for the waiting time at that time I click with mouse but nothing is happining. And after the waitkey the code stops. Is there a way to get the position of a mouse click on an IplImage?

namespace mousedeneeme
{

    class Program
    {           
        static void Main(string[] args)
        {
            IplImage I_clean = Cv.LoadImage("Iclean.tiff", LoadMode.GrayScale);

            String s = Cv.NamedWindow("I_clean image").ToString();

            CvMouseCallback mo = new CvMouseCallback(on_mouse);    

            Cv.SetMouseCallback(s, on_mouse);

            Cv.ShowImage("I_clean image", I_clean);
            Cv.WaitKey(5000);

        }
        public static void on_mouse(MouseEvent me, int x, int y, MouseEvent me7) {

            Console.WriteLine(x);
        }


    }
}

2 Answers 2

1

here is working code

CvWindow w = new CvWindow("OpenCV Example") 

w.OnMouseCallback+=new CvMouseCallback(on_mouse);

use += because you are binding event
Sign up to request clarification or add additional context in comments.

Comments

0

try this

#include "stdafx.h"
#include <cv.h>
#include <highgui.h>


//callback function
void mouseEvent(int evt, int x, int y, int flags, void* param){
    if(evt==CV_EVENT_LBUTTONDOWN){
       printf("%d %d\n",x,y);
     }
}


    int main()
    {

    cvNamedWindow("MyWindow");

    //assigning the callback function for mouse events
    cvSetMouseCallback("MyWindow", mouseEvent, 0);

    //load and display an image
    IplImage* img = cvLoadImage("C:/Iclean.tiff");
    cvShowImage("MyWindow", img);

    //wait for key press
    cvWaitKey(0);

    //cleaning up
    cvDestroyWindow("MyWindow");
    cvReleaseImage(&img);

    return 0;
   } 

2 Comments

Yeah thank you.But I need c# code. I saw cpp examples and opencv examples before.But it didn't help me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.