Come posso ottenere la posizione e disegnare il rettangolo usando opencv?

Voglio ottenere una posizione quando si sposta e fare clic con il mouse nella casella immagine. Voglio creare un rettangolo nella finestra dell’immagine quando e dove si fa clic su un mouse.

Ho un semplice codice dal documento

#include "stdafx.h" #include "opencv2/highgui/highgui.hpp" #include  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; } 

Funziona su windows form =, ma voglio usare i clic del mouse. Ho messo il codice sulla GUI. Genera il seguente errore:

Errore 3 Errore C3867: ‘ProjectFinal :: MyForm :: CallBackFunc’: elenco di argomenti mancanti alla chiamata di funzione; usa ‘& ProjectFinal :: MyForm :: CallBackFunc’ per creare un puntatore al membro c: \ users \ nungningz \ documents \ visual studio 2012 \ projects \ projectfinal \ projectfinal \ MyForm.h 690 1 ProjectFinal

Errore C3867 dell’errore 6: “ProjectFinal :: MyForm :: CallBackFunc”: elenco di argomenti mancanti di chiamata di funzione; usa ‘& ProjectFinal :: MyForm :: CallBackFunc’ per creare un puntatore al membro c: \ users \ nungningz \ documents \ visual studio 2012 \ projects \ projectfinal \ projectfinal \ MyForm.h 690 1 ProjectFinal

7 IntelliSense: un puntatore a membro non è valido per una class gestita c: \ Users \ NungNingZ \ Documents \ Visual Studio 2012 \ Projects \ ProjectFinal \ ProjectFinal \ MyForm.h 690 37 ProjectFinal

Quindi hai un problema non correlato alla tua domanda.

Tuttavia, puoi raggiungere il tuo objective utilizzando solo i servizi OpenGV highgui:

 #include  #include  using namespace std; using namespace cv; vector rects; bool bDraw; Rect r; Point base; Mat3b img; Mat3b layer; Mat3b working; 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; // Init your rect base.x = x; base.y = y; rx = x; ry = y; r.width = 0; r.height = 0; bDraw = true; } else if ( event == EVENT_MOUSEMOVE ) { cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl; // If drawing, update rect width and height if(!bDraw) return; int dx = abs(rx - x); int dy = abs(ry - y); if(x < base.x) { rx = x; r.width = abs(x - base.x); } else { r.width = dx; } if(y < base.y) { ry = y; r.height = abs(y - base.y); } else { r.height = dy; } // Refresh working = layer.clone(); rectangle(working, r, Scalar(0,255,0)); imshow("My Window", working); } else if ( event == EVENT_LBUTTONUP) { cout << "Left button released" << endl; // Save rect, draw it on layer rects.push_back(r); rectangle(layer, r, Scalar(0,255,255)); r = Rect(); bDraw = false; // Refresh working = layer.clone(); rectangle(working, r, Scalar(0,255,0)); imshow("My Window", working); } } int main(int argc, char** argv) { bool bDraw = false; bool isDragging = false; // Read image from file img = imread("path_to_image"); // initialize your temp images layer = img.clone(); working = img.clone(); //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", working); // Wait until user presses 'q' while((waitKey(1) & 0xFF) != 'q'); return 0; }