矩形、圆、椭圆绘图
1.矩形类Rect
Rect类的成员变量有四个:矩形左上角的坐标x,y。矩形的宽和高width 和 height。
Rect rect1(20,50,100,100);
Rect 常见的成员函数有 :
Size() 的返回值为尺寸Size :返回的是 [宽 × 高]
cout<<rect1.size()<<endl;
area() 返回的是矩形的面积;
cout<<rect1.area()<<endl;
contains(Point) 判断点是是否在矩形内部;
rect.contains(point),如果在,返回1,如果不在,返回0
cout<<rect1.contains(c2)<<endl;
tl() 和 br() 分别表示矩形的左上角和右下角的坐标值。
cout<<"左上角坐标="<<rect1.tl()<<endl<<"右上角坐标="<<rect1.br()<<endl;
#include <iostream> #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> //画直线等等函数在imgproc中 #include <opencv2\imgproc\imgproc.hpp> using namespace cv; using namespace std; int main() { //创建一个窗口并且以Tset为标示 namedWindow("Test"); //创建空白的图像 Mat image=Mat::zeros(600,600,CV_8UC3);//zeros使像素值为0,即黑色 //中心点和长短轴 Point c1(100,200); Size ax1(50,100); Point c2(250,400); Size ax2(50,100); Point c3(400,200); Size ax3(50,100); Rect rect1(20,50,100,100); Rect rect2(10,30,80,80); Rect rect3=rect1|rect2; Rect rect4=rect1&rect2; cout<<rect1.size()<<endl; cout<<rect1.area()<<endl; cout<<"左上角坐标="<<rect1.tl()<<endl<<"右上角坐标="<<rect1.br()<<endl; cout<<rect3<<endl; cout<<rect1.contains(c2)<<endl; rectangle(image,c1,c2,Scalar(0,0,255),1,8,0); rectangle(image,rect1,Scalar(255,0,0),1,8,0); rectangle(image,rect2,Scalar(0,255,0),1,8,0); rectangle(image,rect3,Scalar(0,0,255),1,8,0); rectangle(image,rect4,Scalar(255,0,255),1,8,0); circle(image,c1,50,Scalar(255,255,255),1,8,0); ellipse(image,c1,ax1,0,0,360,Scalar(0,255,255),1,8,0); ellipse(image,c2,ax2,30,0,360,Scalar(0,255,255),1,CV_AA,0); ellipse(image,c3,ax3,0,180,360,Scalar(0,255,255),1,4,0); imshow("Test",image); waitKey(0); /*image =0;//直接等于一个数,即改变的是scalar的值 imshow("Test",image); waitKey(0);*/ return 0; }