OpenCV draws dots, draws lines, draws frames and writes

Draw dots, draw lines, draw frames, write operations

When using OpenCV, it is often necessary to use operations such as drawing dots, drawing lines, drawing frames, writing, etc. Here is a demonstration of the methods of these operations

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>

int main() {
    
    
//几种常见的颜色
    cv::Scalar color_red(47, 0, 255);
    cv::Scalar color_green(47, 225, 0);
    cv::Scalar color_blue(225, 47, 0);
    cv::Scalar color_white(255, 255, 255);
    int x = 300;
    int y = 300;
    int x1 = 500;
    int y1 = 500;
    int x2 = 1000;
    int y2 = 1000;
    int x3 = 800;
    int y3 = 800;
//创建一张黑色底图
    cv::Mat image = cv::Mat::zeros(1080, 1920, CV_8UC3);
//在 x,y 位置写字,大小0.8,粗细2,颜色为蓝色
    cv::putText(image, "hello world", cv::Point(x, y), cv::FONT_HERSHEY_SIMPLEX, 0.8, color_blue, 2);
//在 x1,y1 和 x2,y2 之间画一条线,颜色白色,粗细为1
    cv::line(image, cv::Point(x1, y1), cv::Point(x2, y2), color_white, 1);
//在 x,y 处画圆圈,半径为1,颜色红色,粗细为2,粗细大于半径,看上去就是一个实心点
    cv::circle(image, cv::Point(x, y), 1, color_red, 2);
//x1,y1和x3,y3为左上角点和右下角点,画一个粗细为1的矩形框,绿色
    cv::rectangle(image, cv::Point(x1, y1), cv::Point(x3, y3), color_green, 1);
//显示图片
    cv::imshow("draws", image);
    cv::waitKey(0);
    return 0;
}

The corresponding CmakeLists.txt is as follows:

cmake_minimum_required(VERSION 2.8.4)
project(draw)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(draw main.cpp)
target_link_libraries(draw ${OpenCV_LIBS})

insert image description here

Guess you like

Origin blog.csdn.net/TU_Dresden/article/details/126170587