Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phSimpleVision.cpp

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission : 
00003         Realtime Vision Processing System
00004     
00005     Copyright (C) 2003-2006 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008     
00009     This file is part of Phission.
00010 
00011     Phission is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU Lesser General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     Phission is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019     GNU Lesser General Public License for more details.
00020 
00021     You should have received a copy of the GNU Lesser General Public License
00022     along with Phission; if not, write to the Free Software
00023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 
00025  ---------------------------------------------------------------------------*/
00030 #ifdef HAVE_CONFIG_H
00031     #include <phissionconfig.h>
00032 #endif
00033 
00034 #include <phStandard.h>
00035 
00036 #if defined(WIN32)
00037     #include <windows.h>
00038 #endif
00039 
00040 /* Capture/Display includes */
00041 #ifdef PH_HAVE_V4L
00042     #include <V4LCapture.h>
00043 #endif /* PH_HAVE_V4L */
00044 
00045 #ifdef PH_HAVE_VFW
00046     #include <VFWSource.h>
00047 #endif /* PH_HAVE_VFW */
00048 
00049 #ifdef PH_HAVE_GDI
00050     #include <GDIDisplay.h>
00051 #else
00052     #ifdef PH_HAVE_X11
00053         #include <X11Display.h>
00054     //#else 
00055     //    #error "We're trying to build without X11 or GDI; not possible!!"
00056     #endif /* PH_HAVE_X11 */
00057 #endif /* PH_HAVE_GDI */
00058 
00059 #include <NetDisplay.h>
00060 
00061 /* Filter includes */
00062 #include <add_Filter.h>
00063 #include <convert_Filter.h>
00064 #include <empty_Filter.h>
00065 #include <inverse_Filter.h>
00066 #include <meanNxN_Filter.h>
00067 #include <medianNxN_Filter.h>
00068 #include <motion_Filter.h>
00069 #include <gaussian3x3_Filter.h>
00070 #include <sobel_Filter.h>
00071 #include <subtract_Filter.h>
00072 #include <threshold_Filter.h>
00073 
00074 #if PH_HAVE_OPENCV
00075     #include <cv_canny_Filter.h>
00076 #else
00077     #include <canny_Filter.h>
00078 #endif
00079 
00080 /* General includes */
00081 #include <phSimpleVision.h>
00082 #include <phError.h>
00083 #include <phMemory.h>
00084 #include <phPrint.h>
00085 
00086 #if defined(PH_HAVE_GDI) || defined(PH_HAVE_X11)
00087     #define phSimpleVision_nDisplays 2
00088 #else
00089     #define phSimpleVision_nDisplays 0
00090 #endif
00091 #define phSimpleVision_nNetDisplays 2
00092 #define phSimpleVision_totalDisplays (phSimpleVision_nDisplays + phSimpleVision_nNetDisplays)
00093 
00094 #define phSimpleVision_nLocalFilters 15
00095 
00096 #define phIsOdd(x) (x & 0x01)
00097 
00098 /* ------------------------------------------------------------------------- */
00099 phSimpleVision::phSimpleVision( )
00100 {
00101     phFUNCTION("phSimpleVision::phSimpleVision")
00102     int                 i           = 0;
00103     int                 j           = 0;
00104     uint32_t            basePort    = 44444;
00105     NetDisplay          *n          = NULL;
00106     phLiveObject        *l          = NULL;
00107     histogram_Filter    *h          = NULL;
00108     blob_Filter         *b          = NULL;
00109     char title[255];
00110 
00111     this->m_emptyIndex      = 
00112     this->m_motionIndex     = 
00113     this->m_cannyIndex      =
00114     this->m_sobelIndex      = 
00115     this->m_gaussianIndex   = 
00116     this->m_meanIndex       = 
00117     this->m_medianIndex     = 
00118     this->m_inverseIndex    = 
00119     this->m_thresholdIndex  =
00120     this->m_addIndex        = 
00121     this->m_subtractIndex   = 
00122     this->m_blobIndex       = 
00123     this->m_histIndex       = 
00124     this->m_RGBIndex        = 
00125     this->m_HSVIndex        = -1;
00126     this->m_state = phSimpleVision_Stopped;
00127     
00128     this->m_system          = NULL;
00129     this->m_capture         = NULL;
00130     this->m_pipeline        = NULL;
00131     this->m_displays        = NULL;
00132     this->m_displaysEnabled = NULL;
00133     this->m_filters         = NULL;
00134 
00135     this->m_trainedColor    = phColorRGBA32_new(0,0,0,0);
00136     this->m_trainedThreshold= phColorRGBA32_new(10,64,64,0);
00137 
00138     this->m_histData            = new phHistogramData();
00139     phPRINT_PTR(this->m_histData,"new phHistogramData",
00140               "allocating phHistogramData failed.");
00141     
00142     this->m_blobOutcolor        = phColorRGB24_new(0,255,255);
00143     this->m_blobMinsize         = 0;
00144     this->m_blobMinPercentage   = 0.001;
00145     phMemset(&this->m_maxBlob,0,sizeof(phblob));
00146     this->m_blobData            = new phBlobData();
00147     phPRINT_PTR(this->m_blobData,"new phBlobData",
00148               "allocating phBlobData failed.");
00149     
00150     /* Allocate the system object */
00151     this->m_system = new phSystem();
00152     phCHECK_PTR(this->m_system,"new phSystem",
00153               "failed to allocate system object");
00154     
00155     /* Allocate the capture device */
00156 #ifdef PH_HAVE_V4L
00157     this->m_capture = new V4LCapture();
00158     phCHECK_PTR(this->m_capture,"new V4LCapture",
00159               "failed to allocate capture object");
00160 #else
00161 #ifdef PH_HAVE_VFW
00162     this->m_capture = new VFWSource();
00163     phCHECK_PTR(this->m_capture,"new VFWSource",
00164               "failed to allocate capture object");
00165 #endif
00166 #endif
00167 
00168     rc = this->m_capture->setSize(320,240);
00169     phPRINT_RC(rc,NULL,"this->m_capture->setSize");
00170 
00171     rc = this->m_system->addCapture(this->m_capture);
00172     phCHECK_RC(rc,NULL,"m_system->addCapture failed");
00173     
00174     /* Allocate the pipeline */
00175     this->m_pipeline = new phPipeline();
00176     phCHECK_PTR(this->m_pipeline,"new phPipeline",
00177               "failed to allocate phPipeline object");
00178 
00179     rc = this->m_system->addPipeline(this->m_pipeline);
00180     phCHECK_RC(rc,NULL,"m_system->addPipeline");
00181 
00182     /* Connect the pipeline to the capture card */
00183     l = this->m_capture->getLiveSourceOutput();
00184     rc = this->m_pipeline->setLiveSourceInput(l);
00185     phPRINT_RC(rc,NULL,"setLiveSourceInput");
00186     
00187     /* Allocate the arrays to track whether the user disabled the display */
00188     this->m_displaysEnabled = (int *)phCalloc(phSimpleVision_totalDisplays,
00189                                             sizeof(int));
00190     phCHECK_PTR(this->m_displaysEnabled,"phCalloc",
00191               "phCalloc failed to alloc int array");
00192     
00193     /* Allocate the display array */
00194     this->m_displays = new phDisplayInterface *[phSimpleVision_totalDisplays];
00195     phCHECK_PTR(this->m_displays,"new phDisplayInterface *[]",
00196               "new failed to allocate phDisplayInterface array");
00197     
00198     /* Allocate the displays to be used locally */
00199     for (i = 0; i < phSimpleVision_nDisplays; i++, j++ )
00200     {
00201         char *displaytype = NULL;
00202         
00203         sprintf(title,"phSimpleVision [%d]",j);
00204 #ifdef PH_HAVE_GDI
00205         displaytype = "GDI";
00206         this->m_displays[j] = new GDIDisplay(320,240,title);
00207 #else
00208 #ifdef PH_HAVE_X11
00209         displaytype = "X11";
00210         this->m_displays[j] = new X11Display(320,240,title);
00211 #endif /* PH_HAVE_X11 */
00212 #endif /* PH_HAVE_GDI */
00213         phCHECK_PTR(this->m_displays[j],"new display",
00214                   "allocating %sDisplay failed",
00215                   displaytype );
00216 
00217         this->m_displays[j]->move(50 + (340 * i),50);
00218         
00219         rc = this->m_system->addDisplay(this->m_displays[j]);
00220         phCHECK_RC(rc,NULL,"m_system->addDisplay(%s[%d]) failed",displaytype,j);
00221 
00222         this->m_displaysEnabled[j] = 1;
00223     }
00224  
00225     /* Allocate the network display class */
00226     for (i = 0; i < phSimpleVision_nNetDisplays; i++,j++ )
00227     {
00228         sprintf(title,"phSimpleVision [Net][%d]",j);
00229 
00230         n = new NetDisplay(320,240,title);
00231         phCHECK_PTR(n,"new NetDisplay","allocating NetDisplay failed");
00232 
00233         rc = n->setFormat(phImageRGB24 | phImageJPEG);
00234         phPRINT_RC(rc,NULL,"setFormat(phImageRGB24 | phImageJPEG)");
00235 
00236         rc = n->setPort(basePort + i);
00237         phPRINT_RC(rc,NULL,"n[%d]->setPort(%d)",i,basePort + i);
00238 
00239         this->m_displays[j] = n;
00240         
00241         /*
00242         rc = this->m_system->addDisplay(this->m_displays[j]);
00243         phCHECK_RC(rc,NULL,"m_system.addDisplay(Net[%d]) failed",j);
00244         */
00245         
00246         this->m_displaysEnabled[j] = 0;
00247     }
00248 
00249     /* Connect the display to either the capture or pipeline */
00250     for (i = 0; i < phSimpleVision_totalDisplays; i++ )
00251     {
00252         if (!phIsOdd(i)) /* Capture */
00253             l = this->m_capture->getLiveSourceOutput();
00254         else /* Pipeline */
00255             l = this->m_pipeline->getLiveSourceOutput();
00256         
00257         rc = this->m_displays[i]->setLiveSourceInput(l);
00258         phPRINT_RC(rc,NULL,"setLiveSourceInput");
00259     }
00260  
00261     /* Allocate the phFilter array */
00262     this->m_filters = new phFilter *[phSimpleVision_nLocalFilters];
00263     phCHECK_PTR(this->m_filters,"new phFilter *",
00264               "failed to allocate phFilter array");
00265 
00266     j = 0;
00267 
00268     /* empty */
00269     this->m_filters[j] = new empty_Filter();
00270     phCHECK_PTR(this->m_filters[j],"new empty_Filter()",
00271               "failed to allocate empty_Filter");
00272     this->m_emptyIndex = j;
00273     j++;
00274 
00275     /* motion */
00276     this->m_filters[j] = new motion_Filter();
00277     phCHECK_PTR(this->m_filters[j],"new motion_Filter()",
00278               "failed to allocate motion_Filter");
00279     this->m_motionIndex = j;
00280     j++;
00281 
00282     /* canny */
00283 #if PH_HAVE_OPENCV
00284     this->m_filters[j] = new cv_canny_Filter();
00285 #else
00286     this->m_filters[j] = new canny_Filter();
00287 #endif
00288     phCHECK_PTR(this->m_filters[j],"new canny_Filter()",
00289               "failed to allocate canny_Filter");
00290     this->m_cannyIndex = j;
00291     j++;
00292 
00293     /* sobel */
00294     this->m_filters[j] = new sobel_Filter();
00295     phCHECK_PTR(this->m_filters[j],"new sobel_Filter()",
00296               "failed to allocate sobel_Filter");
00297     this->m_sobelIndex = j;
00298     j++;
00299 
00300     /* gaussian */
00301     this->m_filters[j] = new gaussian3x3_Filter();
00302     phCHECK_PTR(this->m_filters[j],"new gaussian3x3_Filter()",
00303               "failed to allocate gaussian3x3_Filter");
00304     this->m_gaussianIndex = j;
00305     j++;
00306 
00307     /* mean */
00308     this->m_filters[j] = new meanNxN_Filter(7);
00309     phCHECK_PTR(this->m_filters[j],"new meanNxN_Filter(7)",
00310               "failed to allocate meanNxN_Filter");
00311     this->m_meanIndex = j;
00312     j++;
00313 
00314     /* median */
00315     this->m_filters[j] = new medianNxN_Filter(5);
00316     phCHECK_PTR(this->m_filters[j],"new medianNxN_Filter(5)",
00317               "failed to allocate medianNxN_Filter");
00318     this->m_medianIndex = j;
00319     j++;
00320 
00321     /* inverse */
00322     this->m_filters[j] = new inverse_Filter();
00323     phCHECK_PTR(this->m_filters[j],"new inverse_Filter()",
00324               "failed to allocate inverse_Filter");
00325     this->m_inverseIndex = j;
00326     j++;
00327 
00328     /* threshold */
00329     this->m_filters[j] = new threshold_Filter();
00330     phCHECK_PTR(this->m_filters[j],"new threshold_Filter()",
00331               "failed to allocate threshold_Filter");
00332     this->m_thresholdIndex = j;
00333     j++;
00334 
00335     /* add */
00336     this->m_filters[j] = new add_Filter(3,1);
00337     phCHECK_PTR(this->m_filters[j],"new add_Filter(3,1)",
00338               "failed to allocate add_Filter");
00339     this->m_addIndex = j;
00340     j++;
00341 
00342     /* subtract */
00343     this->m_filters[j] = new subtract_Filter(2,1);
00344     phCHECK_PTR(this->m_filters[j],"new subtract_Filter(2,1)",
00345               "failed to allocate subtract_Filter");
00346     this->m_subtractIndex = j;
00347     j++;
00348 
00349     /* blob */
00350     this->m_filters[j] = b = new blob_Filter();
00351     phCHECK_PTR(this->m_filters[j],"new blob_Filter()",
00352               "failed to allocate blob_Filter");
00353     this->m_blobIndex = j;
00354     
00355     rc = this->initBlobSettings();
00356     phPRINT_RC(rc,NULL,"initBlobSettings()");
00357 
00358         /* Connect the local m_blobData to the one in the filter */
00359     rc = this->m_blobData->connect(b->getLiveBlobOutput());
00360     phPRINT_RC(rc,NULL,"this->m_blobData->connect");
00361     
00362     j++;
00363 
00364     /* histogram */
00365     this->m_filters[j] = h = new histogram_Filter();
00366     phCHECK_PTR(this->m_filters[j],"new histogram_Filter()",
00367               "failed to allocate histogram_Filter");
00368     this->m_histIndex = j;
00369     
00370     rc = this->initHistogramSettings();
00371     phPRINT_RC(rc,NULL,"initHistogramSettings()");
00372 
00373         /* Connect the local m_histdata to the one in the filter */
00374     rc = this->m_histData->connect(h->getLiveHistogramOutput());
00375     phPRINT_RC(rc,NULL,"this->m_histData->connect");
00376     
00377     j++;
00378 
00379     /* convert - RGB24 */
00380     this->m_filters[j] = new convert_Filter(phImageRGB24);
00381     phCHECK_PTR(this->m_filters[j],"new convert_Filter(RGB)",
00382               "failed to allocate convert_Filter");
00383     this->m_RGBIndex = j;
00384     j++;
00385     
00386     /* convert - RGB24 */
00387     this->m_filters[j] = new convert_Filter(phImageHSV24);
00388     phCHECK_PTR(this->m_filters[j],"new convert_Filter(HSV)",
00389               "failed to allocate convert_Filter");
00390     this->m_HSVIndex = j;
00391     j++;
00392 
00393     for (i = j; i <  phSimpleVision_nLocalFilters; i++)
00394     {
00395         this->m_filters[i] = NULL;
00396     }
00397     
00398     this->empty();
00399 error:
00400     return;
00401 }
00402 /* ------------------------------------------------------------------------- */
00403 phSimpleVision::~phSimpleVision( )
00404 {
00405     phFUNCTION("phSimpleVision::~phSimpleVision")
00406     int locked = 0;
00407     int i = 0;
00408 
00409     phTHIS_LOCK(locked);
00410  
00411     if ((this->m_state == phSimpleVision_Started) ||
00412         (this->m_state == phSimpleVision_Paused))
00413     {
00414         rc = this->stop();
00415         phPRINT_RC(rc,NULL,"this->stop()");
00416     }
00417     
00418     this->m_RGBIndex = -1;
00419     this->m_HSVIndex = -1;
00420     this->m_histIndex= -1;
00421     this->m_blobIndex= -1;
00422 
00423     for (i = 0; i < phSimpleVision_nLocalFilters; i++ )
00424     {
00425         phDelete(this->m_filters[i]);
00426     }
00427     
00428     phDeleteArray(this->m_filters);
00429    
00430     for (i = 0; i < phSimpleVision_totalDisplays; i++)
00431     {
00432         phDelete(this->m_displays[i]);
00433     }
00434 
00435     phDeleteArray   (this->m_displays);
00436     phFree          (this->m_displaysEnabled);
00437     phDelete        (this->m_pipeline);
00438     phDelete        (this->m_capture);
00439     phDelete        (this->m_system);
00440     phDelete        (this->m_histData);
00441     phDelete        (this->m_blobData);
00442     
00443 error:
00444     phTHIS_ERROR_UNLOCK(locked);
00445 }
00446 
00447 /* ------------------------------------------------------------------------- */
00448 int phSimpleVision::setPath( const char *path )
00449 {
00450     return this->m_capture->setPath(path);
00451 }
00452 
00453 /* ------------------------------------------------------------------------- */
00454 const char *phSimpleVision::getPath( )
00455 {
00456     return this->m_capture->getPath();
00457 }
00458 
00459 /* ------------------------------------------------------------------------- */
00460 int phSimpleVision::setChannel( uint32_t channel )
00461 {
00462     return this->m_capture->setChannel( channel );
00463 }
00464 
00465 /* ------------------------------------------------------------------------- */
00466 uint32_t phSimpleVision::getChannel( )
00467 {
00468     return this->m_capture->getChannel();
00469 }
00470 
00471 /* ------------------------------------------------------------------------- */
00472 int phSimpleVision::enableNetDisplay( int which )
00473 {
00474     phFUNCTION("phSimpleVision::enableNetDisplay")
00475     int locked = 0;
00476 
00477     phTHIS_LOCK(locked);
00478     
00479     if ((which == phSimpleVisionOutput_Capture) ||
00480         (which == phSimpleVisionOutput_Pipeline))
00481     {
00482         int index = phSimpleVision_nDisplays + which;
00483         
00484         if (!this->m_displaysEnabled[index])
00485         {
00486             phDisplayInterface *d = this->m_displays[index];
00487             
00488             rc = this->m_system->addDisplay(d);
00489             phCHECK_RC(rc,NULL,"this->m_system->addDisplay failed"); 
00490             
00491             this->m_displaysEnabled[index] = 1;
00492         }
00493     }
00494     
00495     phTHIS_UNLOCK(locked);
00496     
00497     return phSUCCESS;
00498 error:
00499     phTHIS_ERROR_UNLOCK(locked);
00500 
00501     return phFAIL;
00502 }
00503 
00504 /* ------------------------------------------------------------------------- */
00505 int phSimpleVision::disableNetDisplay( int which )
00506 {
00507     phFUNCTION("phSimpleVision::disableNetDisplay")
00508     int locked = 0;
00509 
00510     phTHIS_LOCK(locked);
00511     
00512     if ((which == phSimpleVisionOutput_Capture) ||
00513         (which == phSimpleVisionOutput_Pipeline))
00514     {
00515         int index = phSimpleVision_nDisplays + which;
00516        
00517         if (this->m_displaysEnabled[index])
00518         {
00519             phDisplayInterface *d = this->m_displays[index];
00520 
00521             rc = this->m_system->removeDisplay(d);
00522             phCHECK_RC(rc,NULL,"this->m_system->removeDisplay failed"); 
00523             
00524             this->m_displaysEnabled[index] = 0;
00525         }
00526     }
00527     
00528     phTHIS_UNLOCK(locked);
00529     
00530     return phSUCCESS;
00531 error:
00532     phTHIS_ERROR_UNLOCK(locked);
00533 
00534     return phFAIL;
00535 }
00536 
00537 /* ------------------------------------------------------------------------- */
00538 int phSimpleVision::isNetEnabled( int which )
00539 {
00540     if ((which == phSimpleVisionOutput_Capture) ||
00541         (which == phSimpleVisionOutput_Pipeline))
00542     {
00543         int index = phSimpleVision_nDisplays + which;
00544         return this->m_displaysEnabled[index];
00545     }
00546  
00547     return 0;
00548 }
00549 
00550 /* ------------------------------------------------------------------------- */
00551 int phSimpleVision::setPort( uint32_t port )
00552 {
00553     phFUNCTION("phSimpleVision::setPort")
00554     int locked = 0;
00555     int i = 0;
00556    int index = phSimpleVision_nDisplays;
00557 
00558     phTHIS_LOCK(locked);
00559     
00560     for (i = 0; i < phSimpleVision_nNetDisplays; i++ )
00561     {
00562         NetDisplay *net = (NetDisplay *)this->m_displays[index + i];
00563         
00564         rc = net->setPort(port + i);
00565         phPRINT_RC(rc,NULL,"net->setPort(%d)",port + i);
00566     }
00567     
00568     phTHIS_UNLOCK(locked);
00569     
00570     return phSUCCESS;
00571 error:
00572     phTHIS_ERROR_UNLOCK(locked);
00573 
00574     return phFAIL;
00575 }
00576 
00577 /* ------------------------------------------------------------------------- */
00578 int phSimpleVision::enableDisplay( int which )
00579 {
00580     phFUNCTION("phSimpleVision::enableDisplay")
00581     int locked = 0;
00582 
00583     phTHIS_LOCK(locked);
00584     
00585     if ((which == phSimpleVisionOutput_Capture) ||
00586         (which == phSimpleVisionOutput_Pipeline))
00587     {
00588         if (!this->m_displaysEnabled[which])
00589         {
00590             phDisplayInterface *d = this->m_displays[which];
00591             
00592             rc = this->m_system->addDisplay(d);
00593             phCHECK_RC(rc,NULL,"this->m_system->addDisplay failed"); 
00594             
00595             this->m_displaysEnabled[which] = 1;
00596         }
00597     }
00598     
00599     phTHIS_UNLOCK(locked);
00600     
00601     return phSUCCESS;
00602 error:
00603     phTHIS_ERROR_UNLOCK(locked);
00604 
00605     return phFAIL;
00606 }
00607 
00608 /* ------------------------------------------------------------------------- */
00609 int phSimpleVision::disableDisplay( int which )
00610 {
00611     phFUNCTION("phSimpleVision::disableDisplay")
00612     int locked = 0;
00613 
00614     phTHIS_LOCK(locked);
00615     
00616     if ((which == phSimpleVisionOutput_Capture) ||
00617         (which == phSimpleVisionOutput_Pipeline))
00618     {
00619         if (this->m_displaysEnabled[which])
00620         {
00621             phDisplayInterface *d = this->m_displays[which];
00622             
00623             rc = this->m_system->removeDisplay(d);
00624             phCHECK_RC(rc,NULL,"this->m_system->removeDisplay failed"); 
00625             
00626             this->m_displaysEnabled[which] = 0;
00627         }
00628     }
00629     
00630     phTHIS_UNLOCK(locked);
00631     
00632     return phSUCCESS;
00633 error:
00634     phTHIS_ERROR_UNLOCK(locked);
00635 
00636     return phFAIL;
00637 }
00638 
00639 /* ------------------------------------------------------------------------- */
00640 int phSimpleVision::isDisplayEnabled( int which )
00641 {
00642     if ((which == phSimpleVisionOutput_Capture) ||
00643         (which == phSimpleVisionOutput_Pipeline))
00644     {
00645         return this->m_displaysEnabled[which];
00646     }
00647  
00648     return 0;
00649 }
00650 
00651 /* ------------------------------------------------------------------------- */
00652 int phSimpleVision::start( )
00653 {
00654     phFUNCTION("phSimpleVision::start")
00655     int locked = 0;
00656 
00657     phTHIS_LOCK(locked);
00658     
00659     rc = this->m_system->startup();
00660     phCHECK_RC(rc,NULL,"this->m_system->startup()");
00661 
00662     this->m_state = phSimpleVision_Started;
00663 
00664     phTHIS_UNLOCK(locked);
00665     
00666     return phSUCCESS;
00667 error:
00668     phTHIS_ERROR_UNLOCK(locked);
00669     
00670     return phFAIL;
00671 }
00672 
00673 /* ------------------------------------------------------------------------- */
00674 int phSimpleVision::pause( )
00675 {
00676     phFUNCTION("phSimpleVision::pause")
00677     int locked = 0;
00678 
00679     phTHIS_LOCK(locked);
00680     
00681     rc = this->m_system->halt();
00682     phCHECK_RC(rc,NULL,"this->m_system->halt()");
00683 
00684     this->m_state = phSimpleVision_Paused;
00685 
00686     phTHIS_UNLOCK(locked);
00687     
00688     return phSUCCESS;
00689 error:
00690     phTHIS_ERROR_UNLOCK(locked);
00691     
00692     return phFAIL;
00693 }
00694 
00695 /* ------------------------------------------------------------------------- */
00696 int phSimpleVision::stop( )
00697 {
00698     phFUNCTION("phSimpleVision::stop")
00699     int locked = 0;
00700 
00701     phTHIS_LOCK(locked);
00702     
00703     rc = this->m_system->shutdown();
00704     phCHECK_RC(rc,NULL,"this->m_system->shutdown()");
00705 
00706     this->m_state = phSimpleVision_Stopped;
00707 
00708     phTHIS_UNLOCK(locked);
00709     
00710     return phSUCCESS;
00711 error:
00712     phTHIS_ERROR_UNLOCK(locked);
00713     
00714     return phFAIL;
00715 
00716 }
00717 
00718 /* ------------------------------------------------------------------------- */
00719 int phSimpleVision::isStarted()
00720 { 
00721     return (this->m_state == phSimpleVision_Started) ? 1 : 0; 
00722 }
00723 /* ------------------------------------------------------------------------- */
00724 int phSimpleVision::isPaused( )
00725 { 
00726     return (this->m_state == phSimpleVision_Paused) ? 1 : 0; 
00727 }
00728 /* ------------------------------------------------------------------------- */
00729 int phSimpleVision::isStopped() 
00730 { 
00731     return (this->m_state == phSimpleVision_Stopped) ? 1 : 0; 
00732 }
00733 /* ------------------------------------------------------------------------- */
00734 /* returns 0 if no error, returns 1 if pipeline stopped, -1 if failure */
00735 int phSimpleVision::stopAndEmptyPipeline()
00736 {
00737     phFUNCTION("phSimpleVision::stopAndEmptyPipeline")
00738     int locked = 0;
00739     int retrc = phSUCCESS;
00740 
00741     phTHIS_LOCK(locked);
00742 
00743     /* Only stop the pipeline if it's running; we can also prevent starting the 
00744      * pipeline if it hasn't been started by doing this check */
00745     if (this->m_pipeline->isPipelineActive())
00746     {
00747         rc = this->m_pipeline->stopPipeline();
00748         phPRINT_RC(rc,NULL,"this->m_pipeline->stopPipeline()");
00749 
00750         retrc = 1;
00751     }
00752     
00753     rc = this->m_pipeline->empty();
00754     phPRINT_RC(rc,NULL,"this->m_pipeline->empty()");
00755 
00756     phTHIS_UNLOCK(locked);
00757 
00758     return retrc;
00759 error:
00760     phTHIS_ERROR_UNLOCK(locked);
00761     
00762     return phFAIL;
00763 }
00764 
00765 /* ------------------------------------------------------------------------- */
00766 /* count is indexed starting at 1 */
00767 int phSimpleVision::switchToFilters( int *indexes, int count )
00768 {
00769     phFUNCTION("phSimpleVision::switchToFilters")
00770     int locked              = 0;
00771     int reenable_pipeline   = 0;
00772     int index               = 0;
00773     int i                   = 0;
00774 
00775     if ((indexes == NULL) || (count <= 0)) return phFAIL;
00776     
00777     phTHIS_LOCK(locked);
00778 
00779     reenable_pipeline = rc = this->stopAndEmptyPipeline();
00780     phCHECK_RC(rc,NULL,"this->stopAndEmptyPipeline()");
00781 
00782     for (i = 0; i < count; i++ )
00783     {
00784         index = indexes[i];
00785         if ((index >= 0) && (index < phSimpleVision_nLocalFilters))
00786         {    
00787             rc = this->m_pipeline->add(this->m_filters[index]);
00788             phPRINT_RC(rc,NULL,"this->m_pipeline->add([%d]%p:%d)",
00789                     i,
00790                     this->m_filters[index],
00791                     index);
00792         }
00793     }
00794 
00795     /* Reenable the pipeline because we stopped it */
00796     if (reenable_pipeline == 1)
00797     {
00798         rc = this->m_pipeline->startPipeline();
00799         phPRINT_RC(rc,NULL,"this->m_pipeline->startPipeline()");
00800     }
00801 
00802     phTHIS_UNLOCK(locked);
00803     
00804     return phSUCCESS;
00805 error:
00806     phTHIS_ERROR_UNLOCK(locked);
00807     
00808     return phFAIL;
00809 }
00810 
00811 /* ------------------------------------------------------------------------- */
00812 int phSimpleVision::switchToFilter( int index )
00813 {
00814     phFUNCTION("phSimpleVision::switchToFilter")
00815     phFilter    *filter         = NULL;
00816     int32_t     alreadyRunning  = 0;
00817     int32_t     locked          = 0;
00818 
00819     phTHIS_LOCK(locked);
00820 
00821     filter = this->m_filters[index];
00822     alreadyRunning = this->m_pipeline->query(filter);
00823     
00824     if (!alreadyRunning)
00825     {
00826         rc =  this->switchToFilters( &index, 1 );
00827         phCHECK_RC(rc,NULL,"this->switchToFilters");
00828     }
00829     
00830     phTHIS_UNLOCK(locked);
00831     
00832     return phSUCCESS;
00833 error:
00834     phTHIS_ERROR_UNLOCK(locked);
00835 
00836     return phFAIL;
00837 }
00838 
00839 /* ------------------------------------------------------------------------- */
00840 int phSimpleVision::empty( )
00841 {
00842     return this->switchToFilter(this->m_emptyIndex);
00843 }
00844 /* ------------------------------------------------------------------------- */
00845 int phSimpleVision::motion( )
00846 {
00847     return this->switchToFilter(this->m_motionIndex);
00848 }
00849 /* ------------------------------------------------------------------------- */
00850 int phSimpleVision::canny( )
00851 {
00852     return this->switchToFilter(this->m_cannyIndex);
00853 }
00854 /* ------------------------------------------------------------------------- */
00855 int phSimpleVision::sobel( )
00856 {
00857     return this->switchToFilter(this->m_sobelIndex);
00858 }
00859 /* ------------------------------------------------------------------------- */
00860 int phSimpleVision::gaussian( )
00861 {
00862     return this->switchToFilter(this->m_gaussianIndex);
00863 }
00864 /* ------------------------------------------------------------------------- */
00865 int phSimpleVision::mean( )
00866 {
00867     return this->switchToFilter(this->m_meanIndex);
00868 }
00869 /* ------------------------------------------------------------------------- */
00870 int phSimpleVision::median( )
00871 {
00872     return this->switchToFilter(this->m_medianIndex);
00873 }
00874 /* ------------------------------------------------------------------------- */
00875 int phSimpleVision::inverse( )
00876 {
00877     return this->switchToFilter(this->m_inverseIndex);
00878 }
00879 /* ------------------------------------------------------------------------- */
00880 int phSimpleVision::threshold( )
00881 {
00882     return this->switchToFilter(this->m_thresholdIndex);
00883 }
00884 /* ------------------------------------------------------------------------- */
00885 int phSimpleVision::add( uint32_t nFrames, uint32_t frame_step )
00886 {
00887     ((add_Filter *)this->m_filters[this->m_addIndex])->set(nFrames,frame_step);
00888     return this->switchToFilter(this->m_addIndex);
00889 }
00890 /* ------------------------------------------------------------------------- */
00891 int phSimpleVision::subtract( )
00892 {
00893     return this->switchToFilter(this->m_subtractIndex);
00894 }
00895 
00896 /* ------------------------------------------------------------------------- */
00897 int phSimpleVision::initHistogramSettings( )
00898 {
00899     phFUNCTION("phSimpleVision::initHistogramSettings")
00900 
00901     histogram_Filter *histogram = 
00902         (histogram_Filter *)this->m_filters[this->m_histIndex];
00903     
00904     uint32_t        bins        = 64;
00905     uint32_t        draw_rect   = 1;
00906     phColor         outcolor    = phColorRGB24_new(0,255,255);
00907     const uint32_t  box_size    = 8;
00908     int locked = 0;
00909 
00910     phTHIS_LOOSE_LOCK(locked);
00911     
00912     rc = histogram->setBoxSize( box_size );
00913     phPRINT_RC(rc,NULL,"histogram->setBinCount");
00914     
00915     rc = histogram->setBinCount( bins );
00916     phPRINT_RC(rc,NULL,"histogram->setBinCount");
00917     
00918     rc = histogram->setDrawRect( draw_rect );
00919     phPRINT_RC(rc,NULL,"histogram->set");
00920     
00921     rc = histogram->setOutcolor( outcolor );
00922     phPRINT_RC(rc,NULL,"histogram->setOutcolor");
00923     
00924     phTHIS_LOOSE_UNLOCK(locked);
00925 
00926     return phSUCCESS;
00927 }
00928      
00929 /* ------------------------------------------------------------------------- */
00930 int phSimpleVision::private_training( int which )
00931 {
00932     phFUNCTION("phSimpleVision::private_training")
00933     int indexes[] = {this->m_gaussianIndex,
00934                      which,
00935                      this->m_histIndex };
00936     int n = sizeof(indexes) / sizeof(int);
00937     
00938     int32_t             locked          = 0;
00939 
00940     histogram_Filter    *hist           = NULL;
00941     convert_Filter      *convert        = NULL;
00942     int                 runningHist     = 0;
00943     int                 runningConvert  = 0;
00944     
00945     phTHIS_LOCK(locked);
00946     
00947     /* Make sure the histogram filter and the repective colorspace
00948      * conversion filter is running in the pipeline */
00949     hist = (histogram_Filter *)this->m_filters[this->m_histIndex];
00950     convert = (convert_Filter *)this->m_filters[which];
00951     
00952     /* Find out if the respective filters are in the pipeline */
00953     runningHist = this->m_pipeline->query(hist);
00954     runningConvert = this->m_pipeline->query(convert);
00955     
00956     /* make sure the conversion and histogram filters are running */
00957     if ((runningHist == 0) || (runningConvert == 0))
00958     {
00959         rc = this->switchToFilters(indexes,n);
00960         phCHECK_RC(rc,NULL,"this->switchToFilters");
00961     }
00962     
00963     phTHIS_UNLOCK(locked);
00964     
00965     return phSUCCESS;
00966 error:
00967     phTHIS_ERROR_UNLOCK(locked);
00968     
00969     return phFAIL;
00970 }
00971    
00972 /* ------------------------------------------------------------------------- */
00973 int phSimpleVision::trainingRGB( )
00974 {
00975     return this->private_training(this->m_RGBIndex);
00976 }
00977 
00978 /* ------------------------------------------------------------------------- */
00979 int phSimpleVision::trainingHSV( )
00980 {
00981     return this->private_training(this->m_HSVIndex);
00982 }
00983 
00984 /* ------------------------------------------------------------------------- */
00985 int phSimpleVision::train( int colorspace, int add_or_set )
00986 {
00987     phFUNCTION("phSimpleVision::train")
00988     int                 retrc           = phSUCCESS;
00989     int32_t             i               = 0;
00990     int32_t             j               = 0;
00991     blob_Filter         *blob           = NULL;
00992     histogram_Filter    *hist           = NULL;
00993     int32_t             locked          = 0;
00994     
00995     const int32_t       samples         = 5;
00996     int32_t             sample_count    = 0;
00997     
00998     phColor             color_samples[samples];
00999     phColor             thresh_samples[samples];
01000     phColor             trained_color;
01001     phColor             trained_threshold;
01002     int32_t             channel_total   = 0;
01003     int32_t             thresh_total    = 0;
01004 
01005     phTHIS_LOCK(locked);
01006    
01007     hist = (histogram_Filter *)this->m_filters[this->m_histIndex];
01008     blob = (blob_Filter *)this->m_filters[this->m_blobIndex];
01009 
01010     /* If the colorspace is set to be RGB */
01011     if (colorspace == phSimpleVision_RGB)
01012     {
01013         rc = this->trainingRGB();
01014         phCHECK_RC(rc,NULL,"this->trainingRGB()");
01015     }
01016     /* Otherwise, default to HSV */ 
01017     else
01018     {
01019         rc = this->trainingHSV();
01020         phCHECK_RC(rc,NULL,"this->trainingHSV()");
01021     }
01022     
01023     if ((this->m_state == phSimpleVision_Stopped) ||
01024         (this->m_state == phSimpleVision_Paused) ||
01025         (!this->m_pipeline->isPipelineActive()))
01026     {
01027         rc = this->start();
01028         phCHECK_RC(rc,NULL,"this->start");
01029     }
01030  
01031     /* Lock the pipeline for the length of this method so it won't be
01032      * altered by external forces while we train */
01033     
01034     /* Gather a number of samples to average the trained color and
01035      * possible get better tracking results. */
01036     for (i = 0; i < samples; i++ )
01037     {
01038         /* pass 1/phLiveObjectNOWAIT for non-blocking */
01039         rc = this->m_histData->update();
01040         phCHECK_RC(rc,NULL,"this->m_histData->update()");
01041    
01042 #if 1
01043         thresh_samples[sample_count] = this->m_histData->getThreshold();
01044         color_samples[sample_count++] = this->m_histData->getColor();
01045 #else
01046         thresh_samples[sample_count] = this->m_histData->getMaxBinThreshold();
01047         color_samples[sample_count++] = this->m_histData->getMaxBinColor();
01048 #endif
01049         /* this->m_histData->print_data(); */
01050     }
01051    
01052     if (sample_count == 0)
01053     {
01054         phCHECK_RC(-1,NULL,"Failed to gather any samples.");
01055     }
01056 
01057     trained_color.type = trained_threshold.type = color_samples[0].type;
01058     
01059     /* Average the samples */
01060     for (j = 0; (sample_count > 0) && (j < phCOLOR_MAXBYTES); j++ )
01061     {
01062         /* Reinitialize the channel value total */
01063         channel_total = 0;
01064         thresh_total = 0;
01065         
01066         /* calculate the total for the channel */
01067         for (i = 0; i < sample_count; i++)
01068         {
01069             channel_total += color_samples[i].array.v[j];
01070             thresh_total += thresh_samples[i].array.v[j];
01071         }
01072        
01073         /* Calculate the average for the sample */
01074         trained_color.array.v[j] = 0;
01075         trained_threshold.array.v[j] = 0;
01076         if (channel_total > 0)
01077         {
01078             trained_color.array.v[j] = channel_total / sample_count;
01079         }
01080         if (thresh_total > 0)
01081         {
01082             trained_threshold.array.v[j] = (thresh_total / sample_count);
01083         }
01084     }
01085 
01086 #if 0
01087     trained_threshold= phColorArray32_new(10,64,64,0);
01088 #endif
01089     this->m_trainedColor        = trained_color;
01090     this->m_trainedThreshold    = trained_threshold;
01091     
01092     if (add_or_set == phSimpleVision_ADD)
01093     {
01094         rc = blob->addColor(this->m_trainedColor,this->m_trainedThreshold);
01095         phPRINT_RC(rc,NULL,"blob->addColor");        
01096     }
01097     else if (add_or_set == phSimpleVision_SET)
01098     {
01099         rc = blob->setColor(this->m_trainedColor,this->m_trainedThreshold);
01100         phPRINT_RC(rc,NULL,"blob->setColor");        
01101     }
01102     
01103     /*
01104     fprintf(stderr,"%s:color    [ %u, %u, %u, %u ]\n",
01105               function,
01106               this->m_trainedColor.array.v[0],
01107               this->m_trainedColor.array.v[1],
01108               this->m_trainedColor.array.v[2],
01109               this->m_trainedColor.array.v[3] );
01110     fprintf(stderr,"%s:threshold[ %u, %u, %u, %u ]\n",
01111               function,
01112               this->m_trainedThreshold.array.v[0],
01113               this->m_trainedThreshold.array.v[1],
01114               this->m_trainedThreshold.array.v[2],
01115               this->m_trainedThreshold.array.v[3] );
01116     fflush(stderr);
01117     */
01118 
01119     phTHIS_UNLOCK(locked);
01120     
01121     return retrc;
01122 error:
01123     phTHIS_ERROR_UNLOCK(locked);
01124     
01125     return phFAIL;
01126 }
01127 /* ------------------------------------------------------------------------- */
01128 int phSimpleVision::trainAdd( int colorspace )
01129 {
01130     return this->train(colorspace,phSimpleVision_ADD);
01131 }
01132 
01133 /* ------------------------------------------------------------------------- */
01134 int phSimpleVision::trainSet( int colorspace )
01135 {
01136     return this->train(colorspace,phSimpleVision_SET);
01137 }
01138 
01139 /* ------------------------------------------------------------------------- */
01140 phColor phSimpleVision::getColor( )
01141 {
01142     return this->m_trainedColor;
01143 }
01144 
01145 /* ------------------------------------------------------------------------- */
01146 phColor phSimpleVision::getThreshold( )
01147 {
01148     return this->m_trainedThreshold;
01149 }
01150 
01151 /* ------------------------------------------------------------------------- */
01152 int phSimpleVision::setColor( phColor color )
01153 {
01154     phFUNCTION("phSimpleVision::setColor")
01155     int locked = 0;
01156 
01157     phTHIS_LOCK(locked);
01158         
01159     this->m_trainedColor = color;
01160         
01161     phTHIS_UNLOCK(locked);
01162     
01163     return phSUCCESS;
01164 error:
01165     phTHIS_ERROR_UNLOCK(locked);
01166 
01167     return phFAIL;
01168 }
01169 
01170 /* ------------------------------------------------------------------------- */
01171 int phSimpleVision::setThreshold( phColor threshold )
01172 {
01173     phFUNCTION("phSimpleVision::setThreshold")
01174     int locked = 0;
01175 
01176     phTHIS_LOCK(locked);
01177 
01178     this->m_trainedThreshold = threshold;
01179         
01180     phTHIS_UNLOCK(locked);
01181     
01182     return phSUCCESS;
01183 error:
01184     phTHIS_ERROR_UNLOCK(locked);
01185 
01186     return phFAIL;
01187 }
01188 
01189 /* ------------------------------------------------------------------------- */
01190 int phSimpleVision::applyTraining( int add_or_set )
01191 {
01192     phFUNCTION("phSimpleVision::applyTraining")
01193     int         locked  = 0;
01194     blob_Filter *blob   = NULL;
01195 
01196     phTHIS_LOCK(locked);
01197             
01198     blob = (blob_Filter *)this->m_filters[this->m_blobIndex];
01199 
01200     if (add_or_set == phSimpleVision_ADD)
01201     {
01202         rc = blob->addColor(this->m_trainedColor,this->m_trainedThreshold);
01203         phPRINT_RC(rc,NULL,"blob->addColor");        
01204     }
01205     else if (add_or_set == phSimpleVision_SET)
01206     {
01207         rc = blob->setColor(this->m_trainedColor,this->m_trainedThreshold);
01208         phPRINT_RC(rc,NULL,"blob->setColor");        
01209     }
01210  
01211     phTHIS_UNLOCK(locked);
01212     
01213     return phSUCCESS;
01214 error:
01215     phTHIS_ERROR_UNLOCK(locked);
01216 
01217     return phFAIL;
01218 }
01219     
01220 /* ------------------------------------------------------------------------- */
01221 int phSimpleVision::initBlobSettings( )
01222 {
01223     phFUNCTION("phSimpleVision::initBlobSettings")
01224     int locked = 0;
01225         
01226     blob_Filter *blob = NULL;
01227 
01228     phTHIS_LOOSE_LOCK(locked);
01229     
01230     blob = (blob_Filter *)this->m_filters[this->m_blobIndex];
01231     
01232     this->m_blobMinsize = (int32_t)((this->m_capture->getWidth() * 
01233                                      this->m_capture->getHeight()) * 
01234                                     this->m_blobMinPercentage);
01235 
01236     rc = blob->setOutcolor(this->m_blobOutcolor);
01237     phPRINT_RC(rc,NULL,"blob->setOutcolor");
01238     
01239     rc = blob->setDrawRects(1);
01240     phPRINT_RC(rc,NULL,"blob->setDrawRects");
01241 
01242     rc = blob->setColorBlobs(1);
01243     phPRINT_RC(rc,NULL,"blob->setColorBlobs");
01244     
01245     rc = blob->setColorMinSize(this->m_blobMinsize);
01246     phPRINT_RC(rc,NULL,"blob->setColorMinSize");
01247     
01248     phTHIS_LOOSE_UNLOCK(locked);
01249 
01250     return phSUCCESS;
01251 }
01252 
01253 /* ------------------------------------------------------------------------- */
01254 int phSimpleVision::private_tracking( int which )
01255 {
01256     phFUNCTION("phSimpleVision::private_tracking")
01257     int indexes[] = {this->m_gaussianIndex,
01258                      which,
01259                      this->m_blobIndex };
01260     int n = sizeof(indexes) / sizeof(int);
01261     
01262     int                 locked          = 0;
01263 
01264     blob_Filter         *blob           = NULL;
01265     convert_Filter      *convert        = NULL;
01266     int                 runningBlob     = 0;
01267     int                 runningConvert  = 0;
01268 
01269     phTHIS_LOCK(locked);
01270     
01271     /* Make sure the histogram filter and the repective colorspace
01272      * conversion filter is running in the pipeline */
01273     blob = (blob_Filter *)this->m_filters[this->m_blobIndex];
01274     convert = (convert_Filter *)this->m_filters[which];
01275    
01276     /* Find out if the respective filters are in the pipeline */
01277     runningBlob = this->m_pipeline->query(blob);
01278     runningConvert  = this->m_pipeline->query(convert);
01279 
01280     /* make sure conversion and blob filters are running */
01281     if ((runningBlob == 0) || (runningConvert == 0))
01282     {
01283         rc = this->switchToFilters(indexes,n);
01284         phCHECK_RC(rc,NULL,"this->switchToFilters");
01285     }
01286     
01287     phTHIS_UNLOCK(locked);
01288     
01289     return phSUCCESS;
01290 error:
01291     phTHIS_ERROR_UNLOCK(locked);
01292     
01293     return phFAIL;
01294 }
01295 
01296 /* ------------------------------------------------------------------------- */
01297 int phSimpleVision::trackingHSV( )
01298 {
01299     return this->private_tracking(this->m_HSVIndex);
01300 }
01301 
01302 /* ------------------------------------------------------------------------- */
01303 int phSimpleVision::trackingRGB( )
01304 {
01305     return this->private_tracking(this->m_RGBIndex);
01306 }
01307 
01308 /* ------------------------------------------------------------------------- */
01309 int phSimpleVision::track( int colorspace )
01310 {
01311     phFUNCTION("phSimpleVision::track")
01312     int                 retrc           = phSUCCESS;
01313     blob_Filter        *blob            = NULL;
01314     int32_t             locked          = 0;
01315     uint32_t            bc              = 0;
01316     
01317     phTHIS_LOCK(locked);
01318    
01319     blob = (blob_Filter *)this->m_filters[this->m_blobIndex];
01320 
01321     /* If the colorspace is set to be RGB */
01322     if (colorspace == phSimpleVision_RGB)
01323     {
01324         rc = this->trackingRGB();
01325         phCHECK_RC(rc,NULL,"this->trackingRGB()");
01326     }
01327     /* Otherwise, default to HSV */ 
01328     else
01329     {
01330         rc = this->trackingHSV();
01331         phCHECK_RC(rc,NULL,"this->trackingHSV()");
01332     }
01333     
01334     if ((this->m_state == phSimpleVision_Stopped) ||
01335         (this->m_state == phSimpleVision_Paused) ||
01336         (!this->m_pipeline->isPipelineActive()))
01337     {
01338         rc = this->start();
01339         phCHECK_RC(rc,NULL,"this->start");
01340     }
01341  
01342     /* Lock the pipeline for the length of this method so it won't be
01343      * altered by external forces while we train */
01344    
01345     /* pass 1/phLiveObjectNOWAIT for non-blocking */
01346     rc = this->m_blobData->update(phLiveObjectNOWAIT); 
01347     phCHECK_RC(rc,NULL,"this->m_blobData->update()");
01348 
01349     if (rc == phLiveObjectNOUPDATE)
01350     {
01351         retrc = phSimpleVision_NOUPDATE;
01352     }
01353     else
01354     {
01355         bc = this->m_blobData->getTotalBlobs();
01356         if (bc > 0)
01357         {
01358             phblob *array = this->m_blobData->getBlobArray();
01359             this->m_maxBlob = array[0];
01360         }
01361         else
01362         {
01363             phMemset(&this->m_maxBlob,0,sizeof(phblob));
01364         }
01365     }
01366     phTHIS_UNLOCK(locked);
01367     
01368     return retrc;
01369 error:
01370     phTHIS_ERROR_UNLOCK(locked);
01371     
01372     return phFAIL;
01373 }
01374 
01375 /* ------------------------------------------------------------------------- */
01376 /* getMaxBlob - returns the phblob structure that contains the maximum
01377  * blob information. If there is no blob, all the values will be
01378  * set to 0;
01379  *
01380  *  Relevant phblob fields:
01381  *
01382  * uint32_t mass : number of pixels covered by blob
01383  * uint32_t cx
01384  * uint32_t cy  : center y coord. of the rectangle that surrounds the blob
01385  * uint32_t x1  : upper left x coordinate
01386  * uint32_t y1  : upper left y coordinate
01387  * uint32_t x2  : lower right x coordinate
01388  * uint32_t y2  : lower right y coordinate
01389  * uint32_t w   : width ...
01390  * uint32_t h   : height of rectangle that surrounds the blob                */
01391 /* ------------------------------------------------------------------------- */
01392 phblob phSimpleVision::getMaxBlob( )
01393 {
01394     return this->m_maxBlob;
01395 }
01396 
01397 /* ------------------------------------------------------------------------- */
01398 uint32_t phSimpleVision::getBlobCount( uint32_t minsize )
01399 {
01400     phFUNCTION("phSimpleVision::getBlobCount")
01401     int locked = 0;
01402     uint32_t bc = 0;
01403 
01404     phTHIS_LOCK(locked);
01405         
01406     bc = this->m_blobData->getTotalBlobs((uint32_t)minsize);
01407     
01408     phTHIS_UNLOCK(locked);
01409     
01410     return bc;
01411 error:
01412     phTHIS_ERROR_UNLOCK(locked);
01413 
01414     return 0;
01415 }
01416 
01417 /* ------------------------------------------------------------------------- */
01418 uint32_t phSimpleVision::getBlobCount()
01419 {
01420     return this->getBlobCount(this->m_blobMinsize);
01421 }
01422 
01423 /* ------------------------------------------------------------------------- */
01424 int phSimpleVision::resetTrackingData( )
01425 {
01426     phFUNCTION("phSimpleVision::resetTrackingData")
01427     int             locked  = 0;
01428     blob_Filter    *b       = NULL;
01429 
01430     phTHIS_LOCK(locked);
01431     
01432     b = (blob_Filter *)this->m_filters[this->m_blobIndex];
01433 
01434     /* Reset the maxblob because it is no longer valid */
01435     phMemset(&this->m_maxBlob,0,sizeof(phblob));
01436 
01437     rc = this->m_blobData->reset();
01438     phPRINT_RC(rc,NULL,"this->m_blobData->reset() failed");
01439 
01440     rc = b->reset();
01441     phPRINT_RC(rc,NULL,"blobfilter->reset() failed.");
01442 
01443     phTHIS_UNLOCK(locked);
01444     
01445     return phSUCCESS;
01446 error:
01447     phTHIS_ERROR_UNLOCK(locked);
01448 
01449     return phFAIL;
01450 }
01451 
01452 
01453 
01454 /* ------------------------------------------------------------------------- */
01455 blob_Filter *phSimpleVision::getBlobFilter( )
01456 {
01457     phFUNCTION("phSimpleVision::getBlobFilter")
01458 
01459     if (this->m_blobIndex > 0)
01460     {
01461         return (blob_Filter *)this->m_filters[this->m_blobIndex];
01462     }
01463     return (blob_Filter *)NULL;
01464 }
01465 
01466 /* ------------------------------------------------------------------------- */
01467 phBlobData &phSimpleVision::getBlobData( )
01468 {
01469     phFUNCTION("phSimpleVision::getBlobData")
01470 
01471     return *(this->m_blobData);
01472 }
01473 
01474 /* ------------------------------------------------------------------------- */
01475 histogram_Filter *phSimpleVision::getHistogramFilter( )
01476 {
01477     if (this->m_histIndex > 0)
01478     {
01479         return (histogram_Filter *)this->m_filters[this->m_histIndex];
01480     }
01481     return (histogram_Filter *)NULL;
01482 }
01483 
01484 /* ------------------------------------------------------------------------- */
01485 phHistogramData &phSimpleVision::getHistogramData( )
01486 {
01487     return *(this->m_histData);
01488 }
01489 
01490 /* ------------------------------------------------------------------------- */
01491 phSystem *phSimpleVision::getSystem( )
01492 {
01493     return this->m_system;
01494 }
01495 
01496 
01497 /* ------------------------------------------------------------------------- */
01498 /* Retrieve the capture class */
01499 phImageCapture *phSimpleVision::getCapture( )
01500 {
01501     return this->m_capture;
01502 }
01503 
01504 /* ------------------------------------------------------------------------- */
01505 /* Return the pipeline instance */
01506 phPipeline *phSimpleVision::getPipeline( )
01507 {
01508     return this->m_pipeline;
01509 }
01510 
01511 /* ------------------------------------------------------------------------- */
01512 /* Retrieve the display classes */
01513 phDisplayInterface *phSimpleVision::getDisplay( int which )
01514 {
01515     phFUNCTION("phSimpleVision::getDisplay")
01516     int locked = 0;
01517     phDisplayInterface *d = NULL;
01518 
01519     phTHIS_LOCK(locked);
01520     
01521     if ((which == phSimpleVisionOutput_Capture) ||
01522         (which == phSimpleVisionOutput_Pipeline))
01523     {
01524         d = this->m_displays[which];
01525     }
01526     
01527     phTHIS_UNLOCK(locked);
01528     
01529     return d;
01530 error:
01531     phTHIS_ERROR_UNLOCK(locked);
01532 
01533     return NULL;
01534 }
01535 
01536 /* ------------------------------------------------------------------------- */
01537 NetDisplay *phSimpleVision::getNetDisplay( int which )
01538 {
01539     phFUNCTION("phSimpleVision::getNetDisplay")
01540     int locked = 0;
01541     NetDisplay *net = NULL;
01542 
01543     phTHIS_LOCK(locked);
01544     
01545     if ((which == phSimpleVisionOutput_Capture) ||
01546         (which == phSimpleVisionOutput_Pipeline))
01547     {
01548         int index = phSimpleVision_nDisplays + which;
01549         net = (NetDisplay *)this->m_displays[index];
01550     }
01551     
01552     phTHIS_UNLOCK(locked);
01553     
01554     return net;
01555 error:
01556     phTHIS_ERROR_UNLOCK(locked);
01557 
01558     return NULL;
01559 }
01560 
01561 /* ------------------------------------------------------------------------- */




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:11 2007 for phission by  doxygen 1.4.4