2013-03-14 17:10:54 +01:00
|
|
|
#include <opencv2/core/utility.hpp>
|
|
|
|
#include "opencv2/imgproc.hpp"
|
2014-07-04 16:48:15 +02:00
|
|
|
#include "opencv2/imgcodecs.hpp"
|
2013-03-14 17:10:54 +01:00
|
|
|
#include "opencv2/highgui.hpp"
|
2010-11-24 06:51:04 +01:00
|
|
|
#include <iostream>
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
using namespace cv;
|
2010-11-26 18:59:40 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2010-05-11 19:44:00 +02:00
|
|
|
Mat img;
|
|
|
|
int threshval = 100;
|
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
static void on_trackbar(int, void*)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2012-06-07 19:21:29 +02:00
|
|
|
Mat bw = threshval < 128 ? (img < threshval) : (img > threshval);
|
2012-08-25 08:57:17 +02:00
|
|
|
Mat labelImage(img.size(), CV_32S);
|
2012-11-27 11:25:52 +01:00
|
|
|
int nLabels = connectedComponents(bw, labelImage, 8);
|
2012-12-09 06:57:49 +01:00
|
|
|
std::vector<Vec3b> colors(nLabels);
|
2012-08-25 08:57:17 +02:00
|
|
|
colors[0] = Vec3b(0, 0, 0);//background
|
|
|
|
for(int label = 1; label < nLabels; ++label){
|
|
|
|
colors[label] = Vec3b( (rand()&255), (rand()&255), (rand()&255) );
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
2012-08-25 08:57:17 +02:00
|
|
|
Mat dst(img.size(), CV_8UC3);
|
|
|
|
for(int r = 0; r < dst.rows; ++r){
|
|
|
|
for(int c = 0; c < dst.cols; ++c){
|
|
|
|
int label = labelImage.at<int>(r, c);
|
|
|
|
Vec3b &pixel = dst.at<Vec3b>(r, c);
|
|
|
|
pixel = colors[label];
|
|
|
|
}
|
|
|
|
}
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
imshow( "Connected Components", dst );
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
static void help()
|
2011-08-10 13:29:32 +02:00
|
|
|
{
|
2011-08-10 13:49:10 +02:00
|
|
|
cout << "\n This program demonstrates connected components and use of the trackbar\n"
|
2012-06-07 19:21:29 +02:00
|
|
|
"Usage: \n"
|
2014-09-13 16:28:41 +02:00
|
|
|
" ./connected_components <image(../data/stuff.jpg as default)>\n"
|
2012-06-07 19:21:29 +02:00
|
|
|
"The image is converted to grayscale and displayed, another image has a trackbar\n"
|
2011-08-10 13:49:10 +02:00
|
|
|
"that controls thresholding and thereby the extracted contours which are drawn in color\n";
|
2011-08-10 13:29:32 +02:00
|
|
|
}
|
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
const char* keys =
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2014-09-13 16:28:41 +02:00
|
|
|
"{@image|../data/stuff.jpg|image for converting to a grayscale}"
|
2011-08-10 13:29:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int main( int argc, const char** argv )
|
|
|
|
{
|
2012-06-07 19:21:29 +02:00
|
|
|
help();
|
|
|
|
CommandLineParser parser(argc, argv, keys);
|
2013-01-31 09:08:43 +01:00
|
|
|
string inputImage = parser.get<string>(0);
|
2012-06-07 19:21:29 +02:00
|
|
|
img = imread(inputImage.c_str(), 0);
|
2011-08-10 13:29:32 +02:00
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
if(img.empty())
|
|
|
|
{
|
2011-08-10 13:49:10 +02:00
|
|
|
cout << "Could not read input image file: " << inputImage << endl;
|
2012-06-07 19:21:29 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
namedWindow( "Image", 1 );
|
2010-05-11 19:44:00 +02:00
|
|
|
imshow( "Image", img );
|
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
namedWindow( "Connected Components", 1 );
|
|
|
|
createTrackbar( "Threshold", "Connected Components", &threshval, 255, on_trackbar );
|
|
|
|
on_trackbar(threshval, 0);
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
waitKey(0);
|
|
|
|
return 0;
|
|
|
|
}
|