1、直线
2、圆形
3、椭圆
4、矩形
5、多边形
6、文字
//图形绘制
void test1()
{
Mat img = Mat::zeros(Size(512, 512), CV_8UC3);//生成一个黑色图像用于绘制几何图形
//绘制圆形
circle(img, Point(50, 50), 25, Scalar(255, 255, 255), -1);//绘制一个实心圆
circle(img, Point(100, 50), 20, Scalar(255, 255, 255), 4);//绘制一个空心圆
//绘制直线
line(img, Point(100, 100), Point(200, 100), Scalar(255, 255, 255), 2, LINE_4, 0);//绘制一条直线
//绘制椭圆
ellipse(img, Point(300, 255), Size(100, 70), 0, 0, 100, Scalar(255, 255, 255), -1);//绘制实心椭圆的
//绘制矩形
rectangle(img, Point(50, 400), Point(100, 450), Scalar(125, 125, 125), -1);
//绘制多边形
Point pp[2][6];
pp[0][0] = Point(72, 200);
pp[0][1] = Point(142, 204);
pp[0][2] = Point(226, 263);
pp[0][3] = Point(172, 310);
pp[0][4] = Point(117, 319);
pp[0][5] = Point(15, 260);
pp[1][0] = Point(359, 339);
pp[1][1] = Point(447, 351);
pp[1][2] = Point(504, 349);
pp[1][3] = Point(484, 433);
pp[1][4] = Point(418, 449);
pp[1][5] = Point(354, 402);
Point pp2[5];
pp2[0] = Point(350, 83);
pp2[1] = Point(463, 90);
pp2[2] = Point(500, 171);
pp2[3] = Point(421, 194);
pp2[4] = Point(338, 141);
const Point* pts[3] = { pp[0], pp[1], pp2 }; //pts变量的生成
int npts[] = { 6, 6, 5 }; //顶点个数数组的生成
fillPoly(img, pts, npts, 3, Scalar(125, 125, 125), 8);//绘制3个多边形
//生成文字
putText(img, "Learn OpenCV 4", Point(100, 400), 2, 1, Scalar(255, 255, 255));
imshow("", img);
waitKey(0);
}
7、ROI区域截取
//ROI区域截取
int test2()
{
Mat img = imread("F:/testMap/lena.png");
Mat noobev = imread("F:/testMap/OpenCV.png");
if (img.empty() || noobev.empty())
{
cout << "请确认图像文件名称是否正确" << endl;
return -1;
}
Mat ROI1,ROI2,ROI2_copy,mask,img2,img_copy;
resize(noobev, mask, Size(200, 200));
img2 = img;//浅拷贝
//深拷贝的两种方式
img.copyTo(img_copy);
//两种在图中截取ROI区域的方式
Rect rect(206,206,200,200);//定义ROI区域
ROI1 = img(rect);//截图
ROI2 = img(Range(300,500),Range(300,500));//第二种截图方式
img(Range(300,500),Range(300,500)).copyTo(ROI2_copy);//深拷贝
mask.copyTo(ROI1);//在图像中加入部分图像
imshow("加入noobcv后图像",img) ;
imshow("深拷贝的img_copy", img_copy);
imshow("ROI对ROI2的影响",ROI2);
imshow("深拷贝的ROI2_copy", ROI2_copy);
circle(img,Point(300,300),20,Scalar(0,0,255),-1);//绘制一个圆形
imshow("浅拷贝的img2",img2);
imshow("画圆对ROI1的影响",ROI1);
waitKey(0);
}
8、高斯三角形
8.1 原理
8.2 实现
//高斯图像金字塔
void test3()
{
Mat img = imread("F:/testMap/lena.png");
vector<Mat> Guass;
int level = 3;
Guass.push_back(img);
for (int i = 0; i < level; i++)
{
Mat guass;
pyrDown(Guass[i], guass);
Guass.push_back(guass);
}
for (int i = 0; i < level; i++)
{
string name = to_string(i);
imshow(name, Guass[i]);
}
waitKey(0);
}