A few changes to comply with upstream requirements for merge.
-Change input/output order from (out Labeled, in Image) -> (in Image, Out Labeled) and convert to Input/OutputArrays in the process. -Adopt OutputArray for statistics export so that the algorithm is "wrapper friendly" and not requiring a new struct in language bindings at the expense of using doubles for everything and slowing statistics computation down..
This commit is contained in:
parent
d5aa679d3f
commit
00bdca7684
@ -1091,24 +1091,13 @@ enum { TM_SQDIFF=0, TM_SQDIFF_NORMED=1, TM_CCORR=2, TM_CCORR_NORMED=3, TM_CCOEFF
|
|||||||
CV_EXPORTS_W void matchTemplate( InputArray image, InputArray templ,
|
CV_EXPORTS_W void matchTemplate( InputArray image, InputArray templ,
|
||||||
OutputArray result, int method );
|
OutputArray result, int method );
|
||||||
|
|
||||||
struct CV_EXPORTS ConnectedComponentStats
|
enum { CC_STAT_LEFT=0, CC_STAT_TOP=1, CC_STAT_WIDTH=2, CC_STAT_HEIGHT=3, CC_STAT_CX=4, CC_STAT_CY=5, CC_STAT_AREA=6, CC_STAT_INTEGRAL_X=7, CC_STAT_INTEGRAL_Y=8, CC_STAT_MAX = 9};
|
||||||
{
|
|
||||||
int lower_x;//!< lower left corner column
|
|
||||||
int lower_y;//!< lower left corner row
|
|
||||||
int upper_x;//!< upper right corner column
|
|
||||||
int upper_y;//!< upper right corner row
|
|
||||||
double centroid_x;//!< centroid column
|
|
||||||
double centroid_y;//!< centroid row
|
|
||||||
uint64 integral_x;//!< sum of all columns where the image was non-zero
|
|
||||||
uint64 integral_y;//!< sum of all rows where the image was non-zero
|
|
||||||
unsigned int area;//!< count of all non-zero pixels
|
|
||||||
};
|
|
||||||
|
|
||||||
//! computes the connected components labeled image of boolean image I with 4 or 8 way connectivity - returns N, the total
|
//! computes the connected components labeled image of boolean image I with 4 or 8 way connectivity - returns N, the total
|
||||||
//number of labels [0, N-1] where 0 represents the background label. L's value type determines the label type, an important
|
//number of labels [0, N-1] where 0 represents the background label. L's value type determines the label type, an important
|
||||||
//consideration based on the total number of labels or alternatively the total number of pixels.
|
//consideration based on the total number of labels or alternatively the total number of pixels.
|
||||||
CV_EXPORTS_W int connectedComponents(CV_OUT Mat &L, const Mat &I, int connectivity = 8);
|
CV_EXPORTS_W int connectedComponents(InputArray image, OutputArray labels, int connectivity = 8);
|
||||||
CV_EXPORTS_W int connectedComponentsWithStats(CV_OUT Mat &L, const Mat &I, CV_OUT std::vector<ConnectedComponentStats> &statsv, int connectivity = 8);
|
CV_EXPORTS_W int connectedComponentsWithStats(InputArray image, OutputArray labels, OutputArray stats, int connectivity = 8);
|
||||||
|
|
||||||
|
|
||||||
//! mode of the contour retrieval algorithm
|
//! mode of the contour retrieval algorithm
|
||||||
|
@ -71,51 +71,54 @@ namespace cv{
|
|||||||
};
|
};
|
||||||
template<typename LabelT>
|
template<typename LabelT>
|
||||||
struct CCStatsOp{
|
struct CCStatsOp{
|
||||||
std::vector<cv::ConnectedComponentStats> &statsv;
|
cv::Mat statsv;
|
||||||
CCStatsOp(std::vector<cv::ConnectedComponentStats> &_statsv): statsv(_statsv){
|
CCStatsOp(OutputArray _statsv): statsv(_statsv.getMat()){
|
||||||
}
|
}
|
||||||
inline
|
inline
|
||||||
void init(const LabelT nlabels){
|
void init(const LabelT nlabels){
|
||||||
statsv.clear();
|
statsv = cv::Mat(nlabels, CC_STAT_MAX, cv::DataType<double>::type);
|
||||||
cv::ConnectedComponentStats stats = cv::ConnectedComponentStats();
|
for(int l = 0; l < (int) nlabels; ++l){
|
||||||
stats.lower_x = std::numeric_limits<LabelT>::max();
|
double *row = &statsv.at<double>(l, 0);
|
||||||
stats.lower_y = std::numeric_limits<LabelT>::max();
|
row[CC_STAT_LEFT] = std::numeric_limits<LabelT>::max();
|
||||||
stats.upper_x = std::numeric_limits<LabelT>::min();
|
row[CC_STAT_TOP] = std::numeric_limits<LabelT>::max();
|
||||||
stats.upper_y = std::numeric_limits<LabelT>::min();
|
row[CC_STAT_WIDTH] = std::numeric_limits<LabelT>::min();
|
||||||
stats.centroid_x = 0;
|
row[CC_STAT_HEIGHT] = std::numeric_limits<LabelT>::min();
|
||||||
stats.centroid_y = 0;
|
row[CC_STAT_CX] = 0;
|
||||||
stats.integral_x = 0;
|
row[CC_STAT_CY] = 0;
|
||||||
stats.integral_y = 0;
|
row[CC_STAT_AREA] = 0;
|
||||||
stats.area = 0;
|
row[CC_STAT_INTEGRAL_X] = 0;
|
||||||
statsv.resize(nlabels, stats);
|
row[CC_STAT_INTEGRAL_Y] = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
void operator()(int r, int c, LabelT l){
|
void operator()(int r, int c, LabelT l){
|
||||||
ConnectedComponentStats &stats = statsv[l];
|
double *row = &statsv.at<double>(l, 0);
|
||||||
if(c > stats.upper_x){
|
if(c > row[CC_STAT_WIDTH]){
|
||||||
stats.upper_x = c;
|
row[CC_STAT_WIDTH] = c;
|
||||||
}else{
|
}else{
|
||||||
if(c < stats.lower_x){
|
if(c < row[CC_STAT_LEFT]){
|
||||||
stats.lower_x = c;
|
row[CC_STAT_LEFT] = c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(r > stats.upper_y){
|
if(r > row[CC_STAT_HEIGHT]){
|
||||||
stats.upper_y = r;
|
row[CC_STAT_HEIGHT] = r;
|
||||||
}else{
|
}else{
|
||||||
if(r < stats.lower_y){
|
if(r < row[CC_STAT_TOP]){
|
||||||
stats.lower_y = r;
|
row[CC_STAT_TOP] = r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stats.integral_x += c;
|
row[CC_STAT_INTEGRAL_X] += c;
|
||||||
stats.integral_y += r;
|
row[CC_STAT_INTEGRAL_Y] += r;
|
||||||
stats.area++;
|
row[CC_STAT_AREA]++;
|
||||||
}
|
}
|
||||||
void finish(){
|
void finish(){
|
||||||
for(size_t l = 0; l < statsv.size(); ++l){
|
for(int l = 0; l < statsv.rows; ++l){
|
||||||
ConnectedComponentStats &stats = statsv[l];
|
double *row = &statsv.at<double>(l, 0);
|
||||||
stats.lower_x = std::min(stats.lower_x, stats.upper_x);
|
row[CC_STAT_LEFT] = std::min(row[CC_STAT_LEFT], row[CC_STAT_WIDTH]);
|
||||||
stats.lower_y = std::min(stats.lower_y, stats.upper_y);
|
row[CC_STAT_WIDTH] = row[CC_STAT_WIDTH] - row[CC_STAT_LEFT] + 1;
|
||||||
stats.centroid_x = stats.integral_x / double(stats.area);
|
row[CC_STAT_TOP] = std::min(row[CC_STAT_TOP], row[CC_STAT_HEIGHT]);
|
||||||
stats.centroid_y = stats.integral_y / double(stats.area);
|
row[CC_STAT_HEIGHT] = row[CC_STAT_HEIGHT] - row[CC_STAT_TOP] + 1;
|
||||||
|
row[CC_STAT_CX] = row[CC_STAT_INTEGRAL_X] / double(row[CC_STAT_AREA]);
|
||||||
|
row[CC_STAT_CY] = row[CC_STAT_INTEGRAL_Y] / double(row[CC_STAT_AREA]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -193,7 +196,11 @@ namespace cv{
|
|||||||
const int G8[4][2] = {{1, -1}, {1, 0}, {1, 1}, {0, -1}};//a, b, c, d neighborhoods
|
const int G8[4][2] = {{1, -1}, {1, 0}, {1, 1}, {0, -1}};//a, b, c, d neighborhoods
|
||||||
template<typename LabelT, typename PixelT, typename StatsOp = NoOp<LabelT>, int connectivity = 8>
|
template<typename LabelT, typename PixelT, typename StatsOp = NoOp<LabelT>, int connectivity = 8>
|
||||||
struct LabelingImpl{
|
struct LabelingImpl{
|
||||||
LabelT operator()(Mat &L, const Mat &I, StatsOp &sop){
|
LabelT operator()(InputArray _I, OutputArray _L, StatsOp &sop){
|
||||||
|
cv::Mat I = _I.getMat();
|
||||||
|
cv::Mat L = _L.getMat();
|
||||||
|
CV_Assert(L.rows == I.rows);
|
||||||
|
CV_Assert(L.cols == I.cols);
|
||||||
const int rows = L.rows;
|
const int rows = L.rows;
|
||||||
const int cols = L.cols;
|
const int cols = L.cols;
|
||||||
size_t Plength = (size_t(rows + 3 - 1)/3) * (size_t(cols + 3 - 1)/3);
|
size_t Plength = (size_t(rows + 3 - 1)/3) * (size_t(cols + 3 - 1)/3);
|
||||||
@ -340,9 +347,7 @@ namespace cv{
|
|||||||
|
|
||||||
//L's type must have an appropriate depth for the number of pixels in I
|
//L's type must have an appropriate depth for the number of pixels in I
|
||||||
template<typename StatsOp>
|
template<typename StatsOp>
|
||||||
int connectedComponents_sub1(Mat &L, const Mat &I, int connectivity, StatsOp &sop){
|
int connectedComponents_sub1(InputArray I, OutputArray L, int connectivity, StatsOp &sop){
|
||||||
CV_Assert(L.rows == I.rows);
|
|
||||||
CV_Assert(L.cols == I.cols);
|
|
||||||
CV_Assert(L.channels() == 1 && I.channels() == 1);
|
CV_Assert(L.channels() == 1 && I.channels() == 1);
|
||||||
CV_Assert(connectivity == 8 || connectivity == 4);
|
CV_Assert(connectivity == 8 || connectivity == 4);
|
||||||
|
|
||||||
@ -354,9 +359,9 @@ int connectedComponents_sub1(Mat &L, const Mat &I, int connectivity, StatsOp &so
|
|||||||
if(lDepth == CV_8U){
|
if(lDepth == CV_8U){
|
||||||
if(iDepth == CV_8U || iDepth == CV_8S){
|
if(iDepth == CV_8U || iDepth == CV_8S){
|
||||||
if(connectivity == 4){
|
if(connectivity == 4){
|
||||||
return (int) LabelingImpl<uint8_t, uint8_t, StatsOp, 4>()(L, I, sop);
|
return (int) LabelingImpl<uint8_t, uint8_t, StatsOp, 4>()(I, L, sop);
|
||||||
}else{
|
}else{
|
||||||
return (int) LabelingImpl<uint8_t, uint8_t, StatsOp, 8>()(L, I, sop);
|
return (int) LabelingImpl<uint8_t, uint8_t, StatsOp, 8>()(I, L, sop);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
CV_Assert(false);
|
CV_Assert(false);
|
||||||
@ -364,9 +369,9 @@ int connectedComponents_sub1(Mat &L, const Mat &I, int connectivity, StatsOp &so
|
|||||||
}else if(lDepth == CV_16U){
|
}else if(lDepth == CV_16U){
|
||||||
if(iDepth == CV_8U || iDepth == CV_8S){
|
if(iDepth == CV_8U || iDepth == CV_8S){
|
||||||
if(connectivity == 4){
|
if(connectivity == 4){
|
||||||
return (int) LabelingImpl<uint16_t, uint8_t, StatsOp, 4>()(L, I, sop);
|
return (int) LabelingImpl<uint16_t, uint8_t, StatsOp, 4>()(I, L, sop);
|
||||||
}else{
|
}else{
|
||||||
return (int) LabelingImpl<uint16_t, uint8_t, StatsOp, 8>()(L, I, sop);
|
return (int) LabelingImpl<uint16_t, uint8_t, StatsOp, 8>()(I, L, sop);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
CV_Assert(false);
|
CV_Assert(false);
|
||||||
@ -376,9 +381,9 @@ int connectedComponents_sub1(Mat &L, const Mat &I, int connectivity, StatsOp &so
|
|||||||
//OpenCV: how should we proceed? .at<T> typechecks in debug mode
|
//OpenCV: how should we proceed? .at<T> typechecks in debug mode
|
||||||
if(iDepth == CV_8U || iDepth == CV_8S){
|
if(iDepth == CV_8U || iDepth == CV_8S){
|
||||||
if(connectivity == 4){
|
if(connectivity == 4){
|
||||||
return (int) LabelingImpl<int32_t, uint8_t, StatsOp, 4>()(L, I, sop);
|
return (int) LabelingImpl<int32_t, uint8_t, StatsOp, 4>()(I, L, sop);
|
||||||
}else{
|
}else{
|
||||||
return (int) LabelingImpl<int32_t, uint8_t, StatsOp, 8>()(L, I, sop);
|
return (int) LabelingImpl<int32_t, uint8_t, StatsOp, 8>()(I, L, sop);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
CV_Assert(false);
|
CV_Assert(false);
|
||||||
@ -389,28 +394,28 @@ int connectedComponents_sub1(Mat &L, const Mat &I, int connectivity, StatsOp &so
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int connectedComponents(Mat &L, const Mat &I, int connectivity){
|
int connectedComponents(InputArray I, OutputArray L, int connectivity){
|
||||||
int lDepth = L.depth();
|
int lDepth = L.depth();
|
||||||
if(lDepth == CV_8U){
|
if(lDepth == CV_8U){
|
||||||
connectedcomponents::NoOp<uint8_t> sop; return connectedComponents_sub1(L, I, connectivity, sop);
|
connectedcomponents::NoOp<uint8_t> sop; return connectedComponents_sub1(I, L, connectivity, sop);
|
||||||
}else if(lDepth == CV_16U){
|
}else if(lDepth == CV_16U){
|
||||||
connectedcomponents::NoOp<uint16_t> sop; return connectedComponents_sub1(L, I, connectivity, sop);
|
connectedcomponents::NoOp<uint16_t> sop; return connectedComponents_sub1(I, L, connectivity, sop);
|
||||||
}else if(lDepth == CV_32S){
|
}else if(lDepth == CV_32S){
|
||||||
connectedcomponents::NoOp<uint32_t> sop; return connectedComponents_sub1(L, I, connectivity, sop);
|
connectedcomponents::NoOp<uint32_t> sop; return connectedComponents_sub1(I, L, connectivity, sop);
|
||||||
}else{
|
}else{
|
||||||
CV_Assert(false);
|
CV_Assert(false);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int connectedComponentsWithStats(Mat &L, const Mat &I, std::vector<ConnectedComponentStats> &statsv, int connectivity){
|
int connectedComponentsWithStats(InputArray I, OutputArray L, OutputArray statsv, int connectivity){
|
||||||
int lDepth = L.depth();
|
int lDepth = L.depth();
|
||||||
if(lDepth == CV_8U){
|
if(lDepth == CV_8U){
|
||||||
connectedcomponents::CCStatsOp<uint8_t> sop(statsv); return connectedComponents_sub1(L, I, connectivity, sop);
|
connectedcomponents::CCStatsOp<uint8_t> sop(statsv); return connectedComponents_sub1(I, L, connectivity, sop);
|
||||||
}else if(lDepth == CV_16U){
|
}else if(lDepth == CV_16U){
|
||||||
connectedcomponents::CCStatsOp<uint16_t> sop(statsv); return connectedComponents_sub1(L, I, connectivity, sop);
|
connectedcomponents::CCStatsOp<uint16_t> sop(statsv); return connectedComponents_sub1(I, L, connectivity, sop);
|
||||||
}else if(lDepth == CV_32S){
|
}else if(lDepth == CV_32S){
|
||||||
connectedcomponents::CCStatsOp<uint32_t> sop(statsv); return connectedComponents_sub1(L, I, connectivity, sop);
|
connectedcomponents::CCStatsOp<uint32_t> sop(statsv); return connectedComponents_sub1(I, L, connectivity, sop);
|
||||||
}else{
|
}else{
|
||||||
CV_Assert(false);
|
CV_Assert(false);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -123,7 +123,6 @@ typedef Ptr<FeatureDetector> Ptr_FeatureDetector;
|
|||||||
typedef Ptr<DescriptorExtractor> Ptr_DescriptorExtractor;
|
typedef Ptr<DescriptorExtractor> Ptr_DescriptorExtractor;
|
||||||
typedef Ptr<Feature2D> Ptr_Feature2D;
|
typedef Ptr<Feature2D> Ptr_Feature2D;
|
||||||
typedef Ptr<DescriptorMatcher> Ptr_DescriptorMatcher;
|
typedef Ptr<DescriptorMatcher> Ptr_DescriptorMatcher;
|
||||||
typedef vector<ConnectedComponentStats> vector_ConnectedComponentStats;
|
|
||||||
|
|
||||||
typedef SimpleBlobDetector::Params SimpleBlobDetector_Params;
|
typedef SimpleBlobDetector::Params SimpleBlobDetector_Params;
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ static void on_trackbar(int, void*)
|
|||||||
{
|
{
|
||||||
Mat bw = threshval < 128 ? (img < threshval) : (img > threshval);
|
Mat bw = threshval < 128 ? (img < threshval) : (img > threshval);
|
||||||
Mat labelImage(img.size(), CV_32S);
|
Mat labelImage(img.size(), CV_32S);
|
||||||
uint64_t nLabels = connectedComponents(labelImage, bw, 8);
|
int nLabels = connectedComponents(bw, labelImage, 8);
|
||||||
Vec3b colors[nLabels];
|
Vec3b colors[nLabels];
|
||||||
colors[0] = Vec3b(0, 0, 0);//background
|
colors[0] = Vec3b(0, 0, 0);//background
|
||||||
for(int label = 1; label < nLabels; ++label){
|
for(int label = 1; label < nLabels; ++label){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user