Basic drawing examples

Drawing a line

void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
 Parameters:
  • img – Image.
  • pt1 – First point of the line segment.
  • pt2 – Second point of the line segment.
  • color – Line color.
  • thickness – Line thickness.
  • lineType – Type of the line:
    • 8 (or omitted) - 8-connected line.
    • 4 - 4-connected line.
    • CV_AA - antialiased line.
  • shift – Number of fractional bits in the point coordinates.

Example 1: Drawing a line

-------------
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

int main( )
{    
  // Create black empty images
  Mat image = Mat::zeros( 400, 400, CV_8UC3 );
  
  // Draw a line 
  line( image, Point( 15, 20 ), Point( 70, 50), Scalar( 110, 220, 0 ),  2, 8 );
  imshow("Image",image);

  waitKey( 0 );
  return(0);
}
-------------

Drawing a Circle

void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
Parameters:
  • img – Image where the circle is drawn.
  • center – Center of the circle.
  • radius – Radius of the circle.
  • color – Circle color.
  • thickness – Thickness of the circle outline, if positive. Negative thickness means that a filled circle is to be drawn.
  • lineType – Type of the circle boundary. See the line() description.
  • shift – Number of fractional bits in the coordinates of the center and in the radius value.
The function circle draws a simple or filled circle with a given center and radius.

Example 2: Drawing a Circle

-------------
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

int main( )
{    
  // Create black empty images
  Mat image = Mat::zeros( 400, 400, CV_8UC3 );
  
  // Draw a circle 
  circle( image, Point( 200, 200 ), 32.0, Scalar( 0, 0, 255 ), 1, 8 );
  imshow("Image",image);

  waitKey( 0 );
  return(0);
}
-------------

Drawing an Ellipse

