互联网@时事传播OpenCv图像处理

【图像处理】OpenCV系列二十九 --- line、circl

2019-05-12  本文已影响98人  307656af5a04

上一节我们学习了如何在图像上绘制一个带箭头的的直线,相信大家学习之后,已经理解了,本节呢,我们学习如何在图像上绘制圆孔。

一、绘制直线line()函数详解

1、函数原型

void line(InputOutputArray img, 
    Point pt1, 
    Point pt2, 
    const Scalar& color,
    int thickness = 1, 
    int lineType = LINE_8, 
    int shift = 0);

2、函数功能
在图像上绘制任意的直线;

3、参数详解

线的类型:
FILLED,填充;
LINE_4,4连通的线条;
LINE_8 ,8连通的线条;
LINE_AA ,抗锯齿线条;

4、实验案例

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    // 创建一幅512 x 512的图像
    Mat src(512, 512, CV_8UC3);

    // 判断图像是否为空
    if (src.empty())
    {
        cout << "Image empty !\n";
        return 0;
    }
    namedWindow("原图");
    imshow("原图", src);

    
    // 绘制直线
    line(src, Point(10, 10), 
        Point(300, 300), 
        Scalar(255, 0, 255), 3, 8);

    namedWindow("效果图");
    imshow("效果图", src);

    waitKey(0);

    return 0;
}

5、实验结果

效果图

二、绘制圆circle()函数详解

1、函数原型

void circle(InputOutputArray img, 
    Point center, 
    int radius,
    const Scalar& color, 
    int thickness = 1,
    int lineType = LINE_8, 
    int shift = 0);

2、函数功能
在图像上绘制任意的圆;

3、参数详解

线的类型:
FILLED,填充;
LINE_4,4连通的线条;
LINE_8 ,8连通的线条;
LINE_AA ,抗锯齿线条;

4、实验案例

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    // 创建一幅256 x 256的图像
    Mat src(256, 256, CV_8UC3);

    // 判断图像是否为空
    if (src.empty())
    {
        cout << "Image empty !\n";
        return 0;
    }
    namedWindow("原图");
    imshow("原图", src);

    
    // 绘制圆孔
    circle(src, Point(128, 128),
        50,
        Scalar(255, 0, 255), 3, 8);

    namedWindow("效果图");
    imshow("效果图", src);

    waitKey(0);

    return 0;
}

5、实验结果

效果图

三、绘制椭圆ellipse()函数详解

1、函数原型

void ellipse(InputOutputArray img, 
    const RotatedRect& box, 
    const Scalar& color,
    int thickness = 1, 
    int lineType = LINE_8);

void ellipse(InputOutputArray img, 
    Point center, 
    Size axes,
    double angle, 
    double startAngle, 
    double endAngle,
    const Scalar& color, 
    int thickness = 1,
    int lineType = LINE_8, 
    int shift = 0);

2、函数功能
在图像上绘制任意的椭圆;

函数ellipse具有更多的参数,绘制椭圆轮廓、填充椭圆、椭圆弧或填充椭圆扇区;

采用分段线性曲线逼近椭圆圆弧边界;如果您需要更多的控制椭圆渲染,您可以重构曲线使用ellipse2Poly,然后用polylines渲染或着用fillPoly填充;

如果您使用该函数的第二个变体,并且希望绘制整个椭圆,而不是一个弧线,则可以传递start角度=0和endanges=360;

如果起始角大于端角,则交换它们。下图解释了绘制蓝色弧的参数的含义:

椭圆弧参数

3、参数详解

线的类型:
FILLED,填充;
LINE_4,4连通的线条;
LINE_8 ,8连通的线条;
LINE_AA ,抗锯齿线条;

4、实验案例

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

static Scalar randomColor(RNG& rng)
{
    int icolor = (unsigned)rng;
    return Scalar(icolor & 255, 
        (icolor >> 8) & 255, 
        (icolor >> 16) & 255);
}

int main(int argc, char** argv)
{
    const int window_width = 512;
    const int window_height = 512;
    int x_1 = -window_width / 2;
    int x_2 = window_width * 3 / 2;
    int y_1 = -window_width / 2;
    int y_2 = window_width * 3 / 2;

    // 创建一幅256 x 256的图像
    Mat src(window_height, window_width, CV_8UC3);

    char window_name[] = "ellipse";
    RNG rng(0xFFFFFFFF);

    // 判断图像是否为空
    if (src.empty())
    {
        cout << "Image empty !\n";
        return 0;
    }
    namedWindow("原图");
    imshow("原图", src);

    for (int i = 0; i < 100; i++)
    {
        Point center;
        center.x = rng.uniform(x_1, x_2);
        center.y = rng.uniform(y_1, y_2);
        Size axes;
        axes.width = rng.uniform(0, 200);
        axes.height = rng.uniform(0, 200);
        double angle = rng.uniform(0, 180);

        ellipse(src, center, axes, 
            angle, angle - 100, angle + 200,
            randomColor(rng), rng.uniform(-1, 9), 8);

        imshow(window_name, src);
        waitKey(100);
        
    }

    waitKey(0);
    return 0;
}

5、实验结果

效果图

四、绘制矩形rectangle()函数详解

1、函数原型

void rectangle(InputOutputArray img, 
    Point pt1, 
    Point pt2,
    const Scalar& color, 
    int thickness = 1,
    int lineType = LINE_8,
    int shift = 0);

void rectangle(InputOutputArray img, 
    Rect rect,
    const Scalar& color, 
    int thickness = 1,
    int lineType = LINE_8, 
    int shift = 0);

2、函数功能
在图像上绘制任意的矩形;

3、参数详解

线的类型:
FILLED,填充;
LINE_4,4连通的线条;
LINE_8 ,8连通的线条;
LINE_AA ,抗锯齿线条;

4、实验案例

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    // 创建一幅256 x 256的图像
    Mat src(256, 256, CV_8UC3);

    // 判断图像是否为空
    if (src.empty())
    {
        cout << "Image empty !\n";
        return 0;
    }
    namedWindow("原图");
    imshow("原图", src);

    // 绘制矩形
    rectangle(src, Point(128, 128),
        Point(200, 200),
        Scalar(255, 0, 255), 3, 8);

    Rect rect = Rect(Point(50, 50), Point(100, 100));

    rectangle(src, rect,
        Scalar(255, 0, 255), 3, 8);

    namedWindow("效果图");
    imshow("效果图", src);

    waitKey(0);

    return 0;
}

5、实验结果

效果图

我是奕双,现在已经毕业将近两年了,从大学开始学编程,期间学习了C语言编程,C++语言编程,Win32编程,MFC编程,毕业之后进入一家图像处理相关领域的公司,掌握了用OpenCV对图像进行处理,如果大家对相关领域感兴趣的话,可以关注我,我这边会为大家进行解答哦!如果大家需要相关学习资料的话,可以私聊我哦!

上一篇下一篇

猜你喜欢

热点阅读