在额外图像中提取boundingRect

时间:2017-06-15 11:30:29

标签: c++ opencv extract rectangles

我想将boundingRect()的{​​strong>全部提取到单张图片中。使用该代码,我已经在一个图像中获得了所有boundingRect:

//...
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);
        }
    }

我需要遍历我的所有boundingRect()中的每一个应该获得额外的输出。已经查看了a python code and "translated" ittried this one

提前谢谢!请分享我的代码的建议改进。

感谢 @RickM。我试了一下(整个更新的代码):

//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);

}
}

部分工作 - 我只获得boundingRect()的一张(最后一张)图片,为了保存所有这些图片,它只保存最后一张图片。我该如何解决?

0 个答案:

没有答案