// deactivation of warning 4786 // (identifier was truncated to '255' characters in the debug information) #if _MSC_VER > 1000 #pragma warning(disable: 4786) #pragma warning(disable: 4503) #endif #include #include "BlobOperators.h" /*************************************************************************** Implementaciķ de les classes per al cālcul de característiques sobre el blob Implementation of the helper classes to perform operations on blobs **************************************************************************/ /** - FUNCTION: Moment - FUNCTIONALITY: Calculates the pq moment of the blob - PARAMETERS: - RESULT: - returns the pq moment or 0 if the moment it is not implemented - RESTRICTIONS: - Currently, implemented moments up to 3 - AUTHOR: Ricard Borrās - CREATION DATE: 20-07-2004. - MODIFICATION: Date. Author. Description. */ float CBlobGetMoment::operator()(CBlob &blob) { return blob.Moment(m_p, m_q); } /** - FUNCIĶ: HullPerimeter - FUNCIONALITAT: Calcula la longitud del perimetre convex del blob. Fa servir la funciķ d'OpenCV cvConvexHull2 per a calcular el perimetre convex. - PARĀMETRES: - RESULTAT: - retorna la longitud del perímetre convex del blob. Si el blob no té coordenades associades retorna el perímetre normal del blob. - RESTRICCIONS: - AUTOR: Ricard Borrās - DATA DE CREACIĶ: 20-07-2004. - MODIFICACIĶ: Data. Autor. Descripciķ. */ /** - FUNCTION: CBlobGetHullPerimeter - FUNCTIONALITY: Calculates the convex hull perimeter of the blob - PARAMETERS: - RESULT: - returns the convex hull perimeter of the blob or the perimeter if the blob edges could not be retrieved - RESTRICTIONS: - AUTHOR: Ricard Borrās - CREATION DATE: 25-05-2005. - MODIFICATION: Date. Author. Description. */ float CBlobGetHullPerimeter::operator()(CBlob &blob) { CvSeq *convexHull; float perimeter; convexHull = blob.GetConvexHull(); if( convexHull ) perimeter = fabs(cvArcLength(convexHull,CV_WHOLE_SEQ,1)); else return 0; cvClearSeq(convexHull); return perimeter; } float CBlobGetHullArea::operator()(CBlob &blob) { CvSeq *convexHull; float area; convexHull = blob.GetConvexHull(); if( convexHull ) area = fabs(cvContourArea(convexHull)); else return 0; cvClearSeq(convexHull); return area; } /** - FUNCTION: CBlobGetMinXatMinY - FUNCTIONALITY: Calculates the minimum X on the minimum Y - PARAMETERS: - RESULT: - RESTRICTIONS: - AUTHOR: Ricard Borrās - CREATION DATE: 25-05-2005. - MODIFICATION: Date. Author. Description. */ float CBlobGetMinXatMinY::operator()(CBlob &blob) { float result = LONG_MAX; CvSeqReader reader; CvPoint actualPoint; t_PointList externContour; externContour = blob.GetExternalContour()->GetContourPoints(); if( !externContour ) return result; cvStartReadSeq( externContour, &reader); for( int i=0; i< externContour->total; i++) { CV_READ_SEQ_ELEM( actualPoint, reader); if( (actualPoint.y == blob.MinY()) && (actualPoint.x < result) ) { result = actualPoint.x; } } return result; } /** - FUNCTION: CBlobGetMinXatMinY - FUNCTIONALITY: Calculates the minimum Y on the maximum X - PARAMETERS: - RESULT: - RESTRICTIONS: - AUTHOR: Ricard Borrās - CREATION DATE: 25-05-2005. - MODIFICATION: Date. Author. Description. */ float CBlobGetMinYatMaxX::operator()(CBlob &blob) { float result = LONG_MAX; CvSeqReader reader; CvPoint actualPoint; t_PointList externContour; externContour = blob.GetExternalContour()->GetContourPoints(); if( !externContour ) return result; cvStartReadSeq( externContour, &reader); for( int i=0; i< externContour->total; i++) { CV_READ_SEQ_ELEM( actualPoint, reader); if( (actualPoint.x == blob.MaxX()) && (actualPoint.y < result) ) { result = actualPoint.y; } } return result; } /** - FUNCTION: CBlobGetMaxXatMaxY - FUNCTIONALITY: Calculates the maximum X on the maximum Y - PARAMETERS: - RESULT: - RESTRICTIONS: - AUTHOR: Ricard Borrās - CREATION DATE: 25-05-2005. - MODIFICATION: Date. Author. Description. */ float CBlobGetMaxXatMaxY::operator()(CBlob &blob) { float result = LONG_MIN; CvSeqReader reader; CvPoint actualPoint; t_PointList externContour; externContour = blob.GetExternalContour()->GetContourPoints(); if( !externContour ) return result; cvStartReadSeq( externContour, &reader); for( int i=0; i< externContour->total; i++) { CV_READ_SEQ_ELEM( actualPoint, reader); if( (actualPoint.y == blob.MaxY()) && (actualPoint.x > result) ) { result = actualPoint.x; } } return result; } /** - FUNCTION: CBlobGetMaxYatMinX - FUNCTIONALITY: Calculates the maximum Y on the minimum X - PARAMETERS: - RESULT: - RESTRICTIONS: - AUTHOR: Ricard Borrās - CREATION DATE: 25-05-2005. - MODIFICATION: Date. Author. Description. */ float CBlobGetMaxYatMinX::operator()(CBlob &blob) { float result = LONG_MIN; CvSeqReader reader; CvPoint actualPoint; t_PointList externContour; externContour = blob.GetExternalContour()->GetContourPoints(); if( !externContour ) return result; cvStartReadSeq( externContour, &reader); for( int i=0; i< externContour->total; i++) { CV_READ_SEQ_ELEM( actualPoint, reader); if( (actualPoint.x == blob.MinX()) && (actualPoint.y > result) ) { result = actualPoint.y; } } return result; } /** - FUNCTION: CBlobGetElongation - FUNCTIONALITY: Calculates the elongation of the blob ( length/breadth ) - PARAMETERS: - RESULT: - RESTRICTIONS: - See below to see how the lenght and the breadth are aproximated - AUTHOR: Ricard Borrās - CREATION DATE: 25-05-2005. - MODIFICATION: Date. Author. Description. */ float CBlobGetElongation::operator()(CBlob &blob) { float ampladaC,longitudC,amplada,longitud; float tmp; if( blob.Area() == 0 ) return 0; tmp = blob.Perimeter()*blob.Perimeter() - 16*blob.Area(); if( tmp > 0.0 ) ampladaC = (float) (blob.Perimeter()+sqrt(tmp))/4; // error intrínsec en els cālculs de l'ārea i el perímetre else ampladaC = (float) (blob.Perimeter())/4; if(ampladaC<=0.0) return 0; longitudC=(float) blob.Area()/ampladaC; longitud=MAX( longitudC , ampladaC ); amplada=MIN( longitudC , ampladaC ); return (float) longitud/amplada; } /** Retorna la compacitat del blob */ /** - FUNCTION: CBlobGetCompactness - FUNCTIONALITY: Calculates the compactness of the blob ( maximum for circle shaped blobs, minimum for the rest) - PARAMETERS: - RESULT: - RESTRICTIONS: - AUTHOR: Ricard Borrās - CREATION DATE: 25-05-2005. - MODIFICATION: Date. Author. Description. */ float CBlobGetCompactness::operator()(CBlob &blob) { if( blob.Area() != 0.0 ) return (float) pow(blob.Perimeter(),2)/(4*CV_PI*blob.Area()); else return 0.0; } /** Retorna la rugositat del blob */ /** - FUNCTION: CBlobGetRoughness - FUNCTIONALITY: Calculates the roughness of the blob ( ratio between perimeter and convex hull perimeter) - PARAMETERS: - RESULT: - RESTRICTIONS: - AUTHOR: Ricard Borrās - CREATION DATE: 25-05-2005. - MODIFICATION: Date. Author. Description. */ float CBlobGetRoughness::operator()(CBlob &blob) { CBlobGetHullPerimeter getHullPerimeter = CBlobGetHullPerimeter(); float hullPerimeter = getHullPerimeter(blob); if( hullPerimeter != 0.0 ) return blob.Perimeter() / hullPerimeter;//HullPerimeter(); return 0.0; } /** Retorna la longitud del blob */ /** - FUNCTION: CBlobGetLength - FUNCTIONALITY: Calculates the lenght of the blob (the biggest axis of the blob) - PARAMETERS: - RESULT: - RESTRICTIONS: - The lenght is an aproximation to the real lenght - AUTHOR: Ricard Borrās - CREATION DATE: 25-05-2005. - MODIFICATION: Date. Author. Description. */ float CBlobGetLength::operator()(CBlob &blob) { float ampladaC,longitudC; float tmp; tmp = blob.Perimeter()*blob.Perimeter() - 16*blob.Area(); if( tmp > 0.0 ) ampladaC = (float) (blob.Perimeter()+sqrt(tmp))/4; // error intrínsec en els cālculs de l'ārea i el perímetre else ampladaC = (float) (blob.Perimeter())/4; if(ampladaC<=0.0) return 0; longitudC=(float) blob.Area()/ampladaC; return MAX( longitudC , ampladaC ); } /** Retorna l'amplada del blob */ /** - FUNCTION: CBlobGetBreadth - FUNCTIONALITY: Calculates the breadth of the blob (the smallest axis of the blob) - PARAMETERS: - RESULT: - RESTRICTIONS: - The breadth is an aproximation to the real breadth - AUTHOR: Ricard Borrās - CREATION DATE: 25-05-2005. - MODIFICATION: Date. Author. Description. */ float CBlobGetBreadth::operator()(CBlob &blob) { float ampladaC,longitudC; float tmp; tmp = blob.Perimeter()*blob.Perimeter() - 16*blob.Area(); if( tmp > 0.0 ) ampladaC = (float) (blob.Perimeter()+sqrt(tmp))/4; // error intrínsec en els cālculs de l'ārea i el perímetre else ampladaC = (float) (blob.Perimeter())/4; if(ampladaC<=0.0) return 0; longitudC = (float) blob.Area()/ampladaC; return MIN( longitudC , ampladaC ); } /** Calcula la distāncia entre un punt i el centre del blob */ /** - FUNCTION: CBlobGetDistanceFromPoint - FUNCTIONALITY: Calculates the euclidean distance between the blob center and the point specified in the constructor - PARAMETERS: - RESULT: - RESTRICTIONS: - AUTHOR: Ricard Borrās - CREATION DATE: 25-05-2005. - MODIFICATION: Date. Author. Description. */ float CBlobGetDistanceFromPoint::operator()(CBlob &blob) { float xmitjana, ymitjana; CBlobGetXCenter getXCenter; CBlobGetYCenter getYCenter; xmitjana = m_x - getXCenter( blob ); ymitjana = m_y - getYCenter( blob ); return sqrt((xmitjana*xmitjana)+(ymitjana*ymitjana)); } /** - FUNCTION: BlobGetXYInside - FUNCTIONALITY: Calculates whether a point is inside the rectangular bounding box of a blob - PARAMETERS: - RESULT: - returns 1 if it is inside; o if not - RESTRICTIONS: - AUTHOR: Francesc Pinyol Margalef - CREATION DATE: 16-01-2006. - MODIFICATION: Date. Author. Description. */ float CBlobGetXYInside::operator()(CBlob &blob) { if( blob.GetExternalContour()->GetContourPoints() ) { return cvPointPolygonTest( blob.GetExternalContour()->GetContourPoints(), m_p,0) >= 0; } return 0; } #ifdef BLOB_OBJECT_FACTORY /** - FUNCIĶ: RegistraTotsOperadors - FUNCIONALITAT: Registrar tots els operadors definits a blob.h - PARĀMETRES: - fabricaOperadorsBlob: fābrica on es registraran els operadors - RESULTAT: - Modifica l'objecte fabricaOperadorsBlob - RESTRICCIONS: - Només es registraran els operadors de blob.h. Si se'n volen afegir, cal afegir-los amb el mčtode Register de la fābrica. - AUTOR: rborras - DATA DE CREACIĶ: 2006/05/18 - MODIFICACIĶ: Data. Autor. Descripciķ. */ void RegistraTotsOperadors( t_OperadorBlobFactory &fabricaOperadorsBlob ) { // blob shape fabricaOperadorsBlob.Register( CBlobGetArea().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetRelativeArea().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetBreadth().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetCompactness().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetElongation().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetExterior().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetLength().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetPerimeter().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetRoughness().GetNom(), Type2Type()); // blob color fabricaOperadorsBlob.Register( CBlobGetMean(NULL).GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetReferencedMean(NULL,0).GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetStdDev(NULL).GetNom(), Type2Type()); // extern pixels fabricaOperadorsBlob.Register( CBlobGetExternPerimeterRatio().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetExternHullPerimeterRatio().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetExternPerimeter().GetNom(), Type2Type()); // hull fabricaOperadorsBlob.Register( CBlobGetHullPerimeter().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetHullArea().GetNom(), Type2Type()); // elipse info fabricaOperadorsBlob.Register( CBlobGetMajorAxisLength().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetMinorAxisLength().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetAxisRatio().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetOrientation().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetOrientationCos().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetAreaElipseRatio().GetNom(), Type2Type()); // min an max fabricaOperadorsBlob.Register( CBlobGetMaxX().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetMaxY().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetMinX().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetMinY().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetMaxXatMaxY().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetMaxYatMinX().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetMinXatMinY().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetMinYatMaxX().GetNom(), Type2Type()); // coordinate info fabricaOperadorsBlob.Register( CBlobGetXYInside().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetDiffY().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetDiffX().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetXCenter().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetYCenter().GetNom(), Type2Type()); fabricaOperadorsBlob.Register( CBlobGetDistanceFromPoint().GetNom(), Type2Type()); // moments fabricaOperadorsBlob.Register( CBlobGetMoment().GetNom(), Type2Type()); } #endif //BLOB_OBJECT_FACTORY