DrawShapes.cxx
# include "DrawShapes_utils.h" #define WINDOW_NAME1 "Painting 1" #define WINDOW_NAME2 "Painting 2" // main int main( void ) { // crate Mat image cv::Mat atomImage = cv::Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3); cv::Mat rookImage = cv::Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3); // draw image 1 // 1) draw ellipses DrawEllipse( atomImage, 90 ); DrawEllipse( atomImage, 0 ); DrawEllipse( atomImage, 45 ); DrawEllipse( atomImage, -45 ); // 2) draw circle DrawFilledCircle(atomImage, cv::Point(WINDOW_WIDTH/2, WINDOW_WIDTH/2)); // draw image 2 // 1) draw polygon DrawPolygon( rookImage ); // 2) draw rectangle cv::rectangle(rookImage, cv::Point(0, 7*WINDOW_WIDTH/8), cv::Point(WINDOW_WIDTH, WINDOW_WIDTH), cv::Scalar( 0, 255, 255), -1, 8); // 3) draw line segments DrawLine(rookImage, cv::Point(0, 15*WINDOW_WIDTH/16), cv::Point(WINDOW_WIDTH, 15*WINDOW_WIDTH/16)); DrawLine(rookImage, cv::Point(WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8), cv::Point(WINDOW_WIDTH/4, WINDOW_WIDTH)); DrawLine(rookImage, cv::Point(WINDOW_WIDTH/2, 7*WINDOW_WIDTH/8), cv::Point(WINDOW_WIDTH/2, WINDOW_WIDTH)); DrawLine(rookImage, cv::Point(3*WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8), cv::Point(3*WINDOW_WIDTH/4, WINDOW_WIDTH)); // show images cv::imshow( WINDOW_NAME1, atomImage ); cv::moveWindow( WINDOW_NAME1, 0, 200 ); cv::imshow( WINDOW_NAME2, rookImage ); cv::moveWindow( WINDOW_NAME2, WINDOW_WIDTH, 200 ); cv::imwrite("atomImage.jpg", atomImage); cv::imwrite("rookImage.jpg", rookImage); cv::waitKey(0); return 0; }
DrawShapes_utils.h
#ifndef DRAWSHAPES_H_ #define DRAWSHAPES_H_ #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #define WINDOW_WIDTH 600 // draw ellipse with the same size but different angle void DrawEllipse( cv::Mat img, double angle ); // draw filled circle void DrawFilledCircle( cv::Mat img, cv::Point center ); // draw polygon void DrawPolygon( cv::Mat img ); // draw line void DrawLine( cv::Mat img, cv::Point start, cv::Point end ); #endif // DRAWSHAPES_H_
DrawShapes_utils.cxx
#include "DrawShapes_utils.h" // draw ellipse with the same size but different angle void DrawEllipse( cv::Mat img, double angle ) { int thickness = 2; int lineType = 8; ellipse( img, cv::Point( WINDOW_WIDTH/2, WINDOW_WIDTH/2 ), // center point cv::Size(WINDOW_WIDTH/4, WINDOW_WIDTH/16), // bounding box angle, // rotation angle 0, // arc start from 0 degree 360, // to 360 degrees cv::Scalar(255,129,0), // color of the ellipse thickness, // line width lineType); // line type: 8 neighbor connected line } // draw filled circle void DrawFilledCircle( cv::Mat img, cv::Point center ) { int thickness = -1; int lineType = 8; cv::circle( img, center, WINDOW_WIDTH/32, // radius cv::Scalar(0, 0, 255), // color thickness, // line width: -1 filled lineType); // line type } // draw polygon void DrawPolygon( cv::Mat img ) { int lineType = 8; // create points cv::Point rookPoints[1][20]; rookPoints[0][0] = cv::Point( WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 ); rookPoints[0][1] = cv::Point( 3*WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 ); rookPoints[0][2] = cv::Point( 3*WINDOW_WIDTH/4, 13*WINDOW_WIDTH/16 ); rookPoints[0][3] = cv::Point( 11*WINDOW_WIDTH/16, 13*WINDOW_WIDTH/16 ); rookPoints[0][4] = cv::Point( 19*WINDOW_WIDTH/32, 3*WINDOW_WIDTH/8 ); rookPoints[0][5] = cv::Point( 3*WINDOW_WIDTH/4, 3*WINDOW_WIDTH/8 ); rookPoints[0][6] = cv::Point( 3*WINDOW_WIDTH/4, WINDOW_WIDTH/8 ); rookPoints[0][7] = cv::Point( 26*WINDOW_WIDTH/40, WINDOW_WIDTH/8 ); rookPoints[0][8] = cv::Point( 26*WINDOW_WIDTH/40, WINDOW_WIDTH/4 ); rookPoints[0][9] = cv::Point( 22*WINDOW_WIDTH/40, WINDOW_WIDTH/4 ); rookPoints[0][10] = cv::Point( 22*WINDOW_WIDTH/40, WINDOW_WIDTH/8 ); rookPoints[0][11] = cv::Point( 18*WINDOW_WIDTH/40, WINDOW_WIDTH/8 ); rookPoints[0][12] = cv::Point( 18*WINDOW_WIDTH/40, WINDOW_WIDTH/4 ); rookPoints[0][13] = cv::Point( 14*WINDOW_WIDTH/40, WINDOW_WIDTH/4 ); rookPoints[0][14] = cv::Point( 14*WINDOW_WIDTH/40, WINDOW_WIDTH/8 ); rookPoints[0][15] = cv::Point( WINDOW_WIDTH/4, WINDOW_WIDTH/8 ); rookPoints[0][16] = cv::Point( WINDOW_WIDTH/4, 3*WINDOW_WIDTH/8 ); rookPoints[0][17] = cv::Point( 13*WINDOW_WIDTH/32, 3*WINDOW_WIDTH/8 ); rookPoints[0][18] = cv::Point( 5*WINDOW_WIDTH/16, 13*WINDOW_WIDTH/16 ); rookPoints[0][19] = cv::Point( WINDOW_WIDTH/4, 13*WINDOW_WIDTH/16 ); const cv::Point* ppt[1] = {rookPoints[0]}; int npt[] = {20}; cv::fillPoly( img, ppt, // vertices npt, // number of vertices 1, // number of polygon to draw cv::Scalar(255, 255, 255), //color of polygon lineType); } // draw line void DrawLine( cv::Mat img, cv::Point start, cv::Point end ) { int thickness = 2; int lineType = 8; cv::line( img, start, // start point end, // end point cv::Scalar(0, 0, 0), // color of line thickness, // line width lineType); // line type }
CMakeLists.txt
cmake_minimum_required (VERSION 2.8) project (DrawShapes) # find OpenCV packages find_package( OpenCV REQUIRED PATHS /usr/local/Cellar/opencv3/3.1.0_3/share/OpenCV/) include_directories( ${OpenCV_INCLUDE_DIRS} ) # add the executable add_executable (DrawShapes DrawShapes.cxx DrawShapes_utils.h DrawShapes_utils.cxx) target_link_libraries(DrawShapes opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs)
atomImage
rookImage
时间: 2024-11-03 06:43:03