#include <iostream>
class canvas{
public:
bool setupQ;
int minDims,maxDims;
int rows, cols;
void init(int minD, int maxD){
minDims = minD; maxDims = maxD;
rows = 0;
cols = 0;
setupQ = false;
}
if(setupQ){
if(corner.
x <
max.x){corner.
x = (int)(
max.x + 1.0);};
if(corner.
y <
max.y){corner.
y = (int)(
max.y + 1.0);};
if(origin.
x >
min.x){origin.
x = (int)
min.x;};
if(origin.
y >
min.y){origin.
y = (int)
min.y;};
} else {
}
int c = (int)(
scale*((corner.
x + 1.0) - origin.
x));
if(c<minDims){
} else {
if(c>maxDims){
}
}
int r = (int)(
scale*((corner.
y + 1.0) - origin.
y));
if(r<minDims){
} else {
if(r>maxDims){
}
}
cols = (int)(
scale*((corner.
x + 1.0) - origin.
x));
rows = (int)(
scale*((corner.
y + 1.0) - origin.
y));
setupQ = true;
}
void stretch(vector<Point2f> pts)
{
for(size_t i=1; i < pts.size(); i++){
if(
max.x < pnt.x){
max.x = pnt.x;};
if(
max.y < pnt.y){
max.y = pnt.y;};
if(
min.x > pnt.x){
min.x = pnt.x;};
if(
min.y > pnt.y){
min.y = pnt.y;};
};
}
{
for( int i = 0; i < 4; i++ ){
}
}
{
stretch(box);
}
for( int j = 0; j < 4; j++ ){
line(img, vtx[j], vtx[(j+1)%4], color, lineThickness,
LINE_AA);
}
}
void drawPoints(vector<Point2f> pts,
cv::Scalar color)
{
stretch(pts);
}
for(size_t i=0; i < pts.size(); i++){
};
}
void drawLabels( std::vector<std::string> text, std::vector<cv::Scalar> colors)
{
}
int vPos = 0;
for (size_t i=0; i < text.size(); i++) {
std::string txt = text[i];
vPos += (int)(1.3 * textsize.height);
Point org((img.
cols - textsize.width), vPos);
}
}
};
static void help(char** argv)
{
cout << "\nThis program is demonstration for ellipse fitting. The program finds\n"
"contours and approximate it by ellipses. Three methods are used to find the \n"
"elliptical fits: fitEllipse, fitEllipseAMS and fitEllipseDirect.\n"
"Call:\n"
<< argv[0] << " [image_name -- Default ellipses.jpg]\n" << endl;
}
int sliderPos = 70;
Mat image;
bool fitEllipseQ, fitEllipseAMSQ, fitEllipseDirectQ;
void processImage(int, void*);
int main(
int argc,
char** argv )
{
fitEllipseQ = true;
fitEllipseAMSQ = true;
fitEllipseDirectQ = true;
if (parser.has("help"))
{
help(argv);
return 0;
}
string filename = parser.get<string>("@image");
if( image.empty() )
{
cout << "Couldn't open image " << filename << "\n";
return 0;
}
createTrackbar(
"threshold",
"result", &sliderPos, 255, processImage );
processImage(0, 0);
return 0;
}
inline static bool isGoodBox(const RotatedRect& box) {
return (box.size.height <= box.size.width * 30) && (box.size.width > 0);
}
void processImage(int , void*)
{
RotatedRect box, boxAMS, boxDirect;
vector<vector<Point> > contours;
Mat bimage = image >= sliderPos;
canvas paper;
paper.init(
int(0.8*
MIN(bimage.rows, bimage.cols)),
int(1.2*
MAX(bimage.rows, bimage.cols)));
std::vector<std::string> text;
std::vector<cv::Scalar> color;
if (fitEllipseQ) {
text.push_back("OpenCV");
color.push_back(fitEllipseColor);
}
if (fitEllipseAMSQ) {
text.push_back("AMS");
color.push_back(fitEllipseAMSColor);
}
if (fitEllipseDirectQ) {
text.push_back("Direct");
color.push_back(fitEllipseDirectColor);
}
paper.drawLabels(text, color);
int margin = 2;
vector< vector<Point2f> > points;
for(size_t i = 0; i < contours.size(); i++)
{
size_t count = contours[i].size();
if( count < 6 )
continue;
Mat pointsf;
Mat(contours[i]).convertTo(pointsf,
CV_32F);
vector<Point2f>pts;
for (int j = 0; j < pointsf.rows; j++) {
Point2f pnt =
Point2f(pointsf.at<
float>(j,0), pointsf.at<
float>(j,1));
if ((pnt.x > margin && pnt.y > margin && pnt.x < bimage.cols-margin && pnt.y < bimage.rows-margin)) {
if(j%20==0){
pts.push_back(pnt);
}
}
}
points.push_back(pts);
}
for(size_t i = 0; i < points.size(); i++)
{
vector<Point2f> pts = points[i];
if (pts.size()<5) {
continue;
}
if (fitEllipseQ) {
if (isGoodBox(box)) {
paper.drawEllipseWithBox(box, fitEllipseColor, 3);
}
}
if (fitEllipseAMSQ) {
if (isGoodBox(boxAMS)) {
paper.drawEllipseWithBox(boxAMS, fitEllipseAMSColor, 2);
}
}
if (fitEllipseDirectQ) {
if (isGoodBox(boxDirect)){
paper.drawEllipseWithBox(boxDirect, fitEllipseDirectColor, 1);
}
}
paper.drawPoints(pts, fitEllipseTrueColor);
}
}
Designed for command line parsing.
Definition: utility.hpp:890
n-dimensional dense array class
Definition: core/include/opencv2/core/mat.hpp:829
static CV_NODISCARD_STD MatExpr zeros(int rows, int cols, int type)
Returns a zero array of the specified size and type.
_Tp & at(int i0=0)
Returns a reference to the specified array element.
int cols
Definition: core/include/opencv2/core/mat.hpp:2155
bool empty() const
Returns true if the array has no elements.
_Tp y
y coordinate of the point
Definition: modules/core/include/opencv2/core/types.hpp:202
_Tp x
x coordinate of the point
Definition: modules/core/include/opencv2/core/types.hpp:201
The class represents rotated (i.e. not up-right) rectangles on a plane.
Definition: modules/core/include/opencv2/core/types.hpp:538
Point2f center
returns the rectangle mass center
Definition: modules/core/include/opencv2/core/types.hpp:570
Size2f size
returns width and height of the rectangle
Definition: modules/core/include/opencv2/core/types.hpp:572
void points(Point2f pts[]) const
_Tp height
the height
Definition: modules/core/include/opencv2/core/types.hpp:363
_Tp width
the width
Definition: modules/core/include/opencv2/core/types.hpp:362
Template class for short numerical vectors, a partial case of Matx.
Definition: matx.hpp:369
void min(InputArray src1, InputArray src2, OutputArray dst)
Calculates per-element minimum of two arrays or an array and a scalar.
void max(InputArray src1, InputArray src2, OutputArray dst)
Calculates per-element maximum of two arrays or an array and a scalar.
Point2i Point
Definition: modules/core/include/opencv2/core/types.hpp:209
Size2i Size
Definition: modules/core/include/opencv2/core/types.hpp:370
Scalar_< double > Scalar
Definition: modules/core/include/opencv2/core/types.hpp:709
Point_< float > Point2f
Definition: modules/core/include/opencv2/core/types.hpp:207
#define CV_32F
Definition: core/include/opencv2/core/hal/interface.h:78
unsigned char uchar
Definition: core/include/opencv2/core/hal/interface.h:51
#define CV_8UC3
Definition: core/include/opencv2/core/hal/interface.h:90
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
#define MIN(a, b)
Definition: cvdef.h:514
#define MAX(a, b)
Definition: cvdef.h:518
@ WINDOW_NORMAL
the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal s...
Definition: highgui.hpp:143
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.
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 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.
Size getTextSize(const String &text, int fontFace, double fontScale, int thickness, int *baseLine)
Calculates the width and height of a text string.
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 line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a line segment connecting two points.
@ FONT_HERSHEY_COMPLEX
normal size serif font
Definition: imgproc/include/opencv2/imgproc.hpp:904
@ LINE_8
8-connected line
Definition: imgproc/include/opencv2/imgproc.hpp:893
@ LINE_AA
antialiased line
Definition: imgproc/include/opencv2/imgproc.hpp:894
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 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.
@ CHAIN_APPROX_NONE
Definition: imgproc/include/opencv2/imgproc.hpp:443
@ RETR_LIST
Definition: imgproc/include/opencv2/imgproc.hpp:427
int main(int argc, char *argv[])
Definition: highgui_qt.cpp:3
void scale(cv::Mat &mat, const cv::Mat &range, const T min, const T max)
Definition: quality_utils.hpp:90
Definition: core/include/opencv2/core.hpp:107