OpenCv 015---图形绘制

Posted vince-wu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenCv 015---图形绘制相关的知识,希望对你有一定的参考价值。

1 前备知识

null

2 所用到的主要OpenCv API

/** @overload
use `rec` parameter as alternative specification of the drawn rectangle: `r.tl() and
r.br()-Point(1,1)` are opposite corners
*/
CV_EXPORTS void rectangle(CV_IN_OUT Mat& img, Rect rec,
                          const Scalar& color, int thickness = 1,
                          int lineType = LINE_8, int shift = 0);
/** @brief Draws a circle.
@param img Image where the circle is drawn.
@param center Center of the circle.
@param radius Radius of the circle.
@param color Circle color.
@param thickness Thickness of the circle outline, if positive. Negative values, like #FILLED,
mean that a filled circle is to be drawn.
@param lineType Type of the circle boundary. See #LineTypes
@param shift Number of fractional bits in the coordinates of the center and in the radius value.
 */
CV_EXPORTS_W void circle(InputOutputArray img, Point center, int radius,
                       const Scalar& color, int thickness = 1,
                       int lineType = LINE_8, int shift = 0);
/** @brief Draws a simple or thick elliptic arc or fills an ellipse sector.
@param img Image.
@param center Center of the ellipse.
@param axes Half of the size of the ellipse main axes.
@param angle Ellipse rotation angle in degrees.
@param startAngle Starting angle of the elliptic arc in degrees.
@param endAngle Ending angle of the elliptic arc in degrees.
@param color Ellipse color.
@param thickness Thickness of the ellipse arc outline, if positive. Otherwise, this indicates that
a filled ellipse sector is to be drawn.
@param lineType Type of the ellipse boundary. See #LineTypes
@param shift Number of fractional bits in the coordinates of the center and values of axes.
 */
CV_EXPORTS_W 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);

3 程序代码

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

using namespace cv;
using namespace std;

int main(int argc, char** argv) 
    Mat image = Mat::zeros(Size(512, 512), CV_8UC3);
    Rect rect(100, 100, 200, 200);
    rectangle(image, rect, Scalar(255, 0, 0), 2, LINE_8, 0);
    circle(image, Point(256, 256), 50, Scalar(0, 0, 255), 2, LINE_8, 0);
    ellipse(image, Point(256, 256), Size(150, 50), 360, 0, 360, Scalar(0, 255, 0), 2, LINE_8, 0);
    imshow("image", image);
    waitKey(0);

    RNG rng(0xFFFFFF);
    image.setTo(Scalar(0, 0, 0));

    for (int i = 0; i < 100000; i++) 
        // image.setTo(Scalar(0, 0, 0));
        int x1 = rng.uniform(0, 512);
        int y1 = rng.uniform(0, 512);
        int x2 = rng.uniform(0, 512);
        int y2 = rng.uniform(0, 512);

        int b = rng.uniform(0, 256);
        int g = rng.uniform(0, 256);
        int r = rng.uniform(0, 256);
        line(image, Point(x1, y1), Point(x2, y2), Scalar(b, g, r), 1, LINE_AA, 0);
        rect.x = x1;
        rect.y = y1;
        rect.width = x2 - x1;
        rect.height = y2 - y1;
        // rectangle(image, rect, Scalar(b, g, r), 1, LINE_AA, 0);
        imshow("image", image);
        char c = waitKey(20);
        if (c == 27)
            break;

        imshow("image", image);
    
    waitKey(0);
    return 0;

4 运行结果

技术图片

5 扩展及注意事项

null

以上是关于OpenCv 015---图形绘制的主要内容,如果未能解决你的问题,请参考以下文章

OpenCV-Python实战(番外篇)——OpenCV中利用鼠标事件动态绘制图形

OpenCV基础---图形绘制

python-openCV 绘制图形

OpenCV 绘制图形

opencv:图形绘制与填充

OpenCV-Python实战——OpenCV中绘制图形与文本(万字总结,️❤️建议收藏️❤️)