need help: image coordinates(x,y) in opencv with mfc Pin
-
hi, below is the complete code that i have developed using opencv with MFC in vs2008. it builds fine and after debugging it shows two different windows, one containing image and other containing static boxes. the purpose of this code is to display image coordinates (x,y) in 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
#endifvoid on_mouse( int evt, int x, int y, int flags, void* param ); //defining mouse event
HWND hwnd; //defining handleclass 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; -
hi, below is the complete code that i have developed using opencv with MFC in vs2008. it builds fine and after debugging it shows two different windows, one containing image and other containing static boxes. the purpose of this code is to display image coordinates (x,y) in 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
#endifvoid on_mouse( int evt, int x, int y, int flags, void* param ); //defining mouse event
HWND hwnd; //defining handleclass 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;Use your debugger to check that your mouse event handler gets called when you double click it. Looking at this code it seems to be somewhat complicated by using opencv and MFC; I wonder why you do not just use MFC on its own?
-
Use your debugger to check that your mouse event handler gets called when you double click it. Looking at this code it seems to be somewhat complicated by using opencv and MFC; I wonder why you do not just use MFC on its own?
i have checked it with some other examples. works fine. but for this program it does not display the coordinates in static boxes. am i defining the handle HWND hwnd correctly??
-
i have checked it with some other examples. works fine. but for this program it does not display the coordinates in static boxes. am i defining the handle HWND hwnd correctly??
jawadali477 wrote:
am i defining the handle HWND hwnd correctly?
Yes, but you do not initialise it anywhere. In any case you do not need this variable as you already have the pre-defined
CWnd::m_hWnd
[^] variable which contains the handle to your object's window. -
jawadali477 wrote:
am i defining the handle HWND hwnd correctly?
Yes, but you do not initialise it anywhere. In any case you do not need this variable as you already have the pre-defined
CWnd::m_hWnd
[^] variable which contains the handle to your object's window.can you please elaborate it because i didn't get your point i.e
"pre-defined CWnd::m_hWnd[^] variable which contains the handle to your object's window."
you see, i'm not good in programming with handles. :( -
can you please elaborate it because i didn't get your point i.e
"pre-defined CWnd::m_hWnd[^] variable which contains the handle to your object's window."
you see, i'm not good in programming with handles. :(The pre-defined variable
m_hWnd
already holds the handle to the window of the dialog, so that is the one to use in your calls toSetDlgItemText()
.jawadali477 wrote:
i'm not good in programming with handles.
This is something you need to study as it is fairly key to the whole Windows framework.
-
The pre-defined variable
m_hWnd
already holds the handle to the window of the dialog, so that is the one to use in your calls toSetDlgItemText()
.jawadali477 wrote:
i'm not good in programming with handles.
This is something you need to study as it is fairly key to the whole Windows framework.
thank you Richard MacCutchan for your replies. i did use
m_hWnd
inSetDlgItemText()
in first place, but at that time it gave the error asm_hWnd :undeclared identifier
. you see i'm using a thread to call mouse function. do i need to redefinem_hWnd
?? if yes, where?? -
thank you Richard MacCutchan for your replies. i did use
m_hWnd
inSetDlgItemText()
in first place, but at that time it gave the error asm_hWnd :undeclared identifier
. you see i'm using a thread to call mouse function. do i need to redefinem_hWnd
?? if yes, where??If your mouse handler is not part of the dialog class then you need to store
m_hWnd
into a global variable in yourOnInitDialog()
method. I wonder, however, why you think it necessary to run this in a separate thread and why you are using opencv, when you could accomplish your goal with a much simpler MFC based application. -
If your mouse handler is not part of the dialog class then you need to store
m_hWnd
into a global variable in yourOnInitDialog()
method. I wonder, however, why you think it necessary to run this in a separate thread and why you are using opencv, when you could accomplish your goal with a much simpler MFC based application.thank you Richard MacCatchan for replies. i have implemented MFC based application using picture control. i have included bitmap image in picture control and have defined subclass where i have used
OnLButtonDblClk
to get the co-ordinates of picture control (not image u know) in static box. and it works fine. but i have some questions and need help. 1. how to implement zoom in/out an the scroll option (for large image sizes)? 2. when applying zoom in/out, will the pixel co-ordinates change (i mean if a point is lying on say (x,y) in picture control before zoom in what will happen to its co-ordinates after zoom in)? Regards JAwad -
thank you Richard MacCatchan for replies. i have implemented MFC based application using picture control. i have included bitmap image in picture control and have defined subclass where i have used
OnLButtonDblClk
to get the co-ordinates of picture control (not image u know) in static box. and it works fine. but i have some questions and need help. 1. how to implement zoom in/out an the scroll option (for large image sizes)? 2. when applying zoom in/out, will the pixel co-ordinates change (i mean if a point is lying on say (x,y) in picture control before zoom in what will happen to its co-ordinates after zoom in)? Regards JAwad1. Google should be your first place to look for some suggestions[^]. 2. As far as I know, the position will remain constant, it is just the scale that changes, but again you may wish to try some research to see if there are any suggestions around.