 |
OpenCV
4.2.0
Open Source Computer Vision
|
A program using pyramid scaling, Canny, contours and contour simplification to find squares in the input image.
#include <iostream>
using namespace std;
int thresh = 50, N = 11;
const char* wndname = "Square Detection Demo";
{
double dx1 = pt1.
x - pt0.
x;
double dy1 = pt1.
y - pt0.
y;
double dx2 = pt2.
x - pt0.
x;
double dy2 = pt2.
y - pt0.
y;
return (dx1*dx2 + dy1*dy2)/
sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}
static void findSquares(
const UMat& image, vector<vector<Point> >& squares )
{
squares.clear();
vector<vector<Point> > contours;
for( int c = 0; c < 3; c++ )
{
int ch[] = {c, 0};
for( int l = 0; l < N; l++ )
{
if( l == 0 )
{
Canny(gray0, gray, 0, thresh, 5);
}
else
{
}
vector<Point> approx;
for( size_t i = 0; i < contours.size(); i++ )
{
if( approx.size() == 4 &&
{
double maxCosine = 0;
for( int j = 2; j < 5; j++ )
{
double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
maxCosine =
MAX(maxCosine, cosine);
}
if( maxCosine < 0.3 )
squares.push_back(approx);
}
}
}
}
}
static void drawSquares(
UMat& _image,
const vector<vector<Point> >& squares )
{
for( size_t i = 0; i < squares.size(); i++ )
{
const Point* p = &squares[i][0];
int n = (int)squares[i].size();
}
}
static UMat drawSquaresBoth(
const UMat& image,
const vector<vector<Point> >& sqs)
{
drawSquares(imgToShow, sqs);
return imgToShow;
}
int main(int argc, char** argv)
{
const char* keys =
"{ i input | ../data/pic1.png | specify input image }"
"{ o output | squares_output.jpg | specify output save path}"
"{ h help | | print help message }"
"{ m cpu_mode | | run without OpenCL }";
if(cmd.has("help"))
{
cout << "Usage : " << argv[0] << " [options]" << endl;
cout << "Available options:" << endl;
cmd.printMessage();
return EXIT_SUCCESS;
}
if (cmd.has("cpu_mode"))
{
cout << "OpenCL was disabled" << endl;
}
string outfile = cmd.get<string>("o");
int iterations = 10;
vector<vector<Point> > squares;
{
cout << "Couldn't load " << inputName << endl;
cmd.printMessage();
return EXIT_FAILURE;
}
int j = iterations;
cout << "warming up ..." << endl;
findSquares(image, squares);
do
{
findSquares(image, squares);
cout << "run loop: " << j << endl;
}
while(--j);
cout <<
"average time: " << 1000.0f * (double)t_cpp /
getTickFrequency() / iterations <<
"ms" << endl;
UMat result = drawSquaresBoth(image, squares);
return EXIT_SUCCESS;
}
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
@ IMREAD_COLOR
If set, always convert image to the 3 channel BGR color image.
Definition: imgcodecs.hpp:67
@ THRESH_BINARY
Definition: imgproc.hpp:317
@ RETR_LIST
Definition: imgproc.hpp:415
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
void mixChannels(const Mat *src, size_t nsrcs, Mat *dst, size_t ndsts, const int *fromTo, size_t npairs)
Copies specified channels from input arrays to the specified channels of output arrays.
Mat getMat(AccessFlag flags) const
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
Applies a fixed-level threshold to each array element.
int waitKey(int delay=0)
Waits for a pressed key.
#define CV_8U
Definition: interface.h:73
softfloat sqrt(const softfloat &a)
Square root.
_Tp y
y coordinate of the point
Definition: types.hpp:187
_Tp x
x coordinate of the point
Definition: types.hpp:186
int type() const
returns element type, similar to CV_MAT_TYPE(cvmat->type)
void Canny(InputArray image, OutputArray edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false)
Finds edges in an image using the Canny algorithm .
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition: mat.hpp:2560
int64_t int64
Definition: interface.h:61
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
void findContours(InputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())
Finds contours in a binary image.
Size2i Size
Definition: types.hpp:347
void dilate(InputArray src, OutputArray dst, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar &borderValue=morphologyDefaultBorderValue())
Dilates an image by using a specific structuring element.
int cols
Definition: mat.hpp:2560
void polylines(InputOutputArray img, const Point *const *pts, const int *npts, int ncontours, bool isClosed, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
double contourArea(InputArray contour, bool oriented=false)
Calculates a contour area.
MatSize size
Definition: mat.hpp:2577
double arcLength(InputArray curve, bool closed)
Calculates a contour perimeter or a curve length.
int64 getTickCount()
Returns the number of ticks.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
void approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed)
Approximates a polygonal curve(s) with the specified precision.
double getTickFrequency()
Returns the number of ticks per second.
Scalar_< double > Scalar
Definition: types.hpp:669
Point2i Point
Definition: types.hpp:194
n-dimensional dense array class
Definition: mat.hpp:792
void copyTo(OutputArray m) const
Copies the matrix to another one.
void pyrDown(InputArray src, OutputArray dst, const Size &dstsize=Size(), int borderType=BORDER_DEFAULT)
Blurs an image and downsamples it.
void pyrUp(InputArray src, OutputArray dst, const Size &dstsize=Size(), int borderType=BORDER_DEFAULT)
Upsamples an image and then blurs it.
Designed for command line parsing.
Definition: utility.hpp:797
bool empty() const
returns true if matrix data is NULL
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:52
#define MAX(a, b)
Definition: cvdef.h:457
@ WINDOW_AUTOSIZE
the user cannot resize the window, the size is constrainted by the image displayed.
Definition: highgui.hpp:184
void copyTo(OutputArray m) const
copies the matrix content to "m".
@ ACCESS_WRITE
Definition: mat.hpp:64
@ CHAIN_APPROX_SIMPLE
Definition: imgproc.hpp:434
CV_EXPORTS_W bool imwrite(const String &filename, InputArray img, const std::vector< int > ¶ms=std::vector< int >())
Saves an image to a specified file.
void setUseOpenCL(bool flag)
@ LINE_AA
antialiased line
Definition: imgproc.hpp:807
bool isContourConvex(InputArray contour)
Tests a contour convexity.