Skip to content

Instantly share code, notes, and snippets.

@mshabunin
Created January 29, 2020 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mshabunin/9a1d05f0b8f0906f19c869f29fb1cd2f to your computer and use it in GitHub Desktop.
Save mshabunin/9a1d05f0b8f0906f19c869f29fb1cd2f to your computer and use it in GitHub Desktop.
Drawing OpenCV logo
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace cv;
using namespace std;
class Logo
{
const int d1;
const int d2;
const int l;
const float h;
const float r1;
const float r2;
const int delta;
inline void drawOne(Mat & image, Point center, double angle1, double angle2, Scalar color) const
{
const Size axes1(round(r1), round(r1));
const Size axes2(round(r2), round(r2));
vector<Point> pts1;
vector<Point> pts2;
ellipse2Poly(center, axes1, 0, angle1, angle2, delta, pts1);
ellipse2Poly(center, axes2, 0, angle1, angle2, delta, pts2);
copy(pts1.rbegin(), pts1.rend(), back_inserter(pts2));
// polylines(image, pts2, true, color);
vector<vector<Point>> polygons;
polygons.push_back(pts2);
fillPoly(image, polygons, color, LINE_AA);
}
public:
Logo(int d1_, int d2_, int l_) :
d1(d1_),
d2(d2_),
l(l_),
h(l * sqrt(3.f) / 2.f),
r1(d1 / 2.f),
r2(d2 / 2.f),
delta(1)
{
}
Size size() const
{
return Size(d2 + l, round(d2 + h));
}
void draw(Mat & image, Point shift = Point(0, 0)) const
{
drawOne(image, shift + Point(round(r2 + l/2), round(r2)), 120, 360 + 60, Scalar(1, 1, 0x4F));
drawOne(image, shift + Point(round(r2), round(r2 + h)), 0, 300, Scalar(1, 0x4F, 1));
drawOne(image, shift + Point(round(r2 + l), round(r2 + h)), -60, 240, Scalar(0x4F, 1, 1));
}
};
Mat image;
int main( int argc, char** argv ){
Mat image(800, 800, CV_8UC3, Scalar::all(0));
Logo logo(150, 205, 300);
const Size sz = image.size() - logo.size();
const int NUM = 10000;
TickMeter tm;
tm.start();
for (int i = 0; i < NUM; ++i)
logo.draw(image, Point(sz.width - 1, sz.height - 1));
tm.stop();
cout << "Time: " << tm.getTimeSec() * 1000 / NUM << " ms" << endl;
namedWindow( "img", WINDOW_AUTOSIZE );
imshow( "img", image );
waitKey(0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment