我想在移動並在圖片框中單擊鼠標時獲得一個位置。 我想在圖像窗口中單擊鼠標的時間和位置創建矩形。如何獲得位置並使用opencv繪製矩形?
我有一個簡單的代碼文件
#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
if (event == EVENT_LBUTTONDOWN)
{
cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if(event == EVENT_RBUTTONDOWN)
{
cout << "Right button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if(event == EVENT_MBUTTONDOWN)
{
cout << "Middle button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
}
else if (event == EVENT_MOUSEMOVE)
{
cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;
}
}
int main(int argc, char** argv)
{
bool isDragging = false;
// Read image from file
Mat img = imread("input/pic1.jpg");
//if fail to read the image
if(img.empty())
{
cout << "Error loading the image" << endl;
return -1;
}
//Create a window
namedWindow("My Window", 1);
//set the callback function for any mouse event
setMouseCallback("My Window", CallBackFunc, NULL);
//show the image
imshow("My Window", img);
// Wait until user press some key
waitKey(0);
return 0;
}
它在Windows上工作形成=,但我想用鼠標點擊。我把代碼放在GUI上。它引發以下錯誤:
Error 3 error C3867: 'ProjectFinal::MyForm::CallBackFunc': function call missing argument list; use '&ProjectFinal::MyForm::CallBackFunc' to create a pointer to member c:\users\nungningz\documents\visual studio 2012\projects\projectfinal\projectfinal\MyForm.h 690 1 ProjectFinal
Error 6 error C3867: 'ProjectFinal::MyForm::CallBackFunc': function call missing argument list; use '&ProjectFinal::MyForm::CallBackFunc' to create a pointer to member c:\users\nungningz\documents\visual studio 2012\projects\projectfinal\projectfinal\MyForm.h 690 1 ProjectFinal
7 IntelliSense: a pointer-to-member is not valid for a managed class c:\Users\NungNingZ\Documents\Visual Studio 2012\Projects\ProjectFinal\ProjectFinal\MyForm.h 690 37 ProjectFinal
您的代碼工作正常,我。它看起來像您發佈的代碼與您發佈的錯誤不匹配。您發佈的代碼編譯並運行良好。請發佈顯示這些錯誤的實際代碼。 – Ove
主題中的代碼對我來說也不錯,但在windowsForms上很好。通常我會在使用前測試並開發一個帶有windowsForm的簡單代碼。當我與Gui一起在項目中應用代碼時,他們有問題。必須在setMouseCallback(「My Window」,CallBackFunc,NULL)中推送一個參數;我無法解決問題。幫助我plz – Nungning