RotatedRect fitEllipse(InputArray points)
Finds a rotated rectangle of the minimum area enclosing the input 2D point set.
Fits an ellipse around a set of 2D points.
RotatedRect minAreaRect(InputArray points)Finds a rotated rectangle of the minimum area enclosing the input 2D point set.
vector<vector<Point>> contours; findContours( ..., contours, ...); // This will find out the contours and save in contours cout<<endl<<Mat(contours[index])<<endl; //index = 0,1,2,... used to print the format of contours ellipse = fitEllipse( Mat(contours[i]) ); // fitting ellipse in contour points Rect = minAreaRect( Mat(contours[i]) );
The code provided below is slight modification of code given in OpenCV documentation.
Steps:
- Load image
- Convert to gray-scale.
- Remove noise by blurring with a Gaussian filter
- Create new window
- Add trackbar to the window
- Detect Canny edges
- Find contours
- Find the rotated rectangles and ellipses for each contour
- Draw contours, rotated rectangles and ellipses
Functions:
minAreaRect, fitEllipse, createTrackbar, findContours,
Canny, blur, cvtColor, imshow, imread, waitKey.
Example:
-----------------#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include<iostream>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 73;
int points = 50;
int max_thresh = 255;
RNG rng(12345);
void thresh_callback(int, void*);
int main( )
{
/// Load source image and convert it to gray
src = imread( "1.jpg", 1 );
/// Convert image to gray and blur it
cvtColor( src, src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );
namedWindow( "Source", CV_WINDOW_AUTOSIZE ); imshow( "Source", src );
createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
namedWindow( "Source1", CV_WINDOW_AUTOSIZE ); imshow( "Source1", src );
createTrackbar( " Threshold:", "Source1", &points, max_thresh, thresh_callback );
thresh_callback(0,0);
waitKey(0);
return(0);
}
void thresh_callback( int, void*)
{
Mat threshold_output;
Mat edge;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Detect edges
Canny( src_gray, edge, thresh, 3*thresh, 3);
edge.convertTo(threshold_output, CV_8U);
/// 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() );
cout<<endl<<contours[1]<<endl;
cout<<endl<<Mat(contours[1])<<endl;
for( int i = 0; i < contours.size(); i++ )
{ minRect[i] = minAreaRect( Mat(contours[i]) );
if( contours[i].size() > points )
{ 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 );
}
-----------------
applets for java for beginners
ReplyDeleteThank You. You saved so much of my time.
ReplyDeleteعندما نتحدث عن شركة لتنظيف المنزل ، يجب أن نأخذ في الاعتبار أن الشركة تقوم بمهام متعددة داخل المنزل ، مثل: اعتني بكافة أعمال التنظيف الشامل سواء في شقتك أو في منزل مكون من عدة طوابق ، بما في ذلك جميع الغرف والحمامات والمطابخ والسلالم والجدران والأسقف والأثاث وما إلى ذلك ، ليس هذا فقط ولكن الشركة البولندية محترفة الأثاث والأرضيات والأجهزة والخزائن. ويتم كل هذا بمساعدة معدات محددة وأدوات التنظيف والتطهير لضمان التنظيف الفعال في وقت محدود.
ReplyDeleteشركة الندى للخدمات المنزلية
Excellent explanation of contour-based shape fitting in OpenCV. The step-by-step demonstration of using minAreaRect() and fitEllipse() provides a clear understanding of how geometric features can be extracted and visualized from detected contours. The inclusion of interactive trackbars and practical code examples makes this article especially valuable for developers working on object detection, shape analysis, and computer vision projects. A concise yet highly informative resource for both learning and implementation.
ReplyDeletecleanco
Steam Generation