Qt
    4
    Вклад в тег
    
      
      
    
  
  
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <algorithm>
int main(int argc, char *argv[])
{
	cv::Mat img,gr,th;
	img = cv::imread("1.jpg");
	cv::cvtColor(img, gr, cv::COLOR_BGR2GRAY);
	
	cv::threshold(gr, th, 240.0, 255, cv::THRESH_BINARY);
	std::vector<std::vector<cv::Point> > contours;
	cv::findContours(th, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
	auto max_vector_comp = [](std::vector<cv::Point> &lhs,
		std::vector<cv::Point> &rhs)
	{
		return lhs.size() < rhs.size();
	};
	//поиск самого длинного контура
	auto maxVector = std::max_element(contours.begin(), contours.end(),
		max_vector_comp);
	//находим описывающий прямоугольник
	cv::Rect roi = cv::boundingRect(*maxVector);
	cv::Mat croppedImg;
	croppedImg = img(roi);
	cv::imshow("inImg", img);
	cv::imshow("croppedImg", croppedImg);
	cv::waitKey();
	return 0;
}