Opencv 绘制图形函数代码实践汇总

cv::putText()图像上绘制文字

函数原型

void cv::putText(
	 cv::Mat& img, // 待绘制的图像
	 const string& text, // 待绘制的文字
	 cv::Point origin, // 文本框的左下角
	 int fontFace, // 字体 (如cv::FONT_HERSHEY_PLAIN)
	 double fontScale, // 尺寸因子,值越大文字越大
	 cv::Scalar color, // 线条的颜色(RGB)
	 int thickness = 1, // 线条宽度
	 int lineType = 8, // 线型(4邻域或8邻域,默认8邻域)
	 bool bottomLeftOrigin = false // true='origin at lower left'
);

Opencv画点

其实画的是小圆圈

cv::Point pointInterest;//特征点,用以画在图像中
pointInterest.x = keys[i][k].x;//特征点在图像中横坐标
pointInterest.y = keys[i][k].y;//特征点在图像中纵坐标
cv::circle(image, pointInterest, 2, cv::Scalar(0, 0, 255));
//在图像中画出特征点,2是圆的半径

cv.line()在图中画一条直线

函数原型

void cvLine( 
CvArr* img, //要划的线所在的图像
CvPoint pt1, //直线起点
CvPoint pt2, //直线终点 
CvScalar color, //直线的颜色 Scalor(0,0,255)
int thickness=1, //线条粗细
int line_type=8, //8-connected line(8邻接)连接线。
                 //4-connected line(4邻接)连接线。
                 //CV_AA - antialiased 线条。
int shift=0 //坐标点的小数点位数。
);

函数作用
给定一个图像img,连接点pt1和pt2的坐标,在图中画一条直线,color表明线的颜色
其中需要注意的是,点坐标(x, y)中,x代表图片的列,y代表图片的行

函数用法样例
所以如果想在图片中的位置(10, 0) 和 位置(0, 100)之间划线,则正确的调用语句为:

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

using namespace std;
using namespace cv;

int main(int argc, char** argv) {

    Mat srcImage = imread("D:/shenjianxin.png");
    if (!srcImage.data) {
        cout << "could not load image" << endl;
        return -1;
    }

    Point p1 = Point(200, 300);
    Point p2 = Point(400, 300);
    Scalar color = Scalar(0, 0, 255);
    line(srcImage, p1, p2, color, 2, 8, 0);
    imshow("直线绘制", srcImage);

    waitKey(0);
    return 0;
}

实践效果图
在这里插入图片描述

矩形绘制:rectangle()

void rectangle(
CV_IN_OUT Mat& img, // 输出图像
Rect rec, //矩形的位置和长宽
const Scalar& color, // 矩形颜色
int thickness = 1, //线宽
int lineType = LINE_8, //直线类型
int shift = 0 //点坐标的小数点位数
);

实践代码

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

using namespace std;
using namespace cv;

int main(int argc, char** argv) {

    Mat srcImage = imread("D:/shenjianxin.png");
    if (!srcImage.data) {
        cout << "could not load image" << endl;
        return -1;
    }

    Rect rect = Rect(200, 100, 200, 200);
    Scalar color = Scalar(255, 0, 0);
    rectangle(srcImage, rect, color, 2, LINE_8);
    imshow("矩形绘制", srcImage);

    waitKey(0);
    return 0;
}

效果图
在这里插入图片描述

圆形绘制:circle()

函数原型

void circle(
InputOutputArray img, //图像
Point center, // 圆心
int radius, // 半径
const Scalar& color, // 颜色
int thickness = 1, // 线宽
int lineType = LINE_8, // 线型
int shift = 0 // 坐标点的小数点位数
);

实践代码

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

using namespace std;
using namespace cv;

int main(int argc, char** argv) {

    Mat srcImage = imread("D:/shenjianxin.png");
    if (!srcImage.data) {
        cout << "could not load image" << endl;
        return -1;
    }

    Scalar color = Scalar(0, 0, 255);
    Point center = Point(srcImage.cols / 2, srcImage.rows / 2);
    circle(srcImage, center, 99, color, 2, 8);
    imshow("圆形绘制", srcImage);

    waitKey(0);
    return 0;
}

效果图
在这里插入图片描述

椭圆绘制:ellipse()

函数原型

