Opencv3学习笔记(C++&Python双语)---绘图函数

涉及的函数:cv2.line() , cv2.cicle() , cv2.rectangle() , cv2.ellipse() , cv2.putText()等
需要设置的参数:
img 你想要绘制的图形的那副图像
color 形状的颜色,以RGB为例,需要传入的元组,例(255,0,0)代表蓝色,对于灰度图只需传入灰度值
thickness 线条的粗细,如果给一个闭合图形设置为-1,那么这个图形就会被填充,默认值为1
linetype 线条的类型,8连接,4锯齿等。默认是8连接。cv2.LINE_AA为抗锯齿;

具体函数内容参考教程《学习Opencv3》第6章;

1.画直线

python代码

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = np.zeros((512,512,3),np.uint8)
img=cv.line(img,(0,0),(522,511),(255,0,0),5)

cv.namedWindow("image",cv.WINDOW_NORMAL)
cv.resizeWindow('image',1000,1000)#定义frame的大小
cv.imshow('image',img)
cv.waitKey(0)
cv.destroyAllWindows()

C++代码

#include
#include
#include
#include

using namespace std;

int main()
{
	cv::Mat img;
	img.create(512, 512,CV_32FC3);
	img.setTo(cv::Scalar(0., 0., 0));

	cv::line(img, cv::Point(0, 0), cv::Point(500,511), cv::Scalar(255, 0, 0), 5);
	cv::namedWindow("image", CV_WINDOW_NORMAL);
	cv::resizeWindow("image", 1000, 1000);

	cv::imshow("image", img);
	cv::waitKey(0);
	cv::destroyAllWindows();
	return 0;
}

2.画矩形

python代码

img = np.zeros((512,512,3),np.uint8)

cv.rectangle(img,(350,0),(500,128),(0,255,0),3)

cv.namedWindow('image',cv.WINDOW_NORMAL)
cv.resizeWindow('image',1000,1000)#定义frame的大小
cv.imshow('image',img)
cv.waitKey(0)
cv.destroyAllWindows()

C++代码

#include
#include
#include
#include

using namespace std;

int main()
{
	cv::Mat img;
	img.create(512, 512, CV_32FC3);
	img.setTo(cv::Scalar(0., 0., 0));

	cv::rectangle(img, cv::Point(350, 0), cv::Point(500,128), cv::Scalar(255, 0, 0), 5);
	cv::namedWindow("image", CV_WINDOW_NORMAL);
	cv::resizeWindow("image", 1000, 1000);

	cv::imshow("image", img);
	cv::waitKey(0);
	cv::destroyAllWindows();
	return 0;
}

3.画圆

python代码

img = np.zeros((512,512,3),np.uint8)

cv.circle(img,(256,256),240,(0,255,0),3)

cv.namedWindow('image',cv.WINDOW_NORMAL)
cv.resizeWindow('image',1000,1000)#定义frame的大小
cv.imshow('image',img)
cv.waitKey(0)
cv.destroyAllWindows()

C++代码

#include
#include
#include
#include

using namespace std;

int main()
{
	cv::Mat img;
	img.create(512, 512, CV_32FC3);
	img.setTo(cv::Scalar(0., 0., 0.));

	cv::namedWindow("image", CV_WINDOW_NORMAL);
	cv::circle(img,cv::Point(256, 256), 240, cv::Scalar(255, 0, 0), 5);
	cv::imshow("image", img);
	cv::waitKey(0);
	return 0;
}

4.画椭圆

python代码

img = np.zeros((512,512,3),np.uint8)

cv.ellipse(img,(256,256),(150,60),30,0,360,(0,255,0),3)

cv.namedWindow('image',cv.WINDOW_NORMAL)
cv.resizeWindow('image',1000,1000)
cv.imshow('image',img)
cv.waitKey(0)
cv.destroyAllWindows()

C++代码

#include
#include
#include
#include

using namespace std;

int main()
{
	cv::Mat img;
	img.create(512, 512, CV_32FC3);
	img.setTo(cv::Scalar(0., 0., 0.));

	cv::namedWindow("image", CV_WINDOW_NORMAL);
	cv::ellipse (img, cv::Point(256, 256), cv::Size(150, 60), 360, 0, 360, cv::Scalar(255, 0, 0), 5);
	cv::imshow("image", img);
	cv::waitKey(0);

	return 0;
}

5.画多边形

python代码


img = np.zeros((512,512,3),np.uint8)

pts=np.array([[10,5],[20,30],[70,20],[50,10]],np.int32)
pts = pts.reshape((-1,1,2))

cv.polylines(img,[pts],True,(0,255,255),3)

cv.namedWindow('image',cv.WINDOW_NORMAL)
cv.resizeWindow('image',1000,1000)
cv.imshow('image',img)
cv.waitKey(0)
cv.destroyAllWindows()

C++代码

#include
#include
#include
#include

using namespace std;

int main()
{
	cv::Mat img;
	img.create(512, 512, CV_32FC3);
	img.setTo(cv::Scalar(0., 0., 0.));

	cv::Point points[1][4];
	points[0][0] = cv::Point(10,5);
	points[0][1] = cv::Point(20,30);
	points[0][2] = cv::Point(70,20);
	points[0][3] = cv::Point(50,10);
	const cv::Point * ppt[1] = { points[0] };
	int npt[] = { 4 };

	cv::namedWindow("image", CV_WINDOW_AUTOSIZE);
	cv::polylines(img, ppt,npt, 1, true, cv::Scalar(255, 0, 0));

	cv::imshow("image", img);
	cv::waitKey(0);

	return 0;

}

6.绘制文字

python代码

img = np.zeros((512,512,3),np.uint8)
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img,"OpenCV",(0,256),font,4,(255,255,255),2,cv.LINE_AA)

cv.namedWindow("image",cv.WINDOW_NORMAL)
cv.resizeWindow("image",1000,1000)
cv.imshow("image",img)
cv.waitKey(0)
cv.destroyAllWindows()

C++代码

#include
#include
#include
#include

using namespace std;

int main()
{
	cv::Mat img;
	img.create(512, 512, CV_32FC3);
	img.setTo(cv::Scalar(0., 0., 0.));

	cv::Size ps(0, 256);
	string word = "OpenCV";
	cv::putText(img, word, ps, cv::FONT_HERSHEY_SCRIPT_SIMPLEX,4,
		cv::Scalar(255, 255, 0));
	
	cv::namedWindow("image", CV_WINDOW_NORMAL);
	cv::imshow("image", img);
	cv::waitKey(0);
	return 0;
}

 

你可能感兴趣的:(c++,opencv,python,计算机视觉)