2

I would like to extract all of my boundingRect()s to single images. With that code I already get all boundingRect in one image:

//...
for (size_t i = 0; i < lines.size(); i++) {
        Vec4i current = lines[i];
        Point pt1 = Point(current[0], current[1]);
        Point pt2 = Point(current[2], current[3]);

        Vec4i previous = lines[i - 1];
        Point ppt1 = Point(previous[0], previous[1]);
        Point ppt2 = Point(previous[2], previous[3]);

        double angle = atan2(pt2.y - pt1.y, pt2.x - pt1.x) * 180.0 / CV_PI;
        if (angle) {
            line(cdst, pt1, pt2, Scalar(0, 0, 255), 2, CV_AA);
        }

        vector<Point> pt;
        vector<Mat> subregions;
        pt.push_back(Point(current[0], current[1]));
        pt.push_back(Point(current[2], current[3]));
        pt.push_back(Point(previous[0], previous[1]));
        pt.push_back(Point(previous[2], previous[3]));


        Rect boundRect = boundingRect(pt);
    rectangle(src, boundRect, Scalar(0, 255, 0), 1, 8, 0);
    imshow("boundings", src);
        }
    }

I need to iterate through all of my boundingRect() and for each of them should be given a extra output. Already looked at a python code and "translated" it and also tried this one

Thank you in advance! Please share suggested improvements of my code.

Thanks to @RickM. I tried something (entire updated code):

//HoughLinesP...    
   for (size_t i = 1; i < lines.size(); i++) {
   //current lines
    Vec4i current = lines[i];
    Point pt1 = Point(current[0], current[1]);
    Point pt2 = Point(current[2], current[3]);

    //previous lines
    Vec4i previous = lines[i - 1];
    Point ppt1 = Point(previous[0], previous[1]);
    Point ppt2 = Point(previous[2], previous[3]);

    double angle = atan2(pt2.y - pt1.y, pt2.x - pt1.x) * 180.0 / CV_PI;
    if (angle) {
        line(cdst, pt1, pt2, Scalar(0, 0, 255), 2, CV_AA);
    }

    vector<Point> pt;
    vector<Mat> subregions;

    pt.push_back(Point(current[0], current[1]));
    pt.push_back(Point(current[2], current[3]));
    pt.push_back(Point(previous[0], previous[1]));
    pt.push_back(Point(previous[2], previous[3]));

    Rect roi = boundingRect(pt);
    Mat contourRegion = src2(roi);
    subregions.push_back(contourRegion);

    if (contourRegion.cols >= 50) {
        cout << i << ": " << roi << endl;
        stringstream ss;
        string name = "wallRegion_";
        string type = ".jpg";
        int ct = 0;
        ss << name << (ct + 1) << type;

    imwrite("here/is/my/path/" + name + type, contourRegion);
    //imshow("", contourRegion);

}
}

It works partly - I'll get only one (the last one) picture of the boundingRect() and for the purpose to save all of them, it saves only the last one. How can I fix it?

Viktoria
  • 533
  • 2
  • 7
  • 24
  • Which part is false from `(npoints >= 0 && (depth == CV_32F || depth == CV_32S)` ? – Jarod42 Jun 15 '17 at 11:33
  • Can you tell us where exactly do you get this error? – Rick M. Jun 15 '17 at 11:35
  • It only tells me: `Exception in save location 0x0000006CE9AEED50 ` – Viktoria Jun 15 '17 at 11:35
  • And the error is in the for loop: `for (int i = 0; i < pt.size(); i++) { //...` – Viktoria Jun 15 '17 at 11:37
  • I would suggest you to debug your code and try and figure out the cause from there. Also according to the [documentation](https://github.com/opencv/opencv/blob/master/modules/imgproc/src/shapedescr.cpp), I don't know why you are passing a **single** point and trying to retrieve a _boundingRect_ from it. This will not work (at least mathematically) – Rick M. Jun 15 '17 at 12:15
  • edited my question, probably it should be clear now, what I want to achieve. – Viktoria Jun 15 '17 at 12:28
  • I don't think its just any edit, it looks more like a fix. And now you forgot to put what the problem exactly is. I guess what you are trying to do is get each of the bounding box to be drawn . – Rick M. Jun 15 '17 at 12:36
  • @RickM. as I already mentioned I would like to extract all of those boundingRects to single images. I tried it with a for loop but it doesn't work. So I just posted my original code, where all boundingRects are shown in one image. How can I extract those? – Viktoria Jun 15 '17 at 13:01
  • 1
    If you want all the boundingRects separately, you could initialize a container like `std::vector allRects` and use `allRects.pushback(boundRect)`. However, the `Rect` wouldn't be a Mat, so may be `Mat roi = Mat(src,rect);` and then instead make a `std::vector allRectMats` and pushback the `roi`. – Rick M. Jun 15 '17 at 13:07
  • @RickM. yes and no. I'll add the code, which I tried, to my question, so it's readable. What doen't still work? Actually it seems to work fine, but now I just get the "last" picture of boundingRect. The purpose is to save all, but for some reason I just get one as the output. Could you help me out? :) – Viktoria Jun 15 '17 at 13:55
  • You should put that in a loop, not the declaration of the vector though. Can you tell me how many bounding boxes you have and the size of your container? – Rick M. Jun 15 '17 at 14:01
  • @RickM. putting in a for loop doesn't make any diffrence. The size as well the quantity shoud be dynamically. I just sort out too small regions. – Viktoria Jun 15 '17 at 14:08
  • Can you update your code with the definition of the vector and where you call it? And I mean the **entire** portion. – Rick M. Jun 15 '17 at 14:09
  • As i told you before, define the `vector subregions;` out of the loop and then consider [Buying a suitable book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or learning a bit more programming. It will help you in the long run – Rick M. Jun 15 '17 at 14:14
  • @RickM. I updated my code. Now I was just testing the code so I put some of the variables inside the loop. To a later moment, it'll be cleaned up and actually doesn't cause the problem. – Viktoria Jun 15 '17 at 14:21
  • So you figured the problem out or do you still get only the last boundingRect? – Rick M. Jun 15 '17 at 14:22
  • @RickM. I still have the problem not outputing *all* of the boundingBoxes. – Viktoria Jun 15 '17 at 14:24
  • 1
    If you define `vector subregions;` inside the `for` loop, for every iteration it gets defined again so you will get only the last image (last iteration). Your entire updated code tells me that you don't need to define a `vector subregions;`, but rather just write the images. But if you give the same path to write the images, images will be overwritten in every iteration. With this you shouldn't expect to get more than one image. – Rick M. Jun 15 '17 at 14:30

0 Answers0