OpenCV Tutorials —— Creating Bounding rotated boxes and ellipses for contours

外接旋转矩形 ,或外接椭圆

 

RotatedRect minAreaRect(InputArray points)

返回面积最小的外接矩形

RotatedRect fitEllipse(InputArray points)

The function calculates the ellipse that fits (in a least-squares sense) a set of 2D points best of all. It returns the rotated rectangle in which the ellipse is inscribed.

 

Code

#include "stdafx.h"

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

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

/// Function header
void thresh_callback(int, void* );

/** @function main */
int main( int argc, char** argv )
{
	/// Load source image and convert it to gray
	src = imread( "img2.jpg", 1 );

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

	createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
	thresh_callback( 0, 0 );

	waitKey(0);
	return(0);
}

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

	/// Find the rotated rectangles and ellipses for each contour
	vector<RotatedRect> minRect( contours.size() );
	vector<RotatedRect> minEllipse( contours.size() );

	for( int i = 0; i < contours.size(); i++ )
	{
		minRect[i] = minAreaRect( Mat(contours[i]) );
		if( contours[i].size() > 5 )
			{ minEllipse[i] = fitEllipse( Mat(contours[i]) ); }
	}

	/// Draw contours + rotated rects + ellipses
	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) );
		// contour
		drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
		// ellipse
		ellipse( drawing, minEllipse[i], color, 2, 8 );
		// rotated rectangle
		Point2f rect_points[4]; minRect[i].points( rect_points );
		for( int j = 0; j < 4; j++ )
			line( drawing, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );
	}

	/// Show in a window
	namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
	imshow( "Contours", drawing );
}
时间: 2024-10-22 02:54:48

OpenCV Tutorials —— Creating Bounding rotated boxes and ellipses for contours的相关文章

OpenCV Tutorials &mdash;&mdash; Creating Bounding boxes and circles for contours

同样是提取出轮廓之后的处理 ~~   void approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed) Parameters: curve – Input vector of a 2D point stored in: std::vector or Mat (C++ interface) Nx2 numpy array (Python interface) CvSeq or CvMa

OpenCV Tutorials &mdash;&mdash; Creating a video with OpenCV

写video 需要用到 VideoWriter  视频文件可看作一个容器 视频的类型由视频文件的后缀名来指定   Due to this OpenCV for video containers supports only the avi extension, its first version. A direct limitation of this is that you cannot save a video file larger than 2 GB. Furthermore you ca

OpenCV Tutorials &mdash;&mdash; Creating yor own corner detector

Use the OpenCV function cornerEigenValsAndVecs to find the eigenvalues and eigenvectors to determine if a pixel is a corner. Use the OpenCV function cornerMinEigenVal to find the minimum eigenvalues for corner detection.   最小特征值对应的角点监测 ~~ 对自相关矩阵 M 进行

OpenCV Tutorials —— Creating Widgets

Explanation Extend Widget3D class to create a new 3D widget. Assign a VTK actor to the widget. Set color of the widget. Construct a triangle widget and display it in the window. Code #include <opencv2/viz/vizcore.hpp> #include <opencv2/viz/widget

学习opencv tutorials

1.opencv里头动态库和静态库的区别 lib是动态库,staticlib是静态库. 这是opencv tutorials中对动态库和静态库的说明.动态库是在runtime时候才load的库文件.而静态库文件会在你build的时候build-in inside your exe file.优点是可以避免误删,缺点是应用程序变大,加载时间也会变长. 2.  Visual Studio中solution和project的关系 在VS中,一个solution中可以包含多个project. 3.  两

OpenCV Tutorials &mdash;&mdash; Basic Drawing

Point It represents a 2D point, specified by its image coordinates and . We can define it as: Point pt;pt.x = 10;pt.y = 8; or Point pt = Point(10, 8); Scalar Represents a 4-element vector. The type Scalar is widely used in OpenCV for passing pixel va

OpenCV Tutorials &mdash;&mdash; Random generator and text with OpenCV

creating a Random Number Generator object (RNG): RNG rng( 0xFFFFFFFF ); 创建并初始化随机数生成子 create a matrix initialized to zeros (which means that it will appear as black), specifying its height, width and its type: /// Initialize a matrix filled with zeros

OpenCV Tutorials &mdash;&mdash; Mat

if you pass on an already existing Mat object, which has already allocated the required space for the matrix, this will be reused. The idea is that each Mat object has its own header, however the matrix may be shared between two instance of them by h

OpenCV Tutorials &mdash;&mdash; Hough Line Transform

霍夫直线变换 -- 用于检测图像中的直线 利用图像空间和Hough参数空间的点--直线对偶性,把图像空间中的检测问题转换到参数空间,通过在参数空间进行简单的累加统计,然后在Hough参数空间中寻找累加器峰值的方法检测直线 Standard and Probabilistic Hough Line Transform OpenCV implements two kind of Hough Line Transforms: The Standard Hough Transform It consis