#include <iostream>
#include <vector>
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);
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) {
help(argv);
if (parser.has("help"))
{
return 0;
}
UserData userData;
string filename = parser.get<string>("@image");
image =
imread(samples::findFile(filename), IMREAD_COLOR);
if (image.empty()) {
cout << "Could not open or find the image" << endl;
return -1;
}
createTrackbar(
"Threshold",
"Shapes", NULL, 255, processImage, &userData);
for(;;) {
if (key == 'q' || key == 27) break;
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;
}
processImage(userData.sliderPos, &userData);
}
return 0;
}
void drawMinEnclosingCircle(
Mat &img,
const vector<Point> &points) {
float radius = 0;
}
void drawMinEnclosingTriangle(
Mat &img,
const vector<Point> &points) {
return;
}
}
void drawMinAreaRect(
Mat &img,
const vector<Point> &points) {
vector<Point> rectPoints;
for (int i = 0; i < 4; i++) {
}
}
void drawConvexHull(
Mat &img,
const vector<Point> &points) {
vector<Point> hull;
}
}
void drawFittedEllipses(
Mat &img,
const vector<Point> &points,
int ellipseMethod) {
switch (ellipseMethod) {
case OpenCV_Method:
{
if (isGoodBox(fittedEllipse)) {
}
putText(img,
"OpenCV",
Point(img.
cols - 80, 20), FONT_HERSHEY_SIMPLEX, 0.5,
Scalar(255, 0, 255), 2, LINE_AA);
}
break;
case AMS_Method:
{
if (isGoodBox(fittedEllipseAMS)) {
}
putText(img,
"AMS",
Point(img.
cols - 80, 20), FONT_HERSHEY_SIMPLEX, 0.5,
Scalar(255, 0, 255), 2, LINE_AA);
}
break;
case Direct_Method:
{
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:
{
if (isGoodBox(fittedEllipse)) {
}
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;
}
}
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);
}
}
void processImage(int position, void* userData){
UserData* data = static_cast<UserData*>(userData);
data->sliderPos = position;
Mat processedImg = image.clone();
cvtColor(processedImg, gray, COLOR_BGR2GRAY);
threshold(gray, gray, data->sliderPos, 255, THRESH_BINARY);
vector<vector<Point>> contours;
findContours(filteredImg, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
if (contours.empty()) {
return;
}
for (size_t i = 0; i < contours.size(); ++i) {
if (contours[i].
size() < 5) {
continue;
}
drawShapes(processedImg, contours[i], data->ellipseMethod, data->shape);
}
imshow(
"Shapes", processedImg);
}
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 ¢er, 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.