void ellipse(Mat& img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
Parameters:
  • img – Image.
  • center – Center of the ellipse.
  • axes – Length of the ellipse axes.
  • angle – Ellipse rotation angle in degrees.
  • startAngle – Starting angle of the elliptic arc in degrees.
  • endAngle – Ending angle of the elliptic arc in degrees.
  • box – Alternative ellipse representation via RotatedRect or CvBox2D. This means that the function draws an ellipse inscribed in the rotated rectangle.
  • color – Ellipse color.
  • thickness – Thickness of the ellipse arc outline, if positive. Otherwise, this indicates that a filled ellipse sector is to be drawn.
  • lineType – Type of the ellipse boundary. See the line() description.
  • shift – Number of fractional bits in the coordinates of the center and values of axes.
The functions ellipse with less parameters draw an ellipse outline, a filled ellipse, an elliptic arc, or a filled ellipse sector. A piecewise-linear curve is used to approximate the elliptic arc boundary.

If you use the first variant of the function and want to draw the whole ellipse, not an arc, pass startAngle=0 and endAngle=360.

Example 3: Drawing an Ellipse

-------------
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

int main( )
{    
  // Create black empty images
  Mat image = Mat::zeros( 400, 400, CV_8UC3 );
  
  // Draw a ellipse 
  ellipse( image, Point( 200, 200 ), Size( 100.0, 160.0 ), 45, 0, 360, Scalar( 255, 0, 0 ), 1, 8 );
  ellipse( image, Point( 200, 200 ), Size( 100.0, 160.0 ), 135, 0, 360, Scalar( 255, 0, 0 ), 10, 8 );
  ellipse( image, Point( 200, 200 ), Size( 150.0, 50.0 ), 135, 0, 360, Scalar( 0, 255, 0 ), 1, 8 );
  imshow("Image",image);

  waitKey( 0 );
  return(0);
}
-------------

Drawing a Rectangle

void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
Parameters:
  • img – Image.
  • pt1 – Vertex of the rectangle.
  • pt2 – Vertex of the rectangle opposite to pt1 .
  • rec – Alternative specification of the drawn rectangle.
  • colorRectangle color or brightness (grayscale image).
  • thickness – Thickness of lines that make up the rectangle. Negative values, like CV_FILLED , mean that the function has to draw a filled rectangle.
  • lineType – Type of the line. See the line() description.
  • shift – Number of fractional bits in the point coordinates.

Example 4: Drawing a Rectangle

-------------
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

int main( )
{    
  // Create black empty images
  Mat image = Mat::zeros( 400, 400, CV_8UC3 );
  
  // Draw a rectangle ( 5th argument is not -ve)
  rectangle( image, Point( 15, 20 ), Point( 70, 50), Scalar( 0, 55, 255 ), +1, 4 );
  imshow("Image1",image);
  // Draw a filled rectangle ( 5th argument is -ve)
  rectangle( image, Point( 115, 120 ), Point( 170, 150), Scalar( 100, 155, 25 ), -1, 8 );
  imshow("Image2",image);

  waitKey( 0 );
  return(0);
}
-------------

Drawing a Filled Polygon

void fillPoly(Mat& img, const Point** pts, const int* npts, int ncontours, const Scalar& color, int lineType=8, int shift=0, Point offset=Point() )
Parameters:
  • img – Image.
  • pts – Array of polygons where each polygon is represented as an array of points.
  • npts – Array of polygon vertex counters.
  • ncontours – Number of contours that bind the filled region.
  • color – Polygon color.
  • lineType – Type of the polygon boundaries. See the line() description.
  • shift – Number of fractional bits in the vertex coordinates.
  • offset – Optional offset of all points of the contours.
The function fillPoly fills an area bounded by several polygonal contours. The function can fill complex areas, for example, areas with holes, contours with self-intersections (some of their parts), and so forth.

Example 4: Drawing a Filled Polygon

-------------
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

int main( )
{    
  // Create black empty images
  Mat image = Mat::zeros( 400, 400, CV_8UC3 );
  
  int w=400;
  // Draw a circle 
  /** Create some points */
  Point rook_points[1][20];
  rook_points[0][0] = Point( w/4.0, 7*w/8.0 );
  rook_points[0][1] = Point( 3*w/4.0, 7*w/8.0 );
  rook_points[0][2] = Point( 3*w/4.0, 13*w/16.0 );
  rook_points[0][3] = Point( 11*w/16.0, 13*w/16.0 );
  rook_points[0][4] = Point( 19*w/32.0, 3*w/8.0 );
  rook_points[0][5] = Point( 3*w/4.0, 3*w/8.0 );
  rook_points[0][6] = Point( 3*w/4.0, w/8.0 );
  rook_points[0][7] = Point( 26*w/40.0, w/8.0 );
  rook_points[0][8] = Point( 26*w/40.0, w/4.0 );
  rook_points[0][9] = Point( 22*w/40.0, w/4.0 );
  rook_points[0][10] = Point( 22*w/40.0, w/8.0 );
  rook_points[0][11] = Point( 18*w/40.0, w/8.0 );
  rook_points[0][12] = Point( 18*w/40.0, w/4.0 );
  rook_points[0][13] = Point( 14*w/40.0, w/4.0 );
  rook_points[0][14] = Point( 14*w/40.0, w/8.0 );
  rook_points[0][15] = Point( w/4.0, w/8.0 );
  rook_points[0][16] = Point( w/4.0, 3*w/8.0 );
  rook_points[0][17] = Point( 13*w/32.0, 3*w/8.0 );
  rook_points[0][18] = Point( 5*w/16.0, 13*w/16.0 );
  rook_points[0][19] = Point( w/4.0, 13*w/16.0) ;

  const Point* ppt[1] = { rook_points[0] };
  int npt[] = { 20 };

  fillPoly( image, ppt, npt, 1, Scalar( 255, 255, 255 ), 8 );
  imshow("Image",image);

  waitKey( 0 );
  return(0);
}
-------------

Putting Text in image

putText renders the specified text string in the image.

void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false )
Parameters:
  • img – Image.
  • text – Text string to be drawn.
  • org – Bottom-left corner of the text string in the image.
  • fontFace – Font type. One of FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX, FONT_HERSHEY_COMPLEX, FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL, FONT_HERSHEY_SCRIPT_SIMPLEX, or FONT_HERSHEY_SCRIPT_COMPLEX, where each of the font ID’s can be combined with FONT_HERSHEY_ITALIC to get the slanted letters.
  • fontScale – Font scale factor that is multiplied by the font-specific base size.
  • color – Text color.
  • thickness – Thickness of the lines used to draw a text.
  • lineType – Line type. See the line for details.
  • bottomLeftOrigin – When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.

