Created
January 20, 2017 12:27
-
-
Save LaurentBerger/541592d4b921483dae722e24560a8667 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "opencv2/opencv.hpp" | |
#include <iostream> | |
using namespace cv; | |
using namespace std; | |
int main(int argc, char* argv[]) | |
{ | |
String folderName="f:/lib/opencv/samples/data"; | |
vector<String> fileName; | |
vector<String> algoName={"SAUF","BBDT(8)/SAUF(4)"}; | |
glob(folderName + "/*.png", fileName); | |
for (int algo = CCL_WU; algo <=CCL_GRANA; algo++) | |
{ | |
for (int connexity = 4; connexity <= 8; connexity += 4) | |
{ | |
for (int i = 0; i< fileName.size(); i++) | |
{ | |
cout <<"Started Test " << algoName[algo]<< " for connexity " << connexity << " " << fileName[i]<<"\n"; | |
Mat img = imread(fileName[i], IMREAD_GRAYSCALE); | |
Mat labelsForeground, labelsBackground, labelsBackForeground; | |
Mat imgThresh, imgThreshNot; | |
TickMeter chronocc; | |
double thresh = threshold(img, imgThresh, 50, 255, THRESH_OTSU); | |
chronocc.start(); | |
int nbLabelsForeGround = connectedComponents(imgThresh, labelsForeground, connexity, CV_32S, algo); | |
chronocc.stop(); | |
cout<<"time(cc)="<< chronocc.getTimeMilli() << "\t"; | |
chronocc.reset(); | |
bitwise_not(imgThresh, imgThreshNot); | |
chronocc.start(); | |
int nbLabelsBackGround = connectedComponents(imgThreshNot, labelsBackground, connexity, CV_32S, algo); | |
chronocc.stop(); | |
cout << "time(~cc)=" << chronocc.getTimeMilli() << "\t"; | |
chronocc.reset(); | |
chronocc.start(); | |
int nbLabels = connectedComponents(imgThresh, labelsBackForeground, connexity, CV_32S, CCL_FORE_BACK_GROUND + algo); | |
chronocc.stop(); | |
cout << "time(newcc)=" << chronocc.getTimeMilli() << "\t"; | |
chronocc.reset(); | |
int nbPixels = 0; | |
for (int j = 0; j < nbLabels; j++) | |
{ | |
Mat maskbg = (labelsBackForeground == j); | |
nbPixels += countNonZero(maskbg); | |
if (j < nbLabelsForeGround-1) | |
{ | |
Mat mask = labelsForeground == j + 1; | |
bitwise_xor(maskbg, mask, mask); | |
int n = countNonZero(mask); | |
if (n != 0) | |
cout << "Image " << fileName[i] << " : ERROR " << nbLabelsForeGround << " " << nbLabelsBackGround << " " << nbLabels << " " << j << "\n"; | |
} | |
else | |
{ | |
Mat mask = labelsBackground == j - nbLabelsForeGround + 2; | |
bitwise_xor(maskbg, mask, mask); | |
int n = countNonZero(mask); | |
if (n != 0) | |
cout << "Image " << fileName[i] << " : ERROR " << nbLabelsForeGround << " " << nbLabelsBackGround << " " << nbLabels << " " << j << "\n"; | |
} | |
} | |
if (nbPixels - imgThresh.rows*imgThresh.cols == 0) | |
cout << " No problem END \n"; | |
else | |
cout << " INCONSISTENCY DETECTED END \n"; | |
} | |
} | |
} | |
for (int algo = CCL_WU; algo <= CCL_GRANA; algo++) | |
{ | |
for (int connexity = 4; connexity <= 8; connexity += 4) | |
{ | |
for (int i = 0; i<fileName.size(); i++) | |
{ | |
cout << "Started Test " << algoName[algo] << " for connexity " << connexity << " " << fileName[i] << "\n"; | |
Mat img = imread(fileName[i], IMREAD_GRAYSCALE); | |
Mat labelsForeground, labelsBackground, labelsBackForeground; | |
Mat imgThresh, imgThreshNot; | |
TickMeter chronocc; | |
Mat statsvForeground, centroidForeground; | |
Mat statsvBackground, centroidBackground; | |
Mat statsvBackForeground, centroidBackForeground; | |
double thresh = threshold(img, imgThresh, 50, 255, THRESH_OTSU); | |
chronocc.start(); | |
int nbLabelsForeGround = connectedComponentsWithStats(imgThresh, labelsForeground,statsvForeground,centroidForeground, connexity, CV_32S, algo); | |
chronocc.stop(); | |
cout << "time(cc)=" << chronocc.getTimeMilli() << "\t"; | |
chronocc.reset(); | |
bitwise_not(imgThresh, imgThreshNot); | |
chronocc.start(); | |
int nbLabelsBackGround = connectedComponentsWithStats(imgThreshNot, labelsBackground, statsvBackground, centroidBackground, connexity, CV_32S, algo); | |
chronocc.stop(); | |
cout << "time(~cc)=" << chronocc.getTimeMilli() << "\t"; | |
chronocc.reset(); | |
chronocc.start(); | |
int nbLabels = connectedComponentsWithStats(imgThresh, labelsBackForeground,statsvBackForeground, centroidBackForeground, connexity, CV_32S, CCL_FORE_BACK_GROUND + algo); | |
chronocc.stop(); | |
cout << "time(newcc)=" << chronocc.getTimeMilli() << "\t"; | |
chronocc.reset(); | |
int nbErreur = 0; | |
int nbPixels = 0; | |
for (int j = 0; j < nbLabels; j++) | |
{ | |
nbPixels += statsvBackForeground.at<int>(j, CC_STAT_AREA); | |
if (j < nbLabelsForeGround-1) | |
{ | |
bool error=false; | |
Mat dst; | |
compare(centroidBackForeground.row(j), centroidForeground.row(j + 1), dst, CMP_EQ); | |
if (countNonZero(dst) != dst.cols) | |
error = true; | |
compare(statsvBackForeground.row(j), statsvForeground.row(j + 1), dst, CMP_EQ); | |
if (countNonZero(dst) != dst.cols) | |
error = true; | |
if (error) | |
{ | |
cout << "Image " << fileName[i] << " : ERROR BackForeground " << j << " != LabelsForeground" << j+1 << "\n"; | |
nbErreur++; | |
} | |
} | |
else | |
{ | |
bool error = false; | |
Mat dst; | |
compare(centroidBackForeground.row(j), centroidBackground.row(j - nbLabelsForeGround + 2), dst, CMP_EQ); | |
if (countNonZero(dst) != dst.cols) | |
error = true; | |
compare(statsvBackForeground.row(j), statsvBackground.row(j - nbLabelsForeGround + 2), dst, CMP_EQ); | |
if (countNonZero(dst) != dst.cols) | |
error = true; | |
if (error) | |
{ | |
cout << "Image " << fileName[i] << ": ERROR BackForeground " << j << " != LabelsBackGround " << j - nbLabelsForeGround + 1 << "\n"; | |
nbErreur++; | |
} | |
} | |
} | |
if (nbErreur == 0 && nbPixels== imgThresh.rows*imgThresh.cols) | |
cout << " No problem END \n"; | |
else | |
cout << nbPixels<<" "<< imgThresh.rows*imgThresh.cols<< " INCONSISTENCY DETECTED END \n"; | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment