实例:图形绘制[OpenCV 笔记15]

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

实例:图形绘制[OpenCV 笔记15]的相关文章

图像载入 imshow()[OpenCV 笔记5]

void imshow(const string& winname InputArray mat); winname 窗口表识名称 mat 需要显示的图像.InputArray类型,声明如下 typedef const _InputArray& InputArray; _InputArray定义比较复杂,类里先定义了一个枚举,然后是各类的模版类型和一些方法.遇到InputArray/OutputArray类型,可以把它当作Mat类型处理. 图像大小缩放 如果窗口是用CV_WINDOW_AU

查找并绘制轮廓[OpenCV 笔记XX]

好久没有更新了,原谅自己放了个假最近又在赶进度,所以...更新的内容是很靠后的第八章,因为最近工作要用就先跳了,后面会更新笔记编号...加油加油! 在二值图像中寻找轮廓 void cv::findContours ( InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset = Point() ) image: 输入图像,需为8位单

图像处理简单实例[OpenCV 笔记1]

几个入门的简单程序,和对应的CMakeList, 虽然简单重新测一下写一下也是好的. CMake教程传送门 图像显示 ShowImage.cxx #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgcodecs/imgcodecs.hpp> int main(){ cv::Mat srcImage = cv::imread("1.jp

实例:图像载入、显示、混合与输出[OpenCV 笔记8]

是的是的,忍着尿意努力更新,就是为了更到wuli男神的部分,当然要把男神放在前面镇楼,欢迎下载配图,具体操作见code wuliEddie.jpg logo.png results.jpg LoadShowWriteImage.cxx #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> //#include <opencv2/imgcodecs/imgcodecs.hpp> //#in

视频处理简单实例 [OpenCV 笔记2]

VideoCapture是OpenCV 2.X中新增的类,提供从摄像机或视频文件捕获视频的C++接口.利用它读入视频的方法一般有两种: // method 1 VideoCapture capture; capture.open("1.avi"); // method 2 VideoCapture capture("1.avi"); 读取并播放视频 ReadPlayVideo.cxx #include <opencv2/opencv.hpp> int m

OpenCV基本架构[OpenCV 笔记0]

最近正在系统学习OpenCV,将不定期发布笔记,主要按照毛星云的<OpenCV3编程入门>的顺序学习,会参考官方教程和文档.学习工具是Xcode+CMake,会对书中一部分内容更正,并加入cmakelist的内容. 书中大部分内容来自OpenCV文档,其实比较推荐官方文档和教程 OpenCV2.4.13: http://docs.opencv.org/2.4/index.html OpenCV安装路径下的include文件夹包含opencv和opencv2两个文件夹.opencv文件夹包含Op

输出图像到文件 imwrite()[OpenCV 笔记7]

bool imwrite(const string& filename, InputArray img, const vector<int>& params=vector<int>()); filename 待写入的文件名.保存图像的格式由扩展名决定. img 一般为一个Mat类型的图像 params 特定格式保存的参数编码: JPEG:params表示0到100的图片质量(CV_IMWRITE_JPEG_QUALITY),默认值为95: PNG:params表示压

滑动条 Trackbar[OpenCV 笔记9]

OpenCV中没有实现按钮的功能,我们可以利用滑动条来实现按钮功能. int createTrackerbar(const string& trackerbarname, const string winame, int* value, int count, TrackbarCallback onChange=0, void* userdata=0); trackbarname 轨迹条的名字. winname 窗口的名字,轨迹条会依附在这个窗口上. value 一个指向整型的指针,表示滑块的位置

OpenCV Tricks[OpenCV 笔记3]

官方例程 事例程序位于opencv-3.1.0/samples/cpp/ 目录下,可以通过编译整个工程,编译所有的Sample Code 显示当前使用的OpenCV版本 CV_VERSION为标识当前OpenCV版本的宏 printf("\t OpenCV Version: OpenCV " CV_VERSION); 头文件opencv.hpp opencv2/opencv.hpp中包含了OpenCV各模块的头文件,原则上仅写一句: #include <opencv2/openc