c++ - 创建轮廓的边界框

标签 c++ opencv image-processing computer-vision opencv3.1

我用这段代码来绘制“边界框”。现在我想要最大的矩形,而有很多矩形。找到最大的矩形后,我想要这个矩形的坐标。通过这些坐标,我想成为原始图像的一部分。最大的矩形是我的车。

#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <iostream>
#include <sstream>
#include <fstream>
using namespace cv;
using namespace std;

Mat src; Mat src_gray;
int thresh = 0;
int max_thresh = 255;
RNG rng(12345);

/// Function header
void thresh_callback(int, void*);
/** @function thresh_callback */
void thresh_callback(int, void*)
{
    Mat threshold_output;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    /// Detect edges using Threshold
    threshold(src_gray, threshold_output, thresh, 255, THRESH_BINARY);
    /// Find contours
    findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    /// Approximate contours to polygons + get bounding rects and circles
    vector<vector<Point> > contours_poly(contours.size());
    vector<Rect> boundRect(contours.size());
    vector<Point2f>center(contours.size());
    vector<float>radius(contours.size());

    for (int i = 0; i < contours.size(); i++)
    {
        approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
        boundRect[i] = boundingRect(Mat(contours_poly[i]));
        minEnclosingCircle((Mat)contours_poly[i], center[i], radius[i]);
    }


    /// Draw polygonal contour + bonding rects + circles
    Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3);
    for (int i = 0; i< contours.size(); i++)
    {
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        drawContours(drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point());
        rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);
    }

    /// Show in a window
    namedWindow("Contours", CV_WINDOW_AUTOSIZE);
    imshow("Contours", drawing);
}
/** @function main */
int main(int argc, char** argv)
{
    /// Load source image and convert it to gray
    src = imread("RGB2YCrCb_BC_SB.png");
    /// Convert image to gray and blur it
    cvtColor(src, src_gray, CV_BGR2GRAY);
    blur(src_gray, src_gray, Size(3, 3));
    /// Create Window
    char* source_window = "Source";
    namedWindow(source_window, CV_WINDOW_AUTOSIZE);
    imshow(source_window, src);


    thresh_callback(0, 0);

    waitKey(0);
    return(0);
}

我已经努力工作并且搜索了很多。如果可以的话请帮忙。 看图片。

输入图像:

结果:

最佳答案

来源:

enter image description here

找到近似多边形轮廓的最大面积边界矩形:

enter image description here

裁剪:

enter image description here


带注释的代码:

//! 2018.05.13 13:31:34 CST
//! Find the max-area bounding-rect of approxed-poly-contours

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int findMaxRect(){
    /// Read, cvtColor, threshold
    Mat img, gray, threshed;
    img = imread("car.jpg");
    cvtColor(img, gray, COLOR_BGR2GRAY);
    threshold(gray, threshed, 100, 255, THRESH_OTSU|THRESH_BINARY);

    /// Find contours
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours(threshed, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));

    ///  Find the max boundingRect of approxed-contours
    double max_area = -1;
    Rect max_rect;
    int idx = -1;
    vector<Point2f> contour_poly;

    for (int i = 0; i < contours.size(); i++){
        /// Approx the contour
        Mat contour = Mat(contours[i]);
        double arclen = arcLength(contour, true);
        approxPolyDP(contour, contour_poly, 0.03*arclen, true);

        /// Get the bounding box
        Rect rect =  boundingRect(Mat(contour_poly));

        /// Update the max_area_box
        double area = rect.width*rect.height;
        if(area>max_area){
            max_area = area;
            max_rect = rect;
        }
    }

    cout << max_rect<<endl;

    /// Draw the max rect
    Mat drawing = img.clone();
    rectangle(drawing, max_rect.tl(), max_rect.br(), Scalar(0,255,0), 1,8,0);
    /// Crop
    Mat dst = img(max_rect);
    /// Display
    imshow("drawing", drawing);
    imshow("dst", dst);
    waitKey(0);
    return 0;
}

int main(int argc, char** argv){
    //test_contours();
    findMaxRect() ;
}

关于c++ - 创建轮廓的边界框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50308055/

相关文章:

python - 创建自定义数据集

matlab - 如何检测图像中的斜线?

c++ - new运算符和new运算符,哪个不能重载?

c++ - DLL卸载程序

python - OpenCV错误-python中的Imread语句

python - 测量图像中线段的长度(以像素为单位)

python - SimpleITK 调整图像大小

c++ - 在 xCode 上安装 SDL2

c++ - 如何将此 C++ 代码转换为 Objective C

python - 改进图像分割以创建围绕对象的闭合轮廓