nim_duilib/examples/contour/blob/BlobContour.h
2025-03-16 16:42:44 +08:00

123 lines
2.9 KiB
C++

#ifndef BLOBCONTOUR_H_INCLUDED
#define BLOBCONTOUR_H_INCLUDED
#include <list>
#include "opencv2/imgproc/imgproc_c.h"
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
//! Forward declaration in order to enable the "parent" field
class Blob;
//! Type of chain codes
typedef unsigned char ChainCode;
//! Type of list of chain codes
typedef std::vector<ChainCode> ChainCodeList;
//! In order to emulate CvSeq objects and to comply with opencv 2.0 interface
typedef std::vector<ChainCodeList> ChainCodeContours;
//! Type of list of points
typedef std::vector<cv::Point> PointList;
typedef std::vector<PointList> Contours;
//! Max order of calculated moments
#define MAX_MOMENTS_ORDER 3
//! Blob contour class (in crack code)
class BlobContour
{
public:
//! Constructors
BlobContour();
//! Size is used to empirically reserve internal vectors for contour points.
//! This can be a help for very small images, where the vector would be too large.
BlobContour(const cv::Point& startPoint, const cv::Size& imageRes = cv::Size(-1, -1));
//! Copy constructor
BlobContour(BlobContour* source);
BlobContour(const BlobContour& source);
~BlobContour();
//! Assigment operator
BlobContour& operator=(const BlobContour& source);
//! Add point to end of contour, according to chain code.
void addChainCode(ChainCode code);
//! Return freeman chain coded contour
ChainCodeList& getChainCodeList() {
return m_contour[0];
}
bool isEmpty() {
return m_contour.size() == 0;
}
//! Returns first contour
const PointList& getContourPoints();
//! Returns all contours (compatible with drawContours structure)
Contours& getContours();
void shiftBlobContour(int x, int y);
const cv::Point& getStartPoint() const {
return m_startPoint;
}
protected:
//! Clears chain code contour
void reset();
//! Computes area from contour
double getArea();
//! Computes perimeter from contour
double getPerimeter();
//! Get contour moment (p,q up to MAX_CALCULATED_MOMENTS)
double getMoment(int p, int q);
//! Crack code list
ChainCodeContours m_contour;
private:
friend class Blob;
friend class CompLabeler;
//! Starting point of the contour
cv::Point m_startPoint;
//! All points from the contour
Contours m_contourPoints;
//! Computed area from contour
double m_area;
//! Computed perimeter from contour
double m_perimeter;
//! Computed moments from contour
CvMoments m_moments;
static const PointList EMPTY_LIST;
//! This value is actually used mainly in the detection part, for the labels.
Blob* m_parent;
};
ChainCode points2ChainCode(const cv::Point& p1, const cv::Point& p2);
cv::Point chainCode2Point(const cv::Point& origin, ChainCode code);
#endif //!BLOBCONTOUR_H_INCLUDED