OpenCV 5.0.0-pre
Open Source Computer Vision
Loading...
Searching...
No Matches
samples/cpp/geometry.cpp

An example program illustrates the use of cv::convexHull, cv::fitEllipse, cv::minEnclosingTriangle, cv::minEnclosingCircle and cv::minAreaRect.

/*******************************************************************************
*
* This program demonstrates various shape fitting techniques using OpenCV.
* It reads an image, applies binary thresholding, and then detects contours.
*
* For each contour, it fits and draws several geometric shapes including
* convex hulls, minimum enclosing circles, rectangles, triangles, and ellipses
* using different fitting methods:
* 1: OpenCV's original method fitEllipse which implements Fitzgibbon 1995 method.
* 2: The Approximate Mean Square (AMS) method fitEllipseAMS proposed by Taubin 1991
* 3: The Direct least square (Direct) method fitEllipseDirect proposed by Fitzgibbon1999
*
* The results are displayed with unique colors
* for each shape and fitting method for clear differentiation.
*
*
*********************************************************************************/
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;
const string hot_keys =
"\n\nHot keys: \n"
"\tESC - quit the program\n"
"\tq - quit the program\n"
"\tc - make the circle\n"
"\tr - make the rectangle\n"
"\th - make the convexhull\n"
"\tt - make the triangle\n"
"\te - make the ellipse\n"
"\ta - make all shapes\n"
"\t0 - use OpenCV's method for ellipse fitting\n"
"\t1 - use Approximate Mean Square (AMS) method for ellipse fitting \n"
"\t2 - use Direct least square (Direct) method for ellipse fitting\n";
static void help(char** argv)
{
cout << "\nThis program demonstrates various shape fitting techniques on a set of points using functions: \n"
<< "minAreaRect(), minEnclosingTriangle(), minEnclosingCircle(), convexHull(), and ellipse().\n\n"
<< "Usage: " << argv[0] << " [--image_name=<image_path> Default: ellipses.jpg]\n\n";
cout << hot_keys << endl;
}
void processImage(int, void*);
void drawShapes(Mat &img, const vector<Point> &points, int ellipseMethod, string shape);
void drawConvexHull(Mat &img, const vector<Point> &points);
void drawMinAreaRect(Mat &img, const vector<Point> &points);
void drawFittedEllipses(Mat &img, const vector<Point> &points, int ellipseMethod);
void drawMinEnclosingCircle(Mat &img, const vector<Point> &points);
void drawMinEnclosingTriangle(Mat &img, const vector<Point> &points);
// Shape fitting options
Mat image;
enum EllipseFittingMethod {
OpenCV_Method,
AMS_Method,
Direct_Method
};
struct UserData {
int sliderPos = 70;
string shape = "--all";
int ellipseMethod = OpenCV_Method;
};
const char* keys =
"{help h | | Show help message }"
"{@image |ellipses.jpg| Path to input image file }";
int main(int argc, char** argv) {
cv::CommandLineParser parser(argc, argv, keys);
help(argv);
if (parser.has("help"))
{
return 0;
}
UserData userData; // variable to pass all the necessary values to trackbar callback
string filename = parser.get<string>("@image");
image = imread(samples::findFile(filename), IMREAD_COLOR); // Read the image from the specified path
if (image.empty()) {
cout << "Could not open or find the image" << endl;
return -1;
}
namedWindow("Shapes", WINDOW_AUTOSIZE); // Create a window to display the results
createTrackbar("Threshold", "Shapes", NULL, 255, processImage, &userData); // Create a threshold trackbar
setTrackbarPos("Threshold", "Shapes", userData.sliderPos);
for(;;) {
char key = (char)waitKey(0); // Listen for a key press
if (key == 'q' || key == 27) break; // Exit the loop if 'q' or ESC is pressed
switch (key) {
case 'h': userData.shape = "--convexhull"; break;
case 'a': userData.shape = "--all"; break;
case 't': userData.shape = "--triangle"; break;
case 'c': userData.shape = "--circle"; break;
case 'e': userData.shape = "--ellipse"; break;
case 'r': userData.shape = "--rectangle"; break;
case '0': userData.ellipseMethod = OpenCV_Method; break;
case '1': userData.ellipseMethod = AMS_Method; break;
case '2': userData.ellipseMethod = Direct_Method; break;
default: break; // Do nothing for other keys
}
processImage(userData.sliderPos, &userData); // Process the image with the current settings
}
return 0;
}
// Function to draw the minimum enclosing circle around given points
void drawMinEnclosingCircle(Mat &img, const vector<Point> &points) {
Point2f center;
float radius = 0;
minEnclosingCircle(points, center, radius); // Find the enclosing circle
// Draw the circle
circle(img, center, cvRound(radius), Scalar(0, 0, 255), 2, LINE_AA);
}
// Function to draw the minimum enclosing triangle around given points
void drawMinEnclosingTriangle(Mat &img, const vector<Point> &points) {
vector<Point> triangle;
minEnclosingTriangle(points, triangle); // Find the enclosing triangle
if (triangle.size() != 3) {
return;
}
// Use polylines to draw the triangle. The 'true' argument closes the triangle.
polylines(img, triangle, true, Scalar(255, 0, 0), 2, LINE_AA);
}
// Function to draw the minimum area rectangle around given points
void drawMinAreaRect(Mat &img, const vector<Point> &points) {
RotatedRect box = minAreaRect(points); // Find the minimum area rectangle
Point2f vtx[4];
box.points(vtx);
// Convert Point2f to Point because polylines expects a vector of Point
vector<Point> rectPoints;
for (int i = 0; i < 4; i++) {
rectPoints.push_back(Point(cvRound(vtx[i].x), cvRound(vtx[i].y)));
}
// Use polylines to draw the rectangle. The 'true' argument closes the loop, drawing a rectangle.
polylines(img, rectPoints, true, Scalar(0, 255, 0), 2, LINE_AA);
}
// Function to draw the convex hull of given points
void drawConvexHull(Mat &img, const vector<Point> &points) {
vector<Point> hull;
convexHull(points, hull, false); // Find the convex hull
// Draw the convex hull
polylines(img, hull, true, Scalar(255, 255, 0), 2, LINE_AA);
}
inline static bool isGoodBox(const RotatedRect& box) {
//size.height >= size.width awalys,only if the pts are on a line or at the same point,size.width=0
return (box.size.height <= box.size.width * 30) && (box.size.width > 0);
}
// Function to draw fitted ellipses using different methods
void drawFittedEllipses(Mat &img, const vector<Point> &points, int ellipseMethod) {
switch (ellipseMethod) {
case OpenCV_Method: // Standard ellipse fitting
{
RotatedRect fittedEllipse = fitEllipse(points);
if (isGoodBox(fittedEllipse)) {
ellipse(img, fittedEllipse, Scalar(255, 0, 255), 2, LINE_AA);
}
putText(img, "OpenCV", Point(img.cols - 80, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 0, 255), 2, LINE_AA);
}
break;
case AMS_Method: // AMS ellipse fitting
{
RotatedRect fittedEllipseAMS = fitEllipseAMS(points);
if (isGoodBox(fittedEllipseAMS)) {
ellipse(img, fittedEllipseAMS, Scalar(255, 0, 255), 2, LINE_AA);
}
putText(img, "AMS", Point(img.cols - 80, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 0, 255), 2, LINE_AA);
}
break;
case Direct_Method: // Direct ellipse fitting
{
RotatedRect fittedEllipseDirect = fitEllipseDirect(points);
if (isGoodBox(fittedEllipseDirect)) {
ellipse(img, fittedEllipseDirect, Scalar(255, 0, 255), 2, LINE_AA);
}
putText(img, "Direct", Point(img.cols - 80, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 0, 255), 2, LINE_AA);
}
break;
default: // Default case falls back to OpenCV method
{
RotatedRect fittedEllipse = fitEllipse(points);
if (isGoodBox(fittedEllipse)) {
ellipse(img, fittedEllipse, Scalar(255, 0, 255), 2, LINE_AA);
}
putText(img, "OpenCV (default)", Point(img.cols - 80, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 0, 255), 2, LINE_AA);
cout << "Warning: Invalid ellipseMethod value. Falling back to default OpenCV method." << endl;
}
break;
}
}
// Function to draw shapes
void drawShapes(Mat &img, const vector<Point> &points, int ellipseMethod, string shape) {
if (shape == "--circle") {
drawMinEnclosingCircle(img, points);
} else if (shape == "--triangle") {
drawMinEnclosingTriangle(img, points);
} else if (shape == "--rectangle") {
drawMinAreaRect(img, points);
} else if (shape == "--convexhull") {
drawConvexHull(img, points);
} else if (shape == "--ellipse"){
drawFittedEllipses(img, points, ellipseMethod);
}
else if (shape == "--all") {
drawMinEnclosingCircle(img, points);
drawMinEnclosingTriangle(img, points);
drawMinAreaRect(img, points);
drawConvexHull(img, points);
drawFittedEllipses(img, points, ellipseMethod);
}
}
// Main function to process the image based on the current trackbar position
void processImage(int position, void* userData){
UserData* data = static_cast<UserData*>(userData);
data->sliderPos = position;
Mat processedImg = image.clone(); // Clone the original image for processing
Mat gray;
cvtColor(processedImg, gray, COLOR_BGR2GRAY); // Convert to grayscale
threshold(gray, gray, data->sliderPos, 255, THRESH_BINARY); // Apply binary threshold
Mat filteredImg;
medianBlur(gray, filteredImg, 3);
vector<vector<Point>> contours;
findContours(filteredImg, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); // Find contours
if (contours.empty()) {
return;
}
imshow("Mask", filteredImg); // Show the mask
for (size_t i = 0; i < contours.size(); ++i) {
if (contours[i].size() < 5) { // Check if the contour has enough points
continue;
}
drawShapes(processedImg, contours[i], data->ellipseMethod, data->shape);
}
imshow("Shapes", processedImg); // Display the processed image with fitted shapes
}
Designed for command line parsing.
Definition utility.hpp:890
n-dimensional dense array class
Definition mat.hpp:951
int cols
Definition mat.hpp:2425
The class represents rotated (i.e. not up-right) rectangles on a plane.
Definition types.hpp:541
Size2f size
returns width and height of the rectangle
Definition types.hpp:575
void points(Point2f pts[]) const
_Tp height
the height
Definition types.hpp:366
_Tp width
the width
Definition types.hpp:365
int cvRound(double value)
Rounds floating-point number to the nearest integer.
Definition fast_math.hpp:200
@ triangle
Definition gr_skig.hpp:63
@ circle
Definition gr_skig.hpp:62
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
void setTrackbarPos(const String &trackbarname, const String &winname, int pos)
Sets the trackbar position.
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
Creates a trackbar and attaches it to the specified window.
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
Converts an image from one color space to another.
void ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a simple or thick elliptic arc or fills an ellipse sector.
void putText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
Draws a text string.
void polylines(InputOutputArray img, InputArrayOfArrays pts, bool isClosed, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws several polygonal curves.
void medianBlur(InputArray src, OutputArray dst, int ksize)
Blurs an image using the median filter.
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
Applies a fixed-level threshold to each array element.
void convexHull(InputArray points, OutputArray hull, bool clockwise=false, bool returnPoints=true)
Finds the convex hull of a point set.
double minEnclosingTriangle(InputArray points, OutputArray triangle)
Finds a triangle of minimum area enclosing a 2D point set and returns its area.
RotatedRect minAreaRect(InputArray points)
Finds a rotated rectangle of the minimum area enclosing the input 2D point set.
RotatedRect fitEllipseDirect(InputArray points)
Fits an ellipse around a set of 2D points.
RotatedRect fitEllipseAMS(InputArray points)
Fits an ellipse around a set of 2D points.
void minEnclosingCircle(InputArray points, Point2f &center, float &radius)
Finds a circle of the minimum area enclosing a 2D point set.
void findContours(InputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())
Finds contours in a binary image.
RotatedRect fitEllipse(InputArray points)
Fits an ellipse around a set of 2D points.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
GOpaque< Size > size(const GMat &src)
Gets dimensions from Mat.
Definition core.hpp:107
STL namespace.