OpenCV中的绘图用法

0.概述

本文档简单记录OpenCV中不同图形的绘制.

1.绘图

1.1 线条绘制

line(img, pt1, pt2, color, thickness, lineType, shift)

  • img:这是图像文件。
  • pt1:线段的起点。线段两端的第一个点。它是两个坐标(x 坐标、y 坐标))的元组。
  • pt2:线段的终点。线段两端的第二个点。它是两个坐标(x 坐标、y 坐标))的元组。
  • color要绘制的线的颜色。它是一个代表 3 种颜色(B、G、R)的元组,即(蓝色、绿色、红色)。
  • thickness绘制的线的粗细。
  • lineType:行的类型。有 3 种类型的线路:
    • LINE_4:使用 4 个连接的 Bresenham 算法绘制线条。
    • LINE_8:使用 8 个连接的 Bresenham 算法绘制的线。
    • LINE_AA:它绘制使用高斯滤波器形成的抗锯齿线。
  • nshift:它是点坐标中的小数位数

1)在自定义的背景上绘制线条

// C++ program for the above approach 
#include <iostream> 
#include <opencv2/core/core.hpp> 

// Library to include for 
// drawing shapes 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc.hpp> 
using namespace cv; 
using namespace std; 

// Driver Code 
int main(int argc, char** argv) 
{ 
	// Create a blank image of size 
	// (500 x 500) with black 
	// background (B, G, R) : (0, 0, 0) 
	Mat image(500, 500, CV_8UC3, 
			Scalar(0, 0, 0)); 

	// Check if the image is created 
	// successfully 
	if (!image.data) { 
		cout << "Could not open or find"
			<< " the image"; 

		return 0; 
	} 

	Point p1(0, 0), p2(100, 0); 
	Point p3(200, 0), p4(500, 500); 
	int thickness = 2; 

	// Line drawn using 8 connected 
	// Bresenham algorithm 
	line(image, p1, p4, Scalar(255, 0, 0), 
		thickness, LINE_8); 

	// Line drawn using 4 connected 
	// Bresenham algorithm 
	line(image, p2, p4, Scalar(0, 255, 0), 
		thickness, LINE_4); 

	// Antialiased line 
	line(image, p3, p4, Scalar(0, 0, 255), 
		thickness, LINE_AA); 

	// Show our image inside window 
	imshow("Output", image); 
	waitKey(0); 

	return 0; 
} 

2)在加载的图片上绘制线条

// C++ program for the above approach 
#include <iostream> 
#include <opencv2/core/core.hpp> 

// Library to include for 
// drawing shapes 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc.hpp> 
using namespace cv; 
using namespace std; 

// Driver code 
int main(int argc, char** argv) 
{ 
	// Path of the image file 
	Mat image = imread( 
		"C:/Users/harsh/Downloads/geeks.png", 
		IMREAD_COLOR); 

	// Check if the image is loaded 
	// successfully 
	if (!image.data) { 
		std::cout << "Could not open or "
					"find the image"; 
		return 0; 
	} 

	Point p1(0, 0), p2(250, 250); 
	int thickness = 2; 

	// Line drawn using 8 connected 
	// Bresenham algorithm 
	line(image, p1, p2, Scalar(255, 0, 0), 
		thickness, LINE_8); 

	// Show our image inside window 
	imshow("Output", image); 
	waitKey(0); 

	return 0; 
} 

 

1.2 椭圆绘制

ellipse(image, centerCoordinates, axesLength, angle, startAngle, endAngle, color, thickness, lineType, shift)

  • image它是要绘制椭圆的图像。
  • centerCoordinates: 椭圆中心的坐标。(两个坐标的元组(X 坐标、Y 坐标))
  • axesLength包含椭圆的长轴和短轴(长轴长度、短轴长度)的元组。
  • angle椭圆旋转角度(以度为单位)。
  • startAngle: 椭圆弧的起始角度(度)。
  • endAngle:椭圆弧的结束角(度)。
  • color它是要绘制的椭圆边界线的颜色。表示 3 种颜色(B、G、R)的元组,即(蓝色、绿色、红色)。
  • thickness它是椭圆边界线的粗细,单位为 px-1 px 的厚度将按指定的颜色填充椭圆形状。
  • lineType:行的类型。有 3 种类型的线路:
    • LINE_4:使用 4 个连接的 Bresenham 算法绘制线条。
    • LINE_8:使用 8 个连接的 Bresenham 算法绘制线条。
    • LINE_AA:它使用高斯滤波器绘制抗锯齿线。
  • shift点坐标中的小数位数。

a) 自形成的背景图像上绘制椭圆

// C++ program to demonstrating ellipse 
// over a self-formed background image 
#include <iostream> 
#include <opencv2/core/core.hpp> 

// Drawing shapes 
#include <opencv2/imgproc.hpp> 

#include <opencv2/highgui/highgui.hpp> 
using namespace cv; 
using namespace std; 

// Driver Code 
int main(int argc, char** argv) 
{ 
	// Creating a blank image with 
	// white background 
	Mat image(500, 500, CV_8UC3, 
			Scalar(255, 255, 255)); 

	// Check if the image is created 
	// successfully or not 
	if (!image.data) { 
		std::cout << "Could not open or "
				<< "find the image\n"; 

		return 0; 
	} 

	// Drawing the ellipse 
	ellipse(image, Point(256, 256), 
			Size(100, 50), 0, 0, 
			360, Scalar(0, 255, 255), 
			-1, LINE_AA); 

	// Showing image inside a window 
	imshow("Output", image); 
	waitKey(0); 

	return 0; 
} 

2)加载图像上的矩形,GFG 徽标周围绘制一个椭圆

// C++ program to demonstrate rectangle 
// over a loaded image with an ellipse 
// around the GFG logo 
#include <iostream> 
#include <opencv2/core/core.hpp> 

// Drawing shapes 
#include <opencv2/imgproc.hpp> 

#include <opencv2/highgui/highgui.hpp> 
using namespace cv; 
using namespace std; 

// Driver Code 
int main(int argc, char** argv) 
{ 
	// Reading the Image 
	Mat image = imread("C:/Users/harsh/Downloads/geeks.png", 
					IMREAD_COLOR); 

	// Check if the image is created 
	// successfully or not 
	if (!image.data) { 
		std::cout << "Could not open or "
				<< "find the image\n"; 
		return 0; 
	} 

	// Drawing the ellipse 
	ellipse(image, Point(115, 110), 
			Size(105, 55), 0, 0, 
			360, Scalar(0, 255, 255), 
			1, LINE_AA); 

	// Show our image inside a window 
	imshow("Output", image); 
	waitKey(0); 

	return 0; 
} 

1.3 矩形绘制

rectangle( img, pt1, pt2, color, thickness, line Type, shift)

 

  • img它是要在其上绘制矩形的图像。
  • 开始(pt1):它是矩形的左上角,表示为两个坐标的元组,即(x-坐标,y-坐标)。
  • 结束(pt2):它是矩形的右下角,表示为两个坐标的元组,即(x-坐标,y-坐标)。
  • color它是要绘制的矩形边界线的颜色。表示 3 种颜色(B、G、R)的元组,即(蓝色、绿色、红色)。
  • thickness它是矩形边界线的粗细,单位为 pxpx 为-1的厚度将按指定的颜色填充矩形形状。
  • lineType:行的类型。有 3 种类型的线路:
    • LINE_4:使用 4 个连接的 Bresenham 算法绘制线条。
    • LINE_8:使用 8 个连接的 Bresenham 算法绘制线条。
    • LINE_AA:它使用高斯滤波器绘制抗锯齿线。
  • shift:点坐标中的小数位数。

 1) 自形成的背景图像上绘制矩形

// C++ program to demonstrate rectangle 
// over a self-formed background image 

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

// Drawing shapes 
#include <opencv2/imgproc.hpp> 

#include <opencv2/highgui/highgui.hpp> 
using namespace cv; 
using namespace std; 

// Driver Code 
int main(int argc, char** argv) 
{ 
	// Creating a blank image with 
	// white background 
	Mat image(500, 500, CV_8UC3, 
			Scalar(255, 255, 255)); 

	// Check if the image is created 
	// successfully or not 
	if (!image.data) { 
		std::cout << "Could not open or "
				<< "find the image\n"; 

		return 0; 
	} 

	// Top Left Corner 
	Point p1(30, 30); 

	// Bottom Right Corner 
	Point p2(255, 255); 

	int thickness = 2; 

	// Drawing the Rectangle 
	rectangle(image, p1, p2, 
			Scalar(255, 0, 0), 
			thickness, LINE_8); 

	// Show our image inside a window 
	imshow("Output", image); 
	waitKey(0); 

	return 0; 
} 

 

2)在logo外绘制矩形

// C++ program to demonstrate rectangle 
// over a loaded image of GFG logo 

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

// Drawing shapes 
#include <opencv2/imgproc.hpp> 

#include <opencv2/highgui/highgui.hpp> 
using namespace cv; 
using namespace std; 

// Driver Code 
int main(int argc, char** argv) 
{ 
	// Reading the Image 
	Mat image = imread("C:/Users/harsh/Downloads/geeks.png", 
					IMREAD_COLOR); 

	// Check if the image is created 
	// successfully or not 
	if (!image.data) { 
		std::cout << "Could not open or "
				<< "find the image\n"; 
		return 0; 
	} 

	// Top Left Coordinates 
	Point p1(30, 70); 

	// Bottom Right Coordinates 
	Point p2(115, 155); 

	int thickness = 2; 

	// Drawing the Rectangle 
	rectangle(image, p1, p2, 
			Scalar(255, 0, 0), 
			thickness, LINE_8); 

	// Show our image inside a window 
	imshow("Output", image); 
	waitKey(0); 

	return 0; 
}

 

3)绘制充满颜色的矩形

// C++ program to demonstrate rectangle 
// filled with any color 

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

// Drawing shapes 
#include <opencv2/imgproc.hpp> 

#include <opencv2/highgui/highgui.hpp> 
using namespace cv; 
using namespace std; 

// Driver Code 
int main(int argc, char** argv) 
{ 
	// Creating a blank image with 
	// white background 
	Mat image(500, 500, CV_8UC3, 
			Scalar(255, 255, 255)); 

	// Check if the image is created 
	// successfully or not 
	if (!image.data) { 
		std::cout << "Could not open or "
				<< "find the image\n"; 

		return 0; 
	} 

	// Top Left Corner 
	Point p1(30, 30); 

	// Bottom Right Corner 
	Point p2(255, 255); 

	int thickness = -1; 

	// Drawing the Rectangle 
	rectangle(image, p1, p2, 
			Scalar(0, 255, 0), 
			thickness, LINE_8); 

	// Show our image inside a window 
	imshow("Output", image); 
	waitKey(0); 

	return 0; 
} 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

scott198512

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值