Example 5: Putting Text in image

-------------
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

int main( )
{ 
  // Create black empty images
  Mat image = Mat::zeros( 400, 400, CV_8UC3 );
  
  putText(image, "Hi all...", Point(50,100), FONT_HERSHEY_SIMPLEX, 1, Scalar(0,200,200), 4);
  imshow("Image",image);

  waitKey( 0 );
  return(0);
}
-------------

Source:
http://docs.opencv.org/modules/core/doc/drawing_functions.html?highlight=rectangle#void%20line%28Mat&%20img,%20Point%20pt1,%20Point%20pt2,%20const%20Scalar&%20color,%20int%20thickness,%20int%20lineType,%20int%20shift%29

81 comments:

  1. Great tutorial. Thanks!

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Learn Opencv By Examples: Basic Drawing Examples >>>>> Download Now

      >>>>> Download Full

      Learn Opencv By Examples: Basic Drawing Examples >>>>> Download LINK

      >>>>> Download Now

      Learn Opencv By Examples: Basic Drawing Examples >>>>> Download Full

      >>>>> Download LINK 2d

      Delete
  2. This blog is awesome! You are awesome!

    ReplyDelete
  3. thanks a lot... awesome... do you know that you are awesome...

    ReplyDelete
  4. It helped a lot! Thank you!

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. May need:
    #include "opencv2/imgproc.hpp"

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Hello, I was compiling this using OpenCV 3 , I'm working on MacOS using Xcode 7, in order for it to work i had to delete opencv2/core/core.hpp and included opencv2/imgproc.hpp

    Thank you for this forum, is very neat and clear.

    ReplyDelete
  9. Some of the Interesting Examples:
    http://opencv-hub.blogspot.in/

    ReplyDelete
  10. i found this page really helpful.
    Thanks for the help, you provided.

    ReplyDelete
  11. Do you know how to add new line to the function putText() ?
    Normally it is \n or \r or \r\n ... none of them is working

    ReplyDelete
  12. I am using same putText() function but this appears so big text on image. i need text size that should be readable. text size should not be so big.

    ReplyDelete
  13. great working all these code and so simple thanks alot

    ReplyDelete
  14. You have to add

    #include // drawing shapes

    ReplyDelete
  15. chung cư the kpark có gì hấp dẫn khách hàng? xem ngay tại website http://www.kenhchungcuhanoi.com/2017/05/the-k-park-van-phu.html

    ReplyDelete
  16. http://cit-llp.com/training/best-java-training-institute-in-chennai-at-candid-chrompet.htm

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. I think you should imgproc.hpp because all function line, circle,... belong to imgproc.hpp

    ReplyDelete
  19. Awesome blog, Visit Ogen Infosystem for the best Website Designing Service provider in Delhi, India. Here you will get SEO Service also to promote your website.
    SEO Service in Delhi

    ReplyDelete
  20. Nice Blog, Mutual Fund Wala is one of the best Investment and Mutual Fund Advisor Company in Delhi India. Visit following page for more information.
    Mutual Fund Distributor

    ReplyDelete
  21. Students should use our company in the provision of their Custom Written College Papers solutions because we are committed to delivering 100% Online Essay Writing Services solutions.

    ReplyDelete
  22. بموقع مؤسسة الحرمــين فخدماتنا ليس لها بديل واسعارنا ليس لها مثيل ،ولدينا فريق عمل يتصل مع العملاء على جسور الثقه
    شركه تنظيف منازل بالجبيل
    والصدق والامانه فى العمل ، وهدفنا هو ارضاؤك وراحتك ، لا تقلق ونحن معك

    شركه عزل فوم بالجبيل
    لا تجهد نفسك ونحن تحت امرك ورهن اشارتك .
    أبرز خدمات مؤسسة الحرمــين للمقاولات العامة بالدمام والرياض

    شركه عزل فوم بالدمام


    شركه عزل اسطح بالاحساء

    شركه كشف تسربات المياه بالاحساء


    ReplyDelete
  23. Amazing blog, thanks for sharing this valuable information. Visit Kalakutir Pvt Ltd for Indoor & Outdoor Advertising and Food Truck Branding.
    Indoor & Outdoor Advertising

    ReplyDelete
  24. In this Article we review how online computer sciences courses can help prospective professionals find careers in any industry. artificial intelligence course in hyderabad

    ReplyDelete
  25. Hi! Nice blog. We are also offering you QuickBooks Customer Service Number. If you need any help regarding QuickBooks issues, dial 1-855-756-1077 for instant help.

    ReplyDelete
  26. Download norton antivirus to make your computer virus free with the best support and tech team. norton.com/setup Feel free to contact us. Norton web security is commonly used antivirus gives the least requesting to use and most intutive affirmation for your PC and your mobiles .present it and negligence viruses,spyware,root-units - , Download norton 360 hackers.https://my.norton.com/home/setup for more nuances visit.To enact the Norton setup, select the Activate Now option at the base. To recharge the membership for Norton, select the Help choice and snap on Enter item key. https://my.norton.com/home/setup Cautiously type the right Norton item key in the clear. Snap on the Next catch.Go through with for more details.

    Download Norton Mobile Security and Antivirus https://my.norton.com/onboard/home/setup application that can shield your records from getting affected from any online malware or contamination norton setup product key.Go to the Norton Security Online page and click Get Norton Security Online. Type in your Xfinity ID and password, if you're asked. Create a Norton account, then sign in. https://my.norton.com/onboard/home/setup Choose whether you want to install it on this device or another one. Start your installation. Click Run. Let the program run. If Windows asks for permission, click Yes.

    ReplyDelete
  27. System Mechanic Free keeps your PC running at peak performance and stability with advanced PC optimization, repair and maintenance features. system mechanic To install the most current version of System Mechanic Ultimate Defense, download the System Mechanic download manager via your internet.System Mechanic is the essential PC performance package that helps you automatically fix and speed up your PC. system mechanic System Mechanic is the most effective way to restore and maintain maximum PC speed and stability.

    ReplyDelete
  28. If your computer does not have the CD-ROM drive or you do not have the Setup CD, follow the steps given below. Visit canon.com/ijsetup Click Set Up. Either type in the model name of your printer, or click the first two letters shown under First Letters.The Canon printer enhances scan functionality, and includes a robust security feature set. Using a Canon printer service phone, you can get a full installation of the Canon printer and go to the installed Canon printer to download the canon.com/ijsetup driver.

    ReplyDelete
  29. Enter the link Amazon mytv enter code on your browser. Input your login details to log in to your Amazon Prime video account. A new screen will open which will ask for the “ Amazon Prime Verification Code . Amazon.com/mytventercode Amazon Prime is a subscription service that caters to the media needs of people through the platform. Notifies visitors about topics such as Register TV, Amazon Prime TV, and MyTV code. To register your device on the Amazon website you go to our website and enter the given code. The program requires the customer to have an Amazon account and a streaming device or TV. amazon mytv Amazon Prime Video is available on almost all streaming devices like Roku, Amazon Fire TV, Chromecast. For this, you just have to go to Amazon with the help of the amazon activation code. You can visit our website for details of the entire process. You can do this by installing the Video application on your smart TV, or the digital media player or video game console connected to your TV by visiting the link provided by us in the de cription.

    ReplyDelete
  30. Here is a detailed guide on primevideo.com/mytv registration, how you can get the activation code to activate Prime video on your smart TVs.Using a computer, go to primevideo.com/mytv Sign-in with an active e-mail address and password or primevideo.com/mytv click Create your Amazon account.

    ReplyDelete
  31. Amazon Prime TV, and MyTV code. To register your device on the Amazon website you go to our website and enter the given code. primevideo.com/mytv The program requires the customer to have an Amazon account and a streaming device or TV. Amazon Prime Video is available on almost all streaming devices like Roku, Amazon Fire TV, Chromecast. primevideo.com/mytv For this, you just have to go to Amazon with the help of the amazon activation code. You can visit our website for details of the entire process. You can do this by installing the primevideo.com/mytv Video application on your smart TV, or the digital media player or video game console connected to your TV by visiting the link provided by us in the de cription.

    ReplyDelete
  32. learn how to jailbreak fireststick in latest way

    ReplyDelete
  33. This comment has been removed by the author.

    ReplyDelete
  34. There are many blogs I have read. But when I read Your Blogs I have found such useful information, fresh content with such amazing editing everything is superb in your blog. Thank you so much for sharing this useful and informative information with us.

    online medicine order in jaipur

    ReplyDelete
  35. Gemini is a regulated cryptocurrency exchange, wallet, and custodian that makes it simple and secure to buy bitcoin,
    ether, and other cryptocurrencies. sign in to your wallet through gemini login.
    Webroot Secureanywhere is the best software to protect your Pc & Laptop from unwanted guests like viruses and malwares.
    Coinbase login is a secure online platform for buying, selling, transferring, and storing cryptocurrency. ...
    coinbase login to your Coinbase account or. Sign up as a business.

    ReplyDelete
  36. Thanks for provide valuable content. We have some valuable details related to Xfinity Stream App. To activate a Xfinity Gateway go to its main site that is xfinity.com/authorize and Explore best services online for Internet, Video and Mobile services.You are probably wondering what exactly Xfinity Internet is and whether it is for you. To begin with, Xfinity is a high speed internet service provided by Level Yellow Communications to customers in San Diego, California. This is a high speed internet service that works very well with the telephone lines available in the San Diego area. When you activate xfinity.com/authorize, you become a member
    plan which has two separate plans. One to use on your own computer, the other to connect to the television service.

    https://xfinity-comauthorize.com/!

    ReplyDelete
  37. Amazon defines the e-commerce industry in the new digital era, and amzon.com/code has been their edition of offering customers saving options. Amazon first got into the entertainment field envisioning an advanced modification to viewing experiences.
    amazon prime promo code
    canon,com/ijsetup
    paypal login
    Paypal Account Login
    Aol Mail Login

    ReplyDelete
  38. نظافة المنزل تعتبر نظافة المنزل عاملاً أساسياً للصحة الجسدية والعقلية لأفرادها ، ولأن النظافة هي أساس الوقاية من الأمراض الناتجة عن تراكم الأوساخ والبكتيريا وتعكس صورة ونمط حياة صاحب المنزل ، تحتاج كل أسرة إلى إنشاء نظام للحفاظ على جميع تفاصيل المنزل والبقاء على اتصال مع جميع أفراد الأسرة

    شركة الابداع للخدمات المنزلية

    ReplyDelete
  39. If you are having trouble completing 2-step verification to sign in to your account, make sure your mobile device software and coinbase login up to date. If you're using a browser to sign in, make sure you're using the latest version of Chrome. Clearing your cache and restarting your browser can also help. For more Gemini login

    ReplyDelete
  40. If your website is ranking on a search engine by paying money and running ads

    ReplyDelete
  41. Very efficiently written information. It will be valuable to everyone who utilizes it, please Keep doing what you are doing and keep sharing amazing information like this, it means so much if you can checkout some of mine. RBC online banking login | BMO online banking | CIBC online banking | RBC online banking | Hulu login

    ReplyDelete
  42. Thanks for sharing this blog here. It seems really very informative.
    Get solution to activate roku streaming device using roku com link

    ReplyDelete
  43. This comment has been removed by the author.

    ReplyDelete
  44. Ij start canon and find out the best way to download Canon printer drivers.

    Ij.start.cannon | Ij.start.canon | canon.com/ijsetup

    ReplyDelete
  45. Learn Opencv By Examples: Basic Drawing Examples >>>>> Download Now

    >>>>> Download Full

    Learn Opencv By Examples: Basic Drawing Examples >>>>> Download LINK

    >>>>> Download Now

    Learn Opencv By Examples: Basic Drawing Examples >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete
  46. 360DigiTMG, the top-rated organisation among the most prestigious industries around the world, is an educational destination for those looking to pursue their dreams around the globe. The company is changing careers of many people through constant improvement, 360DigiTMG provides an outstanding learning experience and distinguishes itself from the pack. 360DigiTMG is a prominent global presence by offering world-class training. Its main office is in India and subsidiaries across Malaysia, USA, East Asia, Australia, Uk, Netherlands, and the Middle East.

    ReplyDelete
  47. This comment has been removed by the author.

    ReplyDelete
  48. I am here to inform you that If you are interested to know more ideas about home decoration so you can visit here:- wall art painting design

    ReplyDelete
  49. Presenta inoltre il sistema Ringlock brevettato dal marchio orologi repliche Rolex e una valvola di scarico dell'elio.

    ReplyDelete