目的:
使用cv::Point与cv::Scalar
绘制线、矩形、园、椭圆等基本几何形状
绘制点和圆
void cvCircle( CvArr* img, CvPoint center, int radius, CvScalar color,
int thickness=1, int line_type=8, int shift=0 );
img:图像。
center:圆心坐标。
radius:圆形的半径。
color:线条的颜色。
thickness:如果是正数,表示组成圆的线条的粗细程度。否则,表示圆是否被填充。
line_type:线条的类型。见 cvLine 的描述
shift:圆心坐标点和半径值的小数点位数。
画圆画点都是使用circle()函数来画,点就是圆,我们平常所说的圆只不过是半径大一点而已。
绘制椭圆
void ellipse(Mat& img, Point center, Size axes, double angle, double startAngle, double endAngle,
const Scalar& color, int thickness = 1, int lineType = 8, int shift = 0)
center:椭圆圆心坐标。
axes:轴的长度。
angle:偏转的角度。
start_angle:圆弧起始角的角度。
end_angle:圆弧终结角的角度。
color:线条的颜色。
thickness:线条的粗细程度。
line_type:线条的类型,见CVLINE的描述。
shift:圆心坐标点和数轴的精度。
绘制矩形
void rectangle(Mat& img,Rect rec, const Scalar&color, intthickness=1, intlineType=8,intshift=0 )
img:图像。
rec:表征矩形的位置和长宽。
color:线条颜色 (RGB) 或亮度(灰度图像 )(grayscale image)。
thickness:组成矩形的线条的粗细程度。取负值时(如CV_FILLED)函数绘制填充了色彩的矩形。
line_type:线条的类型。见cvLine的描述
shift:坐标点的小数点位数。
绘制直线
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,
int thickness = 1, int lineType = LINE_8, int shift = 0);
img:图像.
pt1:线条起点.
pt2:线条终点.
color:线条颜色.
thickness:线条宽度.
lineType:线型
cv::RNG
生成高斯随机数gaussian (double sigma)
生成正态分布随机数uniform (int a, int b)
代码:
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat src;
src = imread("D:/opencvSRC/test.jpg");
if (src.empty()) {
printf("load image error!\n");
}
namedWindow("src", CV_WINDOW_AUTOSIZE);
//画空心点
Point p(20, 20);//初始化点坐标为(20,20)
circle(src, p, 2, Scalar(0, 255, 0)); //第三个参数表示点的半径,第四个参数选择颜色。这样子我们就画出了绿色的空心点
//这种初始化点的方式也可以
Point p2;
p2.x = 100;
p2.y = 100;
//画实心点
circle(src, p2, 3, Scalar(255, 0, 0), -1); //第五个参数我设为-1,表明这是个实点。
//画空心圆
Point p3(300, 300);
circle(src, p3, 100, Scalar(0, 0, 255), 3);//第五个参数我们调高点,让线更粗
//画实心圆
Point p4;
p4.x = 600;
p4.y = 600;
circle(src, p4, 100, Scalar(120, 120, 120), -1);
int thickness = 3;
int lineType = 8;
double angle = 30; //椭圆旋转角度
//第三个参数Size中的两个参数分别是横轴长、纵轴长。
//同理,thickness若是小于0,表示实心
ellipse(src, Point(100, 100), Size(90, 60), angle, 0, 300, Scalar(255, 255, 0), thickness, lineType);
//矩形
Rect r(250, 250, 120, 200);
rectangle(src, r, Scalar(0, 255, 255), 3);
Point p5(100, 100);
Point p6(758, 50);
line(src, p5, p6, Scalar(33, 33, 133), 2);
//画第二条线
line(src, Point(300, 300), Point(758, 300), Scalar(89, 90, 90), 3);
imshow("src", src);
RNG rng(12345);
Point pt1;
Point pt2;
Mat bg = Mat::zeros(src.size(), src.type());
namedWindow("randomlinedemo", CV_WINDOW_AUTOSIZE);
for (int i = 0; i < 1000; i++) {
pt1.x = rng.uniform(0, src.cols);
pt2.x = rng.uniform(0, src.cols);
pt1.y = rng.uniform(0, src.rows);
pt2.y = rng.uniform(0, src.rows);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
if (waitKey(50) > 0) {
break;
}
line(bg, pt1, pt2, color, rng.uniform(0, 20), 8);
imshow("randomlinedemo", bg);
}
waitKey(0);
return 0;
}