Add Grana's connected components algorithm for 8-way connectivity. (#6823)
* Add Grana's connected components algorithm for 8-way connectivity. That algorithm is faster than Wu's one (currently implemented in opencv). For more details see https://github.com/prittt/YACCLAB. * New functions signature and distance transform compatibility * Add tests to imgproc/test/test_connectedcomponents.cpp * Change of test_connectedcomponents.cpp for c++98 support
This commit is contained in:
@@ -42,6 +42,7 @@
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
@@ -58,49 +59,81 @@ protected:
|
||||
CV_ConnectedComponentsTest::CV_ConnectedComponentsTest() {}
|
||||
CV_ConnectedComponentsTest::~CV_ConnectedComponentsTest() {}
|
||||
|
||||
// This function force a row major order for the labels
|
||||
void normalizeLabels(Mat1i& imgLabels, int iNumLabels) {
|
||||
vector<int> vecNewLabels(iNumLabels + 1, 0);
|
||||
int iMaxNewLabel = 0;
|
||||
|
||||
for (int r = 0; r<imgLabels.rows; ++r) {
|
||||
for (int c = 0; c<imgLabels.cols; ++c) {
|
||||
int iCurLabel = imgLabels(r, c);
|
||||
if (iCurLabel>0) {
|
||||
if (vecNewLabels[iCurLabel] == 0) {
|
||||
vecNewLabels[iCurLabel] = ++iMaxNewLabel;
|
||||
}
|
||||
imgLabels(r, c) = vecNewLabels[iCurLabel];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void CV_ConnectedComponentsTest::run( int /* start_from */)
|
||||
{
|
||||
|
||||
int ccltype[] = { cv::CCL_WU, cv::CCL_DEFAULT, cv::CCL_GRANA };
|
||||
|
||||
string exp_path = string(ts->get_data_path()) + "connectedcomponents/ccomp_exp.png";
|
||||
Mat exp = imread(exp_path, 0);
|
||||
Mat orig = imread(string(ts->get_data_path()) + "connectedcomponents/concentric_circles.png", 0);
|
||||
|
||||
if (orig.empty())
|
||||
{
|
||||
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA );
|
||||
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
|
||||
return;
|
||||
}
|
||||
|
||||
Mat bw = orig > 128;
|
||||
Mat labelImage;
|
||||
int nLabels = connectedComponents(bw, labelImage, 8, CV_32S);
|
||||
|
||||
for(int r = 0; r < labelImage.rows; ++r){
|
||||
for(int c = 0; c < labelImage.cols; ++c){
|
||||
int l = labelImage.at<int>(r, c);
|
||||
bool pass = l >= 0 && l <= nLabels;
|
||||
if(!pass){
|
||||
ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
|
||||
return;
|
||||
for (uint cclt = 0; cclt < sizeof(ccltype)/sizeof(int); ++cclt)
|
||||
{
|
||||
|
||||
Mat1i labelImage;
|
||||
int nLabels = connectedComponents(bw, labelImage, 8, CV_32S, ccltype[cclt]);
|
||||
|
||||
normalizeLabels(labelImage, nLabels);
|
||||
|
||||
// Validate test results
|
||||
for (int r = 0; r < labelImage.rows; ++r){
|
||||
for (int c = 0; c < labelImage.cols; ++c){
|
||||
int l = labelImage.at<int>(r, c);
|
||||
bool pass = l >= 0 && l <= nLabels;
|
||||
if (!pass){
|
||||
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (exp.empty() || orig.size() != exp.size())
|
||||
{
|
||||
imwrite(exp_path, labelImage);
|
||||
exp = labelImage;
|
||||
}
|
||||
|
||||
if (0 != cvtest::norm(labelImage > 0, exp > 0, NORM_INF))
|
||||
{
|
||||
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
|
||||
return;
|
||||
}
|
||||
if (nLabels != cvtest::norm(labelImage, NORM_INF) + 1)
|
||||
{
|
||||
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if( exp.empty() || orig.size() != exp.size() )
|
||||
{
|
||||
imwrite(exp_path, labelImage);
|
||||
exp = labelImage;
|
||||
}
|
||||
|
||||
if (0 != cvtest::norm(labelImage > 0, exp > 0, NORM_INF))
|
||||
{
|
||||
ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH );
|
||||
return;
|
||||
}
|
||||
if (nLabels != cvtest::norm(labelImage, NORM_INF)+1)
|
||||
{
|
||||
ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH );
|
||||
return;
|
||||
}
|
||||
ts->set_failed_test_info(cvtest::TS::OK);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user