use connected component for a Part of the image

asked 2017-07-26 10:11:26 -0600

louis89 gravatar image

updated 2017-07-26 10:13:28 -0600

I want to use connected component for object detection.I can use connect component on Full image.but I want to use connect component for a part of the image.for example the size of my image is 760*520 and I want to use connected component for A square with size(350,270,60,60).this is a part of my code: `

Mat image;
Mat stat, centroid;

 int threshval = 100;

 static void on_trackbar(int, void*){
  Mat bw = threshval < 128 ? (image < threshval) : (image > threshval);
  Mat labelImage(image.size(), CV_32S);
  int nLabels = connectedComponentsWithStats(bw, labelImage, stat, centroid, 8);
  std::vector<Vec3b> colors(nLabels);
  colors[0] = Vec3b(0, 0, 0);//background
  for (int label = 1; label < nLabels; ++label) {
     colors[label] = Vec3b((rand() & 255), (rand() & 255), (rand() & 255));
  at dst(image.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);}

Except use image(cv::Rect(350, 270, 60, 60)) Instead of image,do you have any idea to help me?I'm beginner in opencv and c++.thanks a lot...

edit retag flag offensive close merge delete