画点 画线 画框 写字操作
在使用OpenCV的时候经常会需要用到画点画线画框写字等等操作,这里演示一下这些操作的方法
#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 之间画一条线,颜色白色,粗细为1cv::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;
}
对应的CmakeLists.txt如下:
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})