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

121 lines
2.2 KiB
C++

#if !defined(_COMPONENT_LABELING_H_INCLUDED)
#define _COMPONENT_LABELING_H_INCLUDED
//#define DEBUG_COMPONENT_LABELLING
#include <thread>
#include <vector>
#include "BlobContour.h"
#include "defines.h"
#include "blob.h"
#include "opencv2/opencv.hpp"
//#include <pthread.h>
//! forward decl. for "parent" field of CompLabeler.
class CompLabelerGroup;
//! implementation of F.Chang algorithm (Luca Nardelli)
class CompLabeler {
public:
//! Double pointer so to pass the array of blob pointers
CompLabeler(cv::Mat& binImage, BlobContour** lab, cv::Point start = cv::Point(-1, -1), cv::Point end = cv::Point(-1, -1));
~CompLabeler();
//! Do labeling in region defined by startpoint and endpoint, populating blobs
void label();
//! Resets internal buffers
void reset();
//! External contours tracer
void tracerExt();
//! Internal contours tracer
void tracerInt(int startDir = 5);
//! Counter clockwise
void getNextPointCCW();
//! Clockwise
void getNextPointCW();
//! Thread function
static void* threadLabeling(void* o);
public:
BlobVector m_blobs;
cv::Mat m_binaryImage;
cv::Point m_startPoint, m_endPoint;
private:
friend class CompLabelerGroup;
//! CompLabelerGroup parent, in order to get access to mutexes.
CompLabelerGroup* m_parent;
BlobContour** m_labels;
//! currentLabel
int m_currentLabel;
int m_r, m_c, m_pos;
//! Width& Height of image
int m_w, m_h;
uchar m_dir;
static int m_freemanR[8], m_freemanC[8];
bool m_singlePixBlob;
uchar* m_ptrDataBinary;
BlobContour** m_ptrDataLabels;
int m_tempR, m_tempC;
Blob* m_currentBlob;
BlobContour* m_currentContour;
};
class CompLabelerGroup {
public:
CompLabelerGroup();
~CompLabelerGroup();
void doLabeling(BlobVector& blobs);
void set(int numThreads, const cv::Mat& img);
void reset();
private:
void acquireMutex();
void releaseMutex();
public:
cv::Mat m_img;
private:
friend class CompLabeler;
CompLabeler** m_labelers;
int m_numThreads;
std::mutex m_mutexBlob;
//pthread_t* m_tIds;
//pthread_mutex_t m_mutexBlob;
BlobContour** m_labels;
};
#endif //!_COMPONENT_LABELING_H_INCLUDED