#include <iostream>
Mat img;
int threshval = 100;
static void on_trackbar(int, void*)
{
Mat bw = threshval < 128 ? (img < threshval) : (img > threshval);
Mat labelImage(img.size(),
CV_32S);
std::vector<Vec3b> colors(nLabels);
colors[0] =
Vec3b(0, 0, 0);
for(int label = 1; label < nLabels; ++label){
colors[label] =
Vec3b( (rand()&255), (rand()&255), (rand()&255) );
}
for(int r = 0; r < dst.rows; ++r){
for(int c = 0; c < dst.cols; ++c){
int label = labelImage.at<int>(r, c);
pixel = colors[label];
}
}
imshow(
"Connected Components", dst );
}
int main(
int argc,
const char** argv )
{
CommandLineParser parser(argc, argv, "{@image|stuff.jpg|image for converting to a grayscale}");
parser.about("\nThis program demonstrates connected components and use of the trackbar\n");
parser.printMessage();
cout << "\nThe image is converted to grayscale and displayed, another image has a trackbar\n"
"that controls thresholding and thereby the extracted contours which are drawn in color\n";
String inputImage = parser.get<
string>(0);
if(img.empty())
{
cout << "Could not read input image file: " << inputImage << endl;
return EXIT_FAILURE;
}
createTrackbar(
"Threshold",
"Connected Components", &threshval, 255, on_trackbar );
on_trackbar(threshval, 0);
return EXIT_SUCCESS;
}
std::string String
Definition: cvstd.hpp:149
Vec< uchar, 3 > Vec3b
Definition: matx.hpp:441
#define CV_32S
Definition: core/include/opencv2/core/hal/interface.h:77
#define CV_8UC3
Definition: core/include/opencv2/core/hal/interface.h:90
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
@ WINDOW_AUTOSIZE
the user cannot resize the window, the size is constrainted by the image displayed.
Definition: highgui.hpp:144
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
Creates a trackbar and attaches it to the specified window.
@ IMREAD_GRAYSCALE
If set, always convert image to the single channel grayscale image (codec internal conversion).
Definition: imgcodecs.hpp:70
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
int connectedComponents(InputArray image, OutputArray labels, int connectivity, int ltype, int ccltype)
computes the connected components labeled image of boolean image
int main(int argc, char *argv[])
Definition: highgui_qt.cpp:3
Definition: core/include/opencv2/core.hpp:107