Commit 45b4f4f3 authored by Jason Newton's avatar Jason Newton

connectedComponents: warning free version

parent 4d059e9e
...@@ -1091,6 +1091,11 @@ enum { TM_SQDIFF=0, TM_SQDIFF_NORMED=1, TM_CCORR=2, TM_CCORR_NORMED=3, TM_CCOEFF ...@@ -1091,6 +1091,11 @@ 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 );
//! 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.
CV_EXPORTS_W uint64_t connectedComponents(Mat &L, const Mat &I, int connectivity = 8);
//! mode of the contour retrieval algorithm //! mode of the contour retrieval algorithm
enum enum
{ {
......
This diff is collapsed.
...@@ -11,25 +11,21 @@ int threshval = 100; ...@@ -11,25 +11,21 @@ int threshval = 100;
static void on_trackbar(int, void*) 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);
vector<vector<Point> > contours; int nLabels = connectedComponents(labelImage, bw, 8);
vector<Vec4i> hierarchy; Vec3b colors[nLabels];
colors[0] = Vec3b(0, 0, 0);//background
findContours( bw, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); for(int label = 1; label < nLabels; ++label){
colors[label] = Vec3b( (rand()&255), (rand()&255), (rand()&255) );
Mat dst = Mat::zeros(img.size(), CV_8UC3);
if( !contours.empty() && !hierarchy.empty() )
{
// iterate through all the top-level contours,
// draw each connected component with its own random color
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
{
Scalar color( (rand()&255), (rand()&255), (rand()&255) );
drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
}
} }
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];
}
}
imshow( "Connected Components", dst ); imshow( "Connected Components", dst );
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment