00001 /*!@file SeaBee/BinRecognizer.C find bin */ 00002 // //////////////////////////////////////////////////////////////////// // 00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00004 // University of Southern California (USC) and the iLab at USC. // 00005 // See http://iLab.usc.edu for information about this project. // 00006 // //////////////////////////////////////////////////////////////////// // 00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00009 // in Visual Environments, and Applications'' by Christof Koch and // 00010 // Laurent Itti, California Institute of Technology, 2001 (patent // 00011 // pending; application number 09/912,225 filed July 23, 2001; see // 00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00013 // //////////////////////////////////////////////////////////////////// // 00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00015 // // 00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00017 // redistribute it and/or modify it under the terms of the GNU General // 00018 // Public License as published by the Free Software Foundation; either // 00019 // version 2 of the License, or (at your option) any later version. // 00020 // // 00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00024 // PURPOSE. See the GNU General Public License for more details. // 00025 // // 00026 // You should have received a copy of the GNU General Public License // 00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00029 // Boston, MA 02111-1307 USA. // 00030 // //////////////////////////////////////////////////////////////////// // 00031 // 00032 // Primary maintainer for this file: Michael Montalbo <montalbo@usc.edu> 00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/SeaBee/BinRecognizer.C $ 00034 // $Id: BinRecognizer.C 10794 2009-02-08 06:21:09Z itti $ 00035 00036 #include "GUI/XWinManaged.H" 00037 #include "Image/Image.H" 00038 #include "Image/Pixels.H" 00039 #include "Util/Types.H" 00040 #include "Util/log.H" 00041 #include "Image/DrawOps.H" 00042 00043 #include "MBARI/Geometry2D.H" 00044 #include "Image/OpenCVUtil.H" 00045 #include "Image/ColorOps.H" 00046 00047 #include "SeaBee/VisionRecognizer.H" 00048 #include "SeaBee/BinRecognizer.H" 00049 00050 // ###################################################################### 00051 BinRecognizer::BinRecognizer() 00052 { 00053 } 00054 00055 // ###################################################################### 00056 BinRecognizer::~BinRecognizer() 00057 { } 00058 00059 // ###################################################################### 00060 uint BinRecognizer::getBinLocation(rutz::shared_ptr<Image<PixRGB <byte> > > image, 00061 rutz::shared_ptr<Image<PixRGB <byte> > > outputImage, 00062 BinRecognizer::BinRecognizeMethod method, 00063 rutz::shared_ptr<Point2D<int> > binCenter, 00064 uint &staleCount 00065 ) 00066 { 00067 CvSeq* squares; 00068 switch(method) 00069 { 00070 case BinRecognizer::CONTOUR: 00071 squares = getContours(img2ipl(*image)); 00072 getBinCenter(staleCount, binCenter, squares); 00073 *outputImage = drawSquares(img2ipl(*image), squares); 00074 return 0; 00075 break; 00076 default: 00077 LERROR("Invalid bin recognizer method specified"); 00078 return 0; 00079 } 00080 } 00081 00082 void BinRecognizer::getBinCenter(uint &staleCount, 00083 rutz::shared_ptr<Point2D<int> > binCenter, 00084 CvSeq* squares) 00085 { 00086 CvSeqReader reader; 00087 int i; 00088 00089 // initialize reader of the sequence 00090 cvStartReadSeq( squares, &reader, 0 ); 00091 00092 int x = 0; 00093 int y = 0; 00094 00095 // read 4 sequence elements at a time (all vertices of a square) 00096 for( i = 0; i < squares->total; i += 4 ) 00097 { 00098 CvPoint pt[4];//, *rect = pt; 00099 //int count = 4; 00100 00101 // read 4 vertices 00102 CV_READ_SEQ_ELEM( pt[0], reader ); 00103 CV_READ_SEQ_ELEM( pt[1], reader ); 00104 CV_READ_SEQ_ELEM( pt[2], reader ); 00105 CV_READ_SEQ_ELEM( pt[3], reader ); 00106 00107 x += pt[0].x; 00108 x += pt[1].x; 00109 x += pt[2].x; 00110 x += pt[3].x; 00111 00112 y += pt[0].y; 00113 y += pt[1].y; 00114 y += pt[2].y; 00115 y += pt[3].y; 00116 00117 } 00118 00119 //LINFO("x:%d y:%d",x,y); 00120 int avgX = x/4; 00121 int avgY = y/4; 00122 00123 if(x > 0 && y > 0 && avgY < 100 ) 00124 { 00125 binCenter->i = avgX; 00126 binCenter->j = avgY; 00127 staleCount = 0; 00128 } 00129 else 00130 { 00131 staleCount++; 00132 } 00133 } 00134 // ###################################################################### 00135 // the function draws all the squares in the image 00136 Image<PixRGB<byte> > BinRecognizer::drawSquares( IplImage* in, CvSeq* squares ) 00137 { 00138 CvSeqReader reader; 00139 int i; 00140 00141 // initialize reader of the sequence 00142 cvStartReadSeq( squares, &reader, 0 ); 00143 00144 // read 4 sequence elements at a time (all vertices of a square) 00145 for( i = 0; i < squares->total; i += 4 ) 00146 { 00147 CvPoint pt[4], *rect = pt; 00148 int count = 4; 00149 00150 // read 4 vertices 00151 CV_READ_SEQ_ELEM( pt[0], reader ); 00152 CV_READ_SEQ_ELEM( pt[1], reader ); 00153 CV_READ_SEQ_ELEM( pt[2], reader ); 00154 CV_READ_SEQ_ELEM( pt[3], reader ); 00155 00156 // draw the square as a closed polyline 00157 cvPolyLine( in, &rect, &count, 1, 1, CV_RGB(0,255,0), 3, CV_AA, 0 ); 00158 } 00159 00160 return ipl2rgb(in); 00161 00162 } 00163 00164 // ###################################################################### 00165 /* So things look consistent in everyone's emacs... */ 00166 /* Local Variables: */ 00167 /* indent-tabs-mode: nil */ 00168 /* End: */