码迷,mamicode.com
首页 > 编程语言 > 详细

用c++的opencv实现以前的python的turtle曲线绘制

时间:2021-02-19 13:16:30      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:oca   int   line   坐标   org   cal   com   一个   str   

        opencv库的安装配置的教程网上很多了,这个可以作为图像处理及2D图形的绘制库,一个cv::Mat就是一个像素矩阵,cv将图像认为是一个二维矩阵

这个图像的原点在左上角,x轴水平向右,y轴水平向下,所以我们画线的坐标要localPointToCVPoint转,setWorldOrigin这个函数是以图像左下角为绝对原点设置

绘图世界的原点的,所以这个worldCoordinate其实还是一个局部坐标系。代码主要是笔记用,不详细解释了

#include <opencv2/opencv.hpp>
#include <string>
#include <vector>

#define _USE_MATH_DEFINES
#include <math.h>

using namespace cv;

cv::Mat s_Mat;
int s_width = 600;
int s_height = 400;

int s_world_origin_x = 0;
int s_world_origin_y = 0;

int s_colorR = 0;
int s_colorG = 0;
int s_colorB = 0;

int s_penSize = 1;

cv::Point localPointToCVPoint(int x, int y)
{
    return cv::Point(s_world_origin_x + x, s_height - 1 - (s_world_origin_y + y));
}

void setUp(int width, int height)
{
    s_Mat = cv::Mat(height, width, CV_8UC3, cv::Scalar(255, 255, 255));
    s_width = width;
    s_height = height;
}

void setWorldOrigin(int x, int y)
{
    s_world_origin_x = x;
    s_world_origin_y = y;
}

void setPenSize(int size)
{
    s_penSize = size;
}

void setColor(int colorR, int colorG, int colorB)
{
    s_colorR = colorR;
    s_colorG = colorG;
    s_colorB = colorB;
}

void drawLine(int x0, int y0, int x1, int y1)
{
    cv::line(s_Mat, localPointToCVPoint(x0,y0), localPointToCVPoint(x1, y1),
        cv::Scalar(s_colorB, s_colorG, s_colorR), 
        s_penSize);
}

void drawText(const cv::String& textStr, int x, int y)
{
    cv::putText(s_Mat, textStr, localPointToCVPoint(x, y), 
        cv::FONT_HERSHEY_SIMPLEX, 0.5,
        cv::Scalar(s_colorB, s_colorG, s_colorR), s_penSize);
}

void drawXAxis(float x0, float y0, float axTailLength, float arrowWidth, float arrowHeight)
{
    drawLine(x0, y0, x0 + axTailLength + arrowHeight, y0);
    int halfw = arrowWidth * 0.5;
    drawLine(x0 + axTailLength, y0 - halfw, x0 + axTailLength, y0 + halfw);
    drawLine(x0 + axTailLength, y0 - halfw, x0 + axTailLength + arrowHeight, y0);
    drawLine(x0 + axTailLength, y0 + halfw, x0 + axTailLength + arrowHeight, y0);
    drawText("X", x0 + axTailLength + arrowHeight, y0);
}

void drawYAxis(float x0, float y0, float axTailLength, float arrowWidth, float arrowHeight)
{
    drawLine(x0, y0, x0, y0 + axTailLength + arrowHeight);
    float halfw = arrowWidth*0.5;
    drawLine(x0 - halfw, y0 + axTailLength, x0 + halfw, y0 + axTailLength);
    drawLine(x0 - halfw, y0 + axTailLength, x0, y0 + axTailLength + arrowHeight);
    drawLine(x0 + halfw, y0 + axTailLength, x0, y0 + axTailLength + arrowHeight);
    drawText("Y", x0, y0 + axTailLength + arrowHeight);
}


int main()
{
    setUp(500, 400);
    setWorldOrigin(100, 150);
    drawXAxis(-20, 0, 350, 10, 10);
    drawYAxis(0, -20, 200, 10, 10);

    int n = 100;
    double dsi = (2.0 * M_PI) / double(n);
    
    std::vector<int> ptsX;
    std::vector<int> ptsY;

    for (int i = 0; i <= n; i++)
    {
        double si = dsi * double(i);
        ptsX.push_back(int(si*40.0));
        double sinsi = sin(si);
        ptsY.push_back(int(sinsi*100.0));
    }


    setColor(0, 180, 0);
    setPenSize(2);

    for (int i = 1; i <= n; i++)
    {
        drawLine(ptsX[i - 1], ptsY[i - 1], ptsX[i], ptsY[i]);
    }

    cv::imshow("turle similar", s_Mat);
    cv::waitKey();
    return 0;
}

技术图片

 

 这个cv实现的画线和turtle的画线在线宽处理上还是有点区别

用c++的opencv实现以前的python的turtle曲线绘制

标签:oca   int   line   坐标   org   cal   com   一个   str   

原文地址:https://www.cnblogs.com/abcstar/p/14409515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!