1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
47 //#include <pthread.h>
48 //#include <sys/types.h>
57 #define Tk_GetHWND(id) id
61 int Tk_GetHWND(int win);
66 #pragma warning( disable: 4100 4663 4189 4101 4018 4710 )
73 #define SYSTEM_WIN_FROM_TK_WIN(tkwin) Tk_GetHWND(Tk_WindowId(tkwin))
77 void MainEx(int argc, char **argv, Tcl_AppInitProc *pfappInitProc, Tcl_Interp *pintrp,
79 int Tcl_AppInit(Tcl_Interp *pintrp);
80 void WishPanic TCL_VARARGS_DEF(char *,arg1);
81 void Panic (Tcl_Interp * pintrp, const char * pch);
87 static char* module_path;
90 ///////////////////////////////////////////////////////////////////////////////
91 static char* GetPathFromModuleName( char* modulename )
97 /* if module is link find real path */
100 strcpy( real, modulename );
101 while( (len = readlink( real, real, 1000 )) > 0 )
103 char* path = (char*)malloc( strlen( real ) + 10 );
104 strcpy( path, real );
106 char* path = (char*)malloc( strlen( modulename ) + 10 );
107 strcpy( path, modulename );
110 for( i = strlen( path ) - 1;
111 i > 0 && path[i] != '\\' && path[i] != '/';
124 for( i = 0; i < (int)strlen( path ); i++ )
125 if( path[i] == '\\' )
132 // Callback function ----------------------------------------------------------
133 void testcallback(IplImage* img)
137 lastframe.Create(img->width,img->height,24,img->origin);
138 lastframe.CopyOf(img);
142 if (!view.m_trackinit) {
143 view.StartTracking(lastframe);
144 view.m_trackinit = true;
147 view.TrackFeatures(lastframe);
149 //double kx = (double)view.m_w/img->width;
150 //double ky = (double)view.m_h/img->height;
152 if( !view.m_night_mode )
159 //CvPoint tl = {0,0};
160 //CvPoint br = {img->width,img->height};
161 //cvRectangle(img, tl, br, 0, (img->width > img->height) ? img->width : img->height);
162 //cvRectangle(img, tl, br, 0, 255);
163 memset(img->imageData,0,img->imageSize);
166 int i, count = view.m_tracker.GetCount();
167 const PointArray& array = view.m_tracker.GetPoints();
169 cvCircle( img, P, cvRound(3), CV_RGB(0,0,255));
171 for( i = 0; i < count; i++ )
175 if( i != view.m_moved_idx )
177 pt = cvPoint( cvRound(array[i].x), cvRound(array[i].y));
178 color = CV_RGB(0,255,0);
182 pt = cvPoint( cvRound(view.m_moved_point.x),
183 cvRound(view.m_moved_point.y));
184 color = CV_RGB(255,0,0);
186 cvCircle( img, pt, 1, color, CV_FILLED );
191 if (view.m_trackinit)
194 view.m_trackinit = false;
202 //-----------------------------------------------------------------------------
205 *----------------------------------------------------------------------
210 * TCL_OK - if sucsess, or TCL_ERROR
212 *----------------------------------------------------------------------
215 int Init_Camera (ClientData, Tcl_Interp *interp,
221 char com[1000]="set dlg [Dialog .dlg -parent . -modal local -separator 1 -title \"Choice of cameras\" \
222 -side bottom -anchor s -default 0]";
223 CameraDescription cd;
225 ncameras = cvcamGetCamerasCount();
229 Tcl_Eval(interp,"tk_dialog .pattern {Error} { Cameras not found.} {} 0 OK");
233 ret = Tcl_Eval(interp,com);
235 strcpy(com,"$dlg add -name ok -width 5");
236 ret = Tcl_Eval(interp,com);
238 strcpy(com, "set top [$dlg getframe]");
239 ret = Tcl_Eval(interp,com);
241 strcpy(com, "label $top.lab1 -text \"Several cameras has found in your system. Choose one of them.\" \n\
242 pack $top.lab1 -side top -anchor nw" );
243 ret = Tcl_Eval(interp,com);
245 strcpy(com, "label $top.lab2 -text \"\" \n\
246 pack $top.lab2 -side top -anchor nw");
247 ret = Tcl_Eval(interp,com);
249 strcpy(com, "label $top.lab3 -text \"Cameras:\" \n\
250 pack $top.lab3 -side top -anchor nw");
251 ret = Tcl_Eval(interp,com);
253 strcpy(com, "ComboBox $top.cb -width 50 -height 4 -editable no -modifycmd CVLkDemo::Modify");
254 ret = Tcl_Eval(interp,com);
256 strcpy(com, "pack $top.cb -side top");
257 ret = Tcl_Eval(interp,com);
259 strcpy(com, "$top.cb configure -values {");
260 for (int i=0; i<ncameras; i++)
262 cvcamGetProperty(i, CVCAM_DESCRIPTION, (void*)&cd);
264 strcat(com,cd.DeviceDescription);
268 ret = Tcl_Eval(interp, com);
270 strcpy(com,"$top.cb setvalue @0 \n CVLkDemo::Modify \n set ret [$dlg draw]");
271 ret = Tcl_Eval(interp, com);
273 strcpy(com,"destroy $dlg");
274 ret = Tcl_Eval(interp, com);
276 ret = Tcl_Eval(interp, "set tmp $CVLkDemo::cam");
277 int n = atoi(interp->result);
279 ret = Tcl_Eval(interp, "set f $CVLkDemo::curframe");
281 win = Tk_NameToWindow(interp, interp->result,
282 Tk_MainWindow(interp));
285 int w = Tk_Width(win);
286 int h = Tk_Height(win);
288 mainwin = SYSTEM_WIN_FROM_TK_WIN(win);
291 cvcamSetProperty(n, CVCAM_PROP_ENABLE, CVCAMTRUE);
292 cvcamSetProperty(n, CVCAM_PROP_RENDER, CVCAMTRUE);
293 cvcamSetProperty(n, CVCAM_PROP_WINDOW, &mainwin);
294 cvcamSetProperty(n, CVCAM_PROP_CALLBACK, (void*)testcallback);
295 cvcamSetProperty(n, CVCAM_RNDWIDTH, (void*)&w);
296 cvcamSetProperty(n, CVCAM_RNDHEIGHT, (void*)&h);
300 cvcamGetProperty(n, CVCAM_DESCRIPTION, (void*)&cd);
301 sprintf(com,"set CVLkDemo::curcam \"%s\"",cd.DeviceDescription);
302 Tcl_Eval(interp, com);
307 int Close_Camera (ClientData, Tcl_Interp *,
310 view.m_started = false;
315 int Start_Camera (ClientData, Tcl_Interp *,
318 view.m_started = true;
323 int Stop_Camera (ClientData, Tcl_Interp *,
326 view.m_started = false;
331 int Set_Track (ClientData, Tcl_Interp *,
335 //view.StartTracking(lastframe);
339 int Reset_Track (ClientData, Tcl_Interp *,
342 //view.StopTracking();
343 view.m_track = false;
348 int Set_Night (ClientData, Tcl_Interp *,
351 view.m_night_mode = true;
355 int Reset_Night (ClientData, Tcl_Interp *,
358 view.m_night_mode = false;
363 int Set_Video (ClientData, Tcl_Interp *,
367 cvcamGetProperty(0,CVCAM_VIDEOFORMAT, p);
372 int Set_CameraOpt (ClientData, Tcl_Interp *,
376 cvcamGetProperty(0,CVCAM_CAMERAPROPS, p);
381 int Button_Press (ClientData, Tcl_Interp *,
388 p.y = view.m_h - p.y;
390 P = view.ConvertScreenToImage(p);
394 int index = view.FindPoint(P);
397 view.m_moved_idx = index;
398 view.m_moved_point = P;
400 else if( view.m_gray.GetImage() )
402 view.m_tracker.AddPoint( P, view.m_gray );
408 int Button_Release (ClientData, Tcl_Interp *,
415 p.y = view.m_h - p.y;
421 if( view.m_moved_idx > 0 && view.m_gray.GetImage() )
423 view.m_tracker.MovePoint( view.m_moved_idx, view.m_moved_point, view.m_gray );
424 view.m_moved_idx = -1;
431 int Motion (ClientData, Tcl_Interp *,
438 p.y = view.m_h - p.y;
440 P = view.ConvertScreenToImage(p);
443 if( view.m_moved_idx > 0 && view.m_gray.GetImage() )
445 view.m_moved_point.x = P.x;
446 view.m_moved_point.y = P.y;
453 int Configure (ClientData, Tcl_Interp *interp,
459 Tcl_Eval(interp, "set f $CVLkDemo::curframe");
461 win = Tk_NameToWindow(interp, interp->result,
462 Tk_MainWindow(interp));
465 int w = Tk_Width(win);
466 int h = Tk_Height(win);
468 cvcamSetProperty(0, CVCAM_RNDWIDTH, (void*)&w);
469 cvcamSetProperty(0, CVCAM_RNDHEIGHT, (void*)&h);
477 //-----------------------------------------------------------------------------
480 *----------------------------------------------------------------------
482 * Commands_Init -- Commands initialisation routine.
485 * TCL_OK - if sucsess, or TCL_ERROR
487 *----------------------------------------------------------------------
490 Commands_Init(Tcl_Interp *interp)
492 Tcl_CreateCommand (interp, "pInitCamera", Init_Camera,
493 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
494 Tcl_CreateCommand (interp, "pStartCamera", Start_Camera,
495 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
496 Tcl_CreateCommand (interp, "pCloseCamera", Close_Camera,
497 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
498 Tcl_CreateCommand (interp, "pStopCamera", Stop_Camera,
499 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
500 Tcl_CreateCommand (interp, "pSetTrack", Set_Track,
501 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
502 Tcl_CreateCommand (interp, "pResetTrack", Reset_Track,
503 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
504 Tcl_CreateCommand (interp, "pSetNight", Set_Night,
505 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
506 Tcl_CreateCommand (interp, "pResetNight", Reset_Night,
507 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
508 Tcl_CreateCommand (interp, "pSetVideo", Set_Video,
509 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
510 Tcl_CreateCommand (interp, "pButtonPress", Button_Press,
511 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
512 Tcl_CreateCommand (interp, "pButtonRelease", Button_Release,
513 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
514 Tcl_CreateCommand (interp, "pMotion", Motion,
515 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
516 Tcl_CreateCommand (interp, "pSetCameraOpt", Set_CameraOpt,
517 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
518 Tcl_CreateCommand (interp, "pConfigure", Configure,
519 (ClientData *) NULL, (Tcl_CmdDeleteProc *) NULL);
527 *----------------------------------------------------------------------
529 * MainEx -- Main program for Tk-based applications.
531 *----------------------------------------------------------------------
534 MainEx( int, char**, Tcl_AppInitProc *appInitProc,
535 Tcl_Interp *pintrp, char *fileName )
539 (*appInitProc)(pintrp);
541 if ( Commands_Init(pintrp) != TCL_OK)
542 Panic (pintrp,"Can't initialise commands!");
545 strcat( strcat( strcpy( set_path, "set ::image_path \"" ), module_path ), "\"" );
546 code = Tcl_Eval( pintrp, set_path );
548 if (fileName != NULL)
551 strcat( strcat( strcpy( script, module_path ), "" ), fileName );
552 code = Tcl_EvalFile(pintrp, script);
554 Panic (pintrp,"Evaluate file error!");
556 else Tcl_SourceRCFile(pintrp);
558 Tcl_ResetResult(pintrp);
562 Tcl_DeleteInterp(pintrp);
567 *----------------------------------------------------------------------
569 * WishPanic -- escape function.
571 *----------------------------------------------------------------------
574 void WishPanic TCL_VARARGS_DEF(char *,arg1)
580 format = TCL_VARARGS_START(char *,arg1,argList);
581 printf(buf, format, argList);
585 *----------------------------------------------------------------------
587 * Panic -- error output & exit function.
589 *----------------------------------------------------------------------
592 void Panic (Tcl_Interp * pintrp, const char * pch)
594 printf("Thread %P:",Tcl_GetCurrentThread());
596 printf("\n Reason:");
597 printf(pintrp->result);
600 Tcl_DeleteInterp(pintrp);
604 /*void Panic (Tcl_Interp * pintrp, const char * pch)
607 sprintf(buf, "\nThread %p, Interpetator %p:\n",
608 Tcl_GetCurrentThread(), pintrp);
610 Tcl_AddErrorInfo(pintrp, buf);
611 Tcl_AddErrorInfo(pintrp, pch);
612 TkpDisplayWarning(Tcl_GetVar(pintrp, "errorInfo",
613 TCL_GLOBAL_ONLY), "Error in startup script");
614 Tcl_DeleteInterp(pintrp);
619 *----------------------------------------------------------------------
621 * Tcl_AppInit -- Initialisation function.
623 *----------------------------------------------------------------------
626 int Tcl_AppInit(Tcl_Interp *pintrp)
629 if (Tcl_InitStubs(pintrp, TCL_VERSION, 1) == NULL)
630 Panic (pintrp,"Tcl stub's initialisation failed!");
632 if (Tcl_Init(pintrp) == TCL_ERROR)
633 Panic (pintrp,"Tcl's initialisation failed!");
635 if (Tk_Init(pintrp) == TCL_ERROR)
636 Panic (pintrp,"Tk's initialisation failed!");
641 // main body ------------------------------------------------------------------
643 int main(int argc, char* argv[])
645 module_path = GetPathFromModuleName( argv[0] );
647 Tcl_Interp* g_pInterp = Tcl_CreateInterp();
649 Tcl_SetPanicProc(WishPanic);
650 Tcl_FindExecutable(argv[0]);
652 MainEx(argc, argv, Tcl_AppInit, g_pInterp, "cvlkdemo.tcl");