opencv提供了line()函数来对直线的绘制。其原型如下:
void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
参数:
img: 要绘制线段的图像。
pt1: 线段的起点。
pt2: 线段的终点。
color: 线段的颜色,通过一个Scalar对象定义。
thickness: 线条的宽度。
lineType: 线段的类型。可以取值8, 4, 和CV_AA, 分别代表8邻接连接线,4邻接连接线和反锯齿连接线。默认值为8邻接。为了获得更好地效果可以选用CV_AA(采用了高斯滤波)。
shift: 坐标点小数点位数。
示例代码:
-
#include <iostream>
-
#include <stdio.h>
-
#include <opencv2/opencv.hpp>
-
using namespace std;
-
using namespace cv;
-
int main()
-
{
-
Mat src=imread("A.jpg");
-
line(src,Point(1,1),Point(250,250),Scalar(0,0,255),5,CV_AA);
-
imwrite("src.jpg",src);
-
imshow("A",src);
-
printf("channel: %d",src.channels());
-
#if 0
-
Mat dst;
-
Canny(src,dst,50,200);
-
imwrite("dst.jpg",dst);
-
imshow("dst",dst);
-
#endif
-
waitKey(0);
-
}
效果图: