Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. help: image coordinates(x,y) in opencv with mfc

help: image coordinates(x,y) in opencv with mfc

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J jawadali477

    hi i have developed program using opencv with mfc and i can't get the coordinates (x,y) of image in the static box using mouse click. image opens in separate window. the code is as:

    void on_mouse( int evt, int x, int y, int flags, void* param )
    {
    if (evt = CV_EVENT_LBUTTONDBLCLK)
    {
    CString Blue, Green;
    Blue.Format(_T("%d"), x);
    Green.Format(_T("%d"), y);
    SetDlgItemText(m_hWnd, IDC_Blue, Blue);
    SetDlgItemText(m_hWnd, IDC_Green, Green);
    }

    }

    the handle

    HWND m_hWnd;

    is defined at beginning where the header files are included (i might be doing it wrong since i'm not good at programming with handle). the mouse callback function is defined in a thread as

    cvSetMouseCallback("box.png", on_mouse, 0);

    any idea what is wrong with this code. Regards Jawad

    S Offline
    S Offline
    SoMad
    wrote on last edited by
    #2

    You are not specifying exactly what the problem is, but I am going to guess that you look at the x and y values and determine that they are incorrect. If that is the case, try calling ScreenToClient()[^] to convert the screen coordinates to client coordinates of your dialog. You have to pass in the m_hWnd of you dialog as the first parameter. Soren Madsen

    1 Reply Last reply
    0
    • J jawadali477

      hi i have developed program using opencv with mfc and i can't get the coordinates (x,y) of image in the static box using mouse click. image opens in separate window. the code is as:

      void on_mouse( int evt, int x, int y, int flags, void* param )
      {
      if (evt = CV_EVENT_LBUTTONDBLCLK)
      {
      CString Blue, Green;
      Blue.Format(_T("%d"), x);
      Green.Format(_T("%d"), y);
      SetDlgItemText(m_hWnd, IDC_Blue, Blue);
      SetDlgItemText(m_hWnd, IDC_Green, Green);
      }

      }

      the handle

      HWND m_hWnd;

      is defined at beginning where the header files are included (i might be doing it wrong since i'm not good at programming with handle). the mouse callback function is defined in a thread as

      cvSetMouseCallback("box.png", on_mouse, 0);

      any idea what is wrong with this code. Regards Jawad

      S Offline
      S Offline
      SoMad
      wrote on last edited by
      #3

      You are not specifying exactly what the problem is, but I am going to guess that you look at the x and y values and determine that they are incorrect. If that is the case, try calling ScreenToClient()[^] to convert the screen coordinates to client coordinates of your dialog. You have to pass in the m_hWnd of you dialog as the first parameter. Soren Madsen

      J 1 Reply Last reply
      0
      • S SoMad

        You are not specifying exactly what the problem is, but I am going to guess that you look at the x and y values and determine that they are incorrect. If that is the case, try calling ScreenToClient()[^] to convert the screen coordinates to client coordinates of your dialog. You have to pass in the m_hWnd of you dialog as the first parameter. Soren Madsen

        J Offline
        J Offline
        jawadali477
        wrote on last edited by
        #4

        sorry for not well explained actually the problem is that static box does not show any value when mouse is being clicked. means it remains unchanged as it was. Jawad

        S 2 Replies Last reply
        0
        • J jawadali477

          sorry for not well explained actually the problem is that static box does not show any value when mouse is being clicked. means it remains unchanged as it was. Jawad

          S Offline
          S Offline
          SoMad
          wrote on last edited by
          #5

          Oh, I see what you mean now. You have a bug right there in your code. I am not sure if you get all mouse events in this handler (including mouse move events), but you should fix it and see if that solves your problem. Change

          if (evt = CV_EVENT_LBUTTONDBLCLK)

          to

          if (evt == CV_EVENT_LBUTTONDBLCLK)

          Soren Madsen

          D 1 Reply Last reply
          0
          • J jawadali477

            sorry for not well explained actually the problem is that static box does not show any value when mouse is being clicked. means it remains unchanged as it was. Jawad

            S Offline
            S Offline
            SoMad
            wrote on last edited by
            #6

            I just thought about something. You mention that the m_hWnd handle is defined, but do you actually assign it to the m_hWnd of your dialog object? Soren Madsen

            J 1 Reply Last reply
            0
            • S SoMad

              Oh, I see what you mean now. You have a bug right there in your code. I am not sure if you get all mouse events in this handler (including mouse move events), but you should fix it and see if that solves your problem. Change

              if (evt = CV_EVENT_LBUTTONDBLCLK)

              to

              if (evt == CV_EVENT_LBUTTONDBLCLK)

              Soren Madsen

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #7

              SoMad wrote:

              ...but you should fix it and see if that solves your problem.

              True it should be fixed, but it won't make a difference as the expression (evt = CV_EVENT_LBUTTONDBLCLK) always evaluates to true.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

              S 1 Reply Last reply
              0
              • J jawadali477

                hi i have developed program using opencv with mfc and i can't get the coordinates (x,y) of image in the static box using mouse click. image opens in separate window. the code is as:

                void on_mouse( int evt, int x, int y, int flags, void* param )
                {
                if (evt = CV_EVENT_LBUTTONDBLCLK)
                {
                CString Blue, Green;
                Blue.Format(_T("%d"), x);
                Green.Format(_T("%d"), y);
                SetDlgItemText(m_hWnd, IDC_Blue, Blue);
                SetDlgItemText(m_hWnd, IDC_Green, Green);
                }

                }

                the handle

                HWND m_hWnd;

                is defined at beginning where the header files are included (i might be doing it wrong since i'm not good at programming with handle). the mouse callback function is defined in a thread as

                cvSetMouseCallback("box.png", on_mouse, 0);

                any idea what is wrong with this code. Regards Jawad

                A Offline
                A Offline
                Albert Holguin
                wrote on last edited by
                #8

                jawadali477 wrote:

                the handle

                HWND m_hWnd;

                is defined at beginning where the header files are included (i might be doing it wrong since i'm not good at programming with handle).

                When do you define it? It has to be defined from the window handle AFTER it has been created (when using MFC, it means it should come after your window's OnInitDialog() has been called), otherwise it could be undefined. FYI... your coding style seems more C-like than C++. Is your function global? Did you declare HWND m_hWnd a global variable? I hope you didn't do that because all MFC window based objects have a variable name m_hWnd.

                J 1 Reply Last reply
                0
                • D David Crow

                  SoMad wrote:

                  ...but you should fix it and see if that solves your problem.

                  True it should be fixed, but it won't make a difference as the expression (evt = CV_EVENT_LBUTTONDBLCLK) always evaluates to true.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                  S Offline
                  S Offline
                  SoMad
                  wrote on last edited by
                  #9

                  Yes, I had originally written that in my message, but took it out. At the time I was thinking there might be more code in this function that he had not included - like this could have been an "else if" with an "if" above it having a similar error. Otherwise this code would be called all the time since the handler processes all mouse events including mouse move. I think the problem has to do with the window handle and I don't really like this call from a separate thread. Soren Madsen

                  1 Reply Last reply
                  0
                  • J jawadali477

                    hi i have developed program using opencv with mfc and i can't get the coordinates (x,y) of image in the static box using mouse click. image opens in separate window. the code is as:

                    void on_mouse( int evt, int x, int y, int flags, void* param )
                    {
                    if (evt = CV_EVENT_LBUTTONDBLCLK)
                    {
                    CString Blue, Green;
                    Blue.Format(_T("%d"), x);
                    Green.Format(_T("%d"), y);
                    SetDlgItemText(m_hWnd, IDC_Blue, Blue);
                    SetDlgItemText(m_hWnd, IDC_Green, Green);
                    }

                    }

                    the handle

                    HWND m_hWnd;

                    is defined at beginning where the header files are included (i might be doing it wrong since i'm not good at programming with handle). the mouse callback function is defined in a thread as

                    cvSetMouseCallback("box.png", on_mouse, 0);

                    any idea what is wrong with this code. Regards Jawad

                    J Offline
                    J Offline
                    jawadali477
                    wrote on last edited by
                    #10

                    hi, below is the complete code that i have developed. it builds fine and after debugging it shows two different windows, one containing image and other containing static boxes. but when i double click on the image window (which is my mouse event), values of (x,y) coordinates are not displayed in the static boxes (static boxes are in the separate window). please pardon my mistakes.

                    #include "stdafx.h"
                    #include "opencv01.h"
                    #include "opencv01Dlg.h"
                    #include "highgui.h"
                    #include "afxwin.h"
                    #include "cv.h"
                    #include "math.h"
                    #include

                    #ifdef _DEBUG
                    #define new DEBUG_NEW
                    #endif

                    void on_mouse( int evt, int x, int y, int flags, void* param ); //defining mouse event
                    HWND hwnd; //defining handle

                    class CAboutDlg : public CDialog
                    {
                    public:
                    CAboutDlg();

                    enum { IDD = IDD\_ABOUTBOX };
                    
                    protected:
                    virtual void DoDataExchange(CDataExchange\* pDX);    // DDX/DDV support
                    

                    protected:
                    DECLARE_MESSAGE_MAP()
                    };

                    CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
                    {
                    }

                    void CAboutDlg::DoDataExchange(CDataExchange* pDX)
                    {
                    CDialog::DoDataExchange(pDX);
                    }

                    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
                    END_MESSAGE_MAP()

                    Copencv01Dlg::Copencv01Dlg(CWnd* pParent /*=NULL*/)
                    : CDialog(Copencv01Dlg::IDD, pParent)
                    {
                    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
                    }

                    void Copencv01Dlg::DoDataExchange(CDataExchange* pDX)
                    {
                    CDialog::DoDataExchange(pDX);
                    }

                    BEGIN_MESSAGE_MAP(Copencv01Dlg, CDialog)
                    ON_WM_SYSCOMMAND()
                    ON_WM_PAINT()
                    ON_WM_QUERYDRAGICON()
                    //}}AFX_MSG_MAP
                    END_MESSAGE_MAP()

                    // Copencv01Dlg message handlers

                    BOOL Copencv01Dlg::OnInitDialog()
                    {
                    CDialog::OnInitDialog();
                    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
                    ASSERT(IDM_ABOUTBOX < 0xF000);

                    CMenu\* pSysMenu = GetSystemMenu(FALSE);
                    if (pSysMenu != NULL)
                    {
                    	CString strAboutMenu;
                    	strAboutMenu.LoadString(IDS\_ABOUTBOX);
                    	if (!strAboutMenu.IsEmpty())
                    	{
                    		pSysMenu->AppendMenu(MF\_SEPARATOR);
                    		pSysMenu->AppendMenu(MF\_STRING, IDM\_ABOUTBOX, strAboutMenu);
                    	}
                    }
                    
                    SetIcon(m\_hIcon, TRUE);			// Set big icon
                    SetIcon(m\_hIcon, FALSE);		// Set small icon
                    
                    
                    AfxBeginThread(MyThreadProc, this); //calling thread
                    
                    return TRUE;  // return TRUE  unless you set the focus to a control
                    

                    }

                    UINT Copencv01Dlg::MyThreadProc(LPVOID pParam)
                    {
                    Copencv01Dlg * me = (Copencv01Dlg *)pParam;
                    me->MyThreadProc();
                    return TRUE;
                    }

                    void Copencv01Dlg::MyThreadProc()
                    {
                    //image initialization

                    IplImage\* img = cvLoadImage("box.png", CV\_WINDOW\_AUTOSIZE);
                    
                    1 Reply Last reply
                    0
                    • A Albert Holguin

                      jawadali477 wrote:

                      the handle

                      HWND m_hWnd;

                      is defined at beginning where the header files are included (i might be doing it wrong since i'm not good at programming with handle).

                      When do you define it? It has to be defined from the window handle AFTER it has been created (when using MFC, it means it should come after your window's OnInitDialog() has been called), otherwise it could be undefined. FYI... your coding style seems more C-like than C++. Is your function global? Did you declare HWND m_hWnd a global variable? I hope you didn't do that because all MFC window based objects have a variable name m_hWnd.

                      J Offline
                      J Offline
                      jawadali477
                      wrote on last edited by
                      #11

                      hi, i'm still stuck with the dialog handle. i have posted the complete code. please i need help. Regards Jawad

                      1 Reply Last reply
                      0
                      • S SoMad

                        I just thought about something. You mention that the m_hWnd handle is defined, but do you actually assign it to the m_hWnd of your dialog object? Soren Madsen

                        J Offline
                        J Offline
                        jawadali477
                        wrote on last edited by
                        #12

                        hi, i'm still stuck with the dialog handle. i have posted the complete code. please i need help. Regards Jawad

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups