Visual C++ .NET - OpenCV desconosco este error

 
Vista:

OpenCV desconosco este error

Publicado por Ricardo (1 intervención) el 27/03/2016 12:25:59
Hola tengo un problema con este codigo de openCV espero me puedan ayudar y muchas gracias saludos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using namespace std;
using namespace cv;
Mat img; //Variable matriz
int color=255;
string cColor;
bool clickado,ldown = false, lup = false;
 
Point corner1;
void createTrackbar();
void on_trackbar(int, void*);
 
void createTrackbar()
{
 
}
 
void on_trackbar(int, void*)
{//This function gets called whenever a
       // trackbar position is changed
       cout << color << endl;
}
 
static void MouseEvent(int event, int x, int y, int, void*){
 
	createTrackbar("Ajustar Color", "Eventos del raton", &color, 255,on_trackbar);
	Mat img(500,800,CV_8UC3,Scalar::all(0));
	string msg="Mouse Movido: X:"+to_string(x)+" y Y:"+to_string(y);
	/*Mat img_botonsito1(300,500,CV_8UC3,Scalar::all(0));
	Mat img_botonsito2(300,500,CV_8UC3,Scalar::all(0));*/
 
	//Boton aceptar
	rectangle(img, Point(70,45), Point(160,45), CV_RGB(255,110,98), 80);
	putText(img,"Aceptar",Point(50,55),FONT_HERSHEY_COMPLEX,1,CV_RGB(255,255,255),0);
 
	//Boton eliminar
	rectangle(img, Point(70,135), Point(160,135), CV_RGB(255,110,98), 80);
	putText(img,"Eliminar",Point(45,145),FONT_HERSHEY_COMPLEX,1,CV_RGB(255,255,255),0);
 
	//Boton Rojo
	rectangle(img, Point(70,230), Point(70,230), CV_RGB(255,0,0), 80);
 
	//Boton azul
	rectangle(img, Point(70,330), Point(70,330), CV_RGB(0,0,255), 80);
 
	//Boton verde
	rectangle(img, Point(70,430), Point(70,430), CV_RGB(0,255,0), 80);
 
	//Boton2 //CV_RGB(96,60,200)
	rectangle(img, Point(500,200), Point(750,250), CV_RGB(color,0,0), 80);
	rectangle(img, Point(510,210), Point(740,240), CV_RGB(0,50,0), 80);
	putText(img,"Boton 2",Point(550,230),FONT_HERSHEY_COMPLEX,1,CV_RGB(255,0,0),0);
 
	putText(img,msg,Point(30,30),FONT_HERSHEY_COMPLEX,1,CV_RGB(255,0,0),0);
 
 
 
	switch (event)
       {
       case CV_EVENT_MOUSEMOVE:
			//putText(img, msg, Point(10,30), 4, 1, CV_RGB(0,255,0), 0);
 
		   break;
       case CV_EVENT_LBUTTONDOWN   :
		   break;
       case CV_EVENT_RBUTTONDOWN   :  break;
       case CV_EVENT_MBUTTONDOWN   :  break;
       case CV_EVENT_LBUTTONUP     :  break;
       case CV_EVENT_RBUTTONUP     :  break;
       case CV_EVENT_MBUTTONUP     :  break;
       case CV_EVENT_LBUTTONDBLCLK :  break;
       case CV_EVENT_RBUTTONDBLCLK :  break;
       case CV_EVENT_MBUTTONDBLCLK :  break;
       }
 
	if(event==EVENT_LBUTTONDOWN){
		ldown = true;
		corner1.x = x;
		corner1.y = y;
 
		if(x<=205 && x>=30 && y<=85 && y>=5 ){
			cout<<"Aceptar"<<endl;
			clickado=true;
		}
 
		else if(x<=200 && x>=30 && y<=175 && y>=95 ){
			cout<<"Eliminar"<<endl;
			clickado=false;
		}
 
		//Boton rojo
		else if(x<=110 && x>=30 && y<=265 && y>=190){
			cColor="rojo";
			cout<<"Rojo seleccionado"<<endl;
			//clickado=true;
		}
 
		//Boton azul
		else if(x<=110 && x>=30 && y<=365 && y>=290){
			cout<<"Azul seleccionado"<<endl;
			cColor="azul";
			clickado=true;
		}
 
		//Boton verde
		else if(x<=110 && x>=30 && y<=465 && y>=390){
			cout<<"Verde seleccionado"<<endl;
			cColor="verde";
			clickado=true;
		}
 
		else if(x<=760 && x>=460 && y<=280 && y>=165 ){
			cout<<"Soy el boton"<<endl;
		}
 
		else{
				cout<<"Clickea una zona valida"<<endl;
		}
 
	}
 
	if(clickado){
		cout<<"Clickaste algo"<<endl;
		if(cColor=="rojo"){
			cout<<"Fue rojo"<<endl;
			rectangle(img, Point(230,75), Point(280,75), CV_RGB(color,0,22), 80);
		}
 
		else if(cColor=="azul"){
			cout<<"Fue azul"<<endl;
			rectangle(img, Point(230,75), Point(280,75), CV_RGB(45,16,color), 80);
		}
 
		else if(cColor=="verde"){
			cout<<"Fue verde"<<endl;
			rectangle(img, Point(230,75), Point(280,75), CV_RGB(45,color,22), 80);
		}
	}
 
	//Declarar evento cuando s e deja de hace click en la imagen
	if (event == EVENT_LBUTTONUP){
		lup = true;
	}
 
	if(ldown == true && lup == false){
		Mat local_img = img.clone();//clonar la pantaylla
		rectangle(local_img, corner1,Point(x,y),CV_RGB(0,255,0));
		//Mostrar donde esta posicionado el mouse
		putText(local_img,"X1: " + to_string(corner1.x) + " Y1: " + to_string(corner1.y), Point(2,20), 4, 1, RGB(0,255,0),1);
		putText(local_img,"X1: " + to_string(x) + " Y1: " + to_string(y), Point(2,80), 4, 1, RGB(0,255,0),1);
		//titulo de la ventana
		imshow("Eventos del raton",local_img);
	}
 
	if(ldown == true && lup == true){
		Mat local_img = img.clone();//clonar la pantalla	
		rectangle(local_img, corner1,Point(x,y),CV_RGB(250,0,0),CV_FILLED);
		rectangle(local_img, Point(corner1.x + 10, corner1.y + 10),Point(x - 10, y - 10),CV_RGB(200,0,0),CV_FILLED);
		//titulo de la ventana
		imshow("Eventos del raton", local_img);
		ldown = false;
		lup = false;
	}
 
	//imshow("Eventos del raton", img);
 
}
 
/*Cuando nosotros retornamos algo a la funcion main, indicamos que el programa retorna algo*/
int main(void){
 
	do{
		//namedWindow("Eventos del raton", CV_WINDOW_AUTOSIZE);//Creando ventana de tamaño automatico
		imshow("Eventos del raton", img);
		setMouseCallback("Eventos del raton", MouseEvent);
			//createTrackbars();
		on_trackbar(color,NULL);
		waitKey();
	}
	while(1);
}

lo que debe hacer es generar un boton con el mouse y que se seleccione un color de los botones rojo, verde y azul y al clickar en aceptar debe de mostrar un boton del boton seleccionado, pero cuando le doy en eliminar no lo hace y se queda asi espero puedan ayudarme se los agradeceria bastante
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Alejandro

Problema con OpenCV y eventos d

Publicado por Alejandro (265 intervenciones) el 14/02/2024 00:15:13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
Mat img; // Variable matriz
int color = 255;
string cColor;
bool clickado, ldown = false, lup = false;
 
Point corner1;
 
void createTrackbar();
void on_trackbar(int, void*);
static void MouseEvent(int event, int x, int y, int, void*);
 
void createTrackbar()
{
    // Aquí puedes agregar código para la creación de trackbars si es necesario
}
 
void on_trackbar(int, void*)
{
    // Esta función se llama cuando se cambia la posición del trackbar
    cout << color << endl;
}
 
static void MouseEvent(int event, int x, int y, int, void*)
{
    createTrackbar("Ajustar Color", "Eventos del raton", &color, 255, on_trackbar);
 
    Mat img(500, 800, CV_8UC3, Scalar::all(0));
    string msg = "Mouse Movido: X:" + to_string(x) + " y Y:" + to_string(y);
 
    // Código para dibujar botones y manejar eventos del ratón
    // ...
 
    // Si se ha hecho clic en Aceptar
    if (clickado)
    {
        cout << "Clickaste algo" << endl;
 
        if (cColor == "rojo")
        {
            cout << "Fue rojo" << endl;
            rectangle(img, Point(230, 75), Point(280, 75), CV_RGB(color, 0, 22), 80);
        }
        else if (cColor == "azul")
        {
            cout << "Fue azul" << endl;
            rectangle(img, Point(230, 75), Point(280, 75), CV_RGB(45, 16, color), 80);
        }
        else if (cColor == "verde")
        {
            cout << "Fue verde" << endl;
            rectangle(img, Point(230, 75), Point(280, 75), CV_RGB(45, color, 22), 80);
        }
    }
 
    // Resto del código para manejar eventos del ratón y dibujar en la imagen
    // ...
 
    imshow("Eventos del raton", img);
}
 
int main(void)
{
    do
    {
        imshow("Eventos del raton", img);
        setMouseCallback("Eventos del raton", MouseEvent);
        createTrackbar(); // Llama a la función de creación de trackbars si es necesario
        on_trackbar(color, NULL);
        waitKey();
    } while (1);
 
    return 0;
}

Ricardo, este código tiene algunas modificaciones para manejar correctamente los eventos del ratón y la lógica de dibujo en la imagen. Asegúrate de ajustar el código según tus necesidades y verifica si resuelve el problema que estás experimentando.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar