BinRecognizer.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
00090 cvStartReadSeq( squares, &reader, 0 );
00091
00092 int x = 0;
00093 int y = 0;
00094
00095
00096 for( i = 0; i < squares->total; i += 4 )
00097 {
00098 CvPoint pt[4];
00099
00100
00101
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
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
00136 Image<PixRGB<byte> > BinRecognizer::drawSquares( IplImage* in, CvSeq* squares )
00137 {
00138 CvSeqReader reader;
00139 int i;
00140
00141
00142 cvStartReadSeq( squares, &reader, 0 );
00143
00144
00145 for( i = 0; i < squares->total; i += 4 )
00146 {
00147 CvPoint pt[4], *rect = pt;
00148 int count = 4;
00149
00150
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
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
00166
00167
00168