OpenCV绘制箭头

转载自:http://tmjfzy.blog.163.com/blog/static/664470252012225101017794/

代码:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;

void drawArrow(cv::Mat& img, cv::Point pStart, cv::Point pEnd, int len, int alpha,
    cv::Scalar& color, int thickness = 1, int lintType = 8);

int main(int argc, char** argv)
{
    cv::Mat mat = imread("test.jpg");
    namedWindow("test");
    imshow("test", mat);

    Mat m(400, 400, CV_8UC3, Scalar(0, 0, 0));
    Point pStart(380, 100), pEnd(100, 250);
    Scalar lineColor(0, 255, 255);
    drawArrow(m, pStart, pEnd, 10, 45, lineColor);
    pStart = Point(100, 100);
    pEnd = Point(320, 190);
    lineColor = Scalar(0, 0, 255);
    drawArrow(m, pStart, pEnd, 25, 30, lineColor, 2, CV_AA);
    pStart = Point(200, 420);
    pEnd = Point(370, 170);
    lineColor = Scalar(255, 0, 255);
    drawArrow(m, pStart, pEnd, 17, 15, lineColor, 1, 4);
    imshow("draw arrow", m);

    waitKey();
    return 0;
}


void drawArrow(cv::Mat& img, cv::Point pStart, cv::Point pEnd, int len, int alpha, cv::Scalar& color, int thickness, int lineType)
{
    const double PI = 3.1415926;
    Point arrow;
    //计算 θ 角(最简单的一种情况在下面图示中已经展示,关键在于 atan2 函数,详情见下面)   
    double angle = atan2((double)(pStart.y - pEnd.y), (double)(pStart.x - pEnd.x));

    line(img, pStart, pEnd, color, thickness, lineType);

    //计算箭角边的另一端的端点位置(上面的还是下面的要看箭头的指向,也就是pStart和pEnd的位置) 
    arrow.x = pEnd.x + len * cos(angle + PI * alpha / 180);

    arrow.y = pEnd.y + len * sin(angle + PI * alpha / 180);

    line(img, pEnd, arrow, color, thickness, lineType);

    arrow.x = pEnd.x + len * cos(angle - PI * alpha / 180);

    arrow.y = pEnd.y + len * sin(angle - PI * alpha / 180);

    line(img, pEnd, arrow, color, thickness, lineType);
}

结果
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值