Składowe spójne

Znajdowanie składowych spójnych za pomocą wyznaczania krawędzi.

 

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h> 

using namespace std;
using namespace cv;


int main(){

  Mat img = imread("./circles.png", 1 );
  
  cvtColor(img, img, CV_BGR2GRAY);
  threshold(img, img, 100, 255,THRESH_BINARY);
  Mat contours_img = img.clone();
  vector<vector <Point> > contours;
  vector<Vec4i> hierarchy;

  findContours(img, contours, hierarchy, 
                CV_RETR_TREE, CV_CHAIN_APPROX_NONE );

  
  imshow( "Contours", contours_img );

  for (int contour=0; (contour < contours.size()); contour++){
    Scalar colour( rand()&0xFF,rand()&0xFF,rand()&0xFF );
    drawContours( contours_img, contours, contour, colour, CV_FILLED, 8, hierarchy );
  }

  imshow( "Component", contours_img);

  waitKey(0);
  return 0;  
}