void ellipse(
InputOutputArray img, // 图像
Point center, // 椭圆原心
Size axes, // 椭圆x轴长度的一半,y轴长度的一半
double angle, // 椭圆旋转角度
double startAngle, // 起始角度
double endAngle, // 终止角度
const Scalar& color, // 椭圆颜色
int thickness = 1, //线宽
int lineType = LINE_8, //线型
int shift = 0); //坐标小数点位数

实践代码

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

using namespace std;
using namespace cv;

int main(int argc, char** argv) {

    Mat srcImage = imread("D:/shenjianxin.png");
    if (!srcImage.data) {
        cout << "could not load image" << endl;
        return -1;
    }

    Scalar color = Scalar(0, 255, 0);
    ellipse(srcImage, Point(srcImage.cols / 2, srcImage.rows / 2), Size(srcImage.cols / 4, srcImage.rows / 4), 0, 0, 360, color, 2, LINE_8);
    imshow("椭圆绘制", srcImage);

    waitKey(0);
    return 0;
}

效果图
在这里插入图片描述

resize缩小或者放大图像至某一个大小

函数功能:

resize(
InputArray src,  // 输入,原图像,即待改变大小的图像
OutputArray dst, // 输出,改变后的图像。这个图像和原图像具有相同的内容,只是大小和原图像不一样而已;
Size dsize, // 输出图像的大小。
// 如果这个参数不为0,那么就代表将原图像缩放到这个Size(width,height)指定的大小;
// 如果这个参数为0,那么原图像缩放之后的大小就要通过下面的公式来计算:
// dsize = Size(round(fxsrc.cols), round(fysrc.rows))
//其中,fx和fy就是下面要说的两个参数,是图像width方向和height方向的缩放比例。
//fx:width方向的缩放比例,如果它是0,那么它就会按照(double)dsize.width/src.cols来计算;
//fy:height方向的缩放比例,如果它是0,那么它就会按照(double)dsize.height src.rows来计算;
double fx=0, 
double fy=0, 
int interpolation=INTER_LINEAR 
//这个是指定插值的方式,图像缩放之后,肯定像素要进行重新计算的,就靠这个参数来指定重新计算像素的方式,有以下几种:
//INTER_NEAREST - 最邻近插值
//INTER_LINEAR - 双线性插值,如果最后一个参数你不指定,默认使用这种方法
//INTER_AREA - resampling using pixel area relation. It may be a preferred //method for image decimation, as it gives moire’-free results. But when //the image is zoomed, it is similar to the INTER_NEAREST method.
//INTER_CUBIC - 4x4像素邻域内的双立方插值
//INTER_LANCZOS4 - 8x8像素邻域内的Lanczos插值
)
cv::resize(rightimg,canvaspart,cv::Size(640,480),0,0,cv::INTER_LINEAR);

在这里插入图片描述加粗样式

cv::Rect类

Rect矩形类包括Point点类的成员x和y(表示矩形的左上角)以及size类的成员width和height(表示矩形的大小)。但是,矩形类不会从Point点类或size类继承,因此通常不会从它们继承操作符。

Opencv Rect_类数据结构详解

Opencv Rect_模板类是一个比较重要的类,可以设置图像ROI区域,截取图像等。

Rect_类的成员变量有x,y,width,height,分别为左上角座标和矩形宽高。常用函数有Size()返回Size;

area()返回矩形面积;

contains(Point)判断点是否在矩形内;

inside(Rect)判断矩形是否在该矩形内;

tl()返回左上角点座标;

br()返回右下角点座标;

Mat image = imread("C:\\Users\\Leo\\Desktop\\lena.jpg");
Rect rect1(256, 256, 128, 128);
Rect rect2(224, 224, 128, 128);
 
Mat roi1;
image(rect1).copyTo(roi1); // copy the region rect1 from the image to roi1
imshow("1", roi1);
waitKey(0);
 
Mat roi2;
image(rect2).copyTo(roi2); // copy the region rect2 from the image to roi2
imshow("2", roi2);
waitKey(0);
 
cv::Rect rect3 = rect1&rect2; // intersection of the two sets
Mat roi3;
image(rect3).copyTo(roi3);
imshow("3", roi3);
waitKey(0);
 
Rect rect4 = rect1|rect2; // union of the two sets (the minimum bounding rectangle)
Mat roi4;
image(rect4).copyTo(roi4);
imshow("4", roi4);
waitKey(0);
 
Rect rect5(10, 10, 128, 128);
roi1.copyTo(image(rect5)); // copy the region rect1 to the designated region in the image
imshow("5", image);
waitKey(0);

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_21950671/article/details/107379285