0

I want to use connected component algorithm for object detection.I can use this algorithm on full image but I want to implementation connected component for a part of image.for example the size of my image is 760*520 and I want to implementation this algorithm on 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...

Siegmeyer
  • 4,312
  • 6
  • 26
  • 43
louis89
  • 5
  • 8
  • 1
    Why do you not want to use a rectangle within `image`? That seems the logical way to go, what problem are you trying to fix? – beaker Jul 27 '17 at 14:26
  • I want to use this algorithm for extract object and draw a bounding box around object and return the coordinate of rectangle.if I want to draw rectangle around object in dst window,no problem but when I want to draw rectangle in original image,the rectangle with size(350,270,60,60) is also a connected component and return it position for I.when I use a condition(for example draw rectangle around object with size <50)The speed of the algorithm goes down.for this reason I want to use another way to do this.I hope I could correctly convey the concept – louis89 Jul 27 '17 at 15:27
  • I'm still a bit confused about the part where you say "when I use a condition...". Are you saying that when you take a sub-rectangle of the original image (let's call it ROI), that detecting connected components within this rectangle is slower than if you use the whole image? It seems that it should be faster. If you're only having a problem with the coordinates, you can add the start coordinates of ROI to the start coordinates of the bounding box to get the correct image coordinates. – beaker Jul 27 '17 at 15:49

0 Answers0