gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:20:39 33 4
gpt4 key购买 nike

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

#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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com