
opencv几何图形画法
本文目的
目的:学习使用opencv的几何图形画法
语言:java
版本:opencv-410
简介:使用org.opencv.imgproc.Imgproc进行几何图形绘画
- 画线
- 画椭圆
- 画圆
- 画矩形
- 画多边形
分解介绍
画线
使用opencv画线,只用了它的一个line函数,
Imgproc.line(img,start,end,new Scalar(0,255,0),thickness,linetype,shift);
函数有几个参数:
第一个是一个Mat对象,代表要在哪个图形上画线
start,end代表线的起始点和结束点坐标(点使用Point)
第四个参数是画线的颜色,这里使用RGB表示法
第五个参数thickness代表了线的粗细
第六个参数linetype代表了线型
public static void drawLine(Mat img,Point start,Point end){
int thickness = 2;
int linetype = 8;
int shift = 0;
Imgproc.line(img,start,end,new Scalar(0,255,0),thickness,linetype,shift);
}
画圆
核心几个参数:
1.center定义了圆心的位置
2.第三个参数定义了圆半径
3.其他几个参数同上
private static void drawCircle(Mat mat,Point center){
Imgproc.circle(mat,center,width/2,new Scalar(255,0,0));
}
画椭圆
核心几个参数:
1.第二个参数定义了椭圆圆心位置
2.第三个参数定义了椭圆长半径和短半径的长度
3.第四个参数定义了椭圆的旋转角度
private static void drawEclipse(Mat img,double angle){
int thickness = 2;
int linetype = 8;
int shift = 0;
Imgproc.ellipse(img,
new Point(width/2,width/2),
new Size(width/4,width/16),
angle,0.0,360,
new Scalar(255,0,0),
thickness,linetype,shift);
}
画矩形框
核心参数:
start,end代表了画矩形时从哪个点到哪个对角点
public static void drawRect(Mat img,Point start,Point end){
int thickness = 2;
int linetype = 8;
int shift = 0;
Imgproc.rectangle(img,start,end,new Scalar(0,0,255));
}
代码
package com.joe.vision.machine.vision.samples;
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import java.util.ArrayList;
import java.util.List;
/**
* 几何形状画法
*/
public class GeometricDrawingTest {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
static int width = 400;
public static void main(String[] args){
String win1 = "window1";
String win2 = "window2";
Mat win1_img = Mat.zeros(new Size(width,width), CvType.CV_8UC3);
Mat win2_img = Mat.zeros(new Size(width,width), CvType.CV_8UC3);
//画椭圆
drawEclipse(win1_img,90);
drawEclipse(win1_img,0);
drawEclipse(win1_img,-45);
drawEclipse(win1_img,45);
//画线
drawLine(win1_img,new Point(width/2,width/2),new Point(width,width/2));
//画圆
drawCircle(win1_img,new Point(width/2,width/2));
//画矩形块
drawRect(win1_img,new Point(width/4,width/4),new Point(width/2,width/2));
//画多边形
//
drawPolygon(win1_img);
HighGui.imshow(win1,win1_img);
HighGui.waitKey();
System.exit(0);
}
private static void drawCircle(Mat mat,Point center){
Imgproc.circle(mat,center,width/2,new Scalar(255,0,0));
}
/**
* 画椭圆
* @param img
* @param angle
*/
private static void drawEclipse(Mat img,double angle){
int thickness = 2;
int linetype = 8;
int shift = 0;
Imgproc.ellipse(img,
new Point(width/2,width/2),
new Size(width/4,width/16),
angle,0.0,360,
new Scalar(255,0,0),
thickness,linetype,shift);
}
public static void drawLine(Mat img,Point start,Point end){
int thickness = 2;
int linetype = 8;
int shift = 0;
Imgproc.line(img,start,end,new Scalar(0,255,0),thickness,linetype,shift);
}
public static void drawRect(Mat img,Point start,Point end){
int thickness = 2;
int linetype = 8;
int shift = 0;
Imgproc.rectangle(img,start,end,new Scalar(0,0,255));
}
private static void drawPolygon( Mat img ) {
int lineType = 8;
int shift = 0;
Point[] rook_points = new Point[20];
rook_points[0] = new Point( width/4, 7*width/8 );
rook_points[1] = new Point( 3*width/4, 7*width/8 );
rook_points[2] = new Point( 3*width/4, 13*width/16 );
rook_points[3] = new Point( 11*width/16, 13*width/16 );
rook_points[4] = new Point( 19*width/32, 3*width/8 );
rook_points[5] = new Point( 3*width/4, 3*width/8 );
rook_points[6] = new Point( 3*width/4, width/8 );
rook_points[7] = new Point( 26*width/40, width/8 );
rook_points[8] = new Point( 26*width/40, width/4 );
rook_points[9] = new Point( 22*width/40, width/4 );
rook_points[10] = new Point( 22*width/40, width/8 );
rook_points[11] = new Point( 18*width/40, width/8 );
rook_points[12] = new Point( 18*width/40, width/4 );
rook_points[13] = new Point( 14*width/40, width/4 );
rook_points[14] = new Point( 14*width/40, width/8 );
rook_points[15] = new Point( width/4, width/8 );
rook_points[16] = new Point( width/4, 3*width/8 );
rook_points[17] = new Point( 13*width/32, 3*width/8 );
rook_points[18] = new Point( 5*width/16, 13*width/16 );
rook_points[19] = new Point( width/4, 13*width/16 );
MatOfPoint matPt = new MatOfPoint();
matPt.fromArray(rook_points);
List<MatOfPoint> ppt = new ArrayList<MatOfPoint>();
ppt.add(matPt);
Imgproc.fillPoly(img,
ppt,
new Scalar( 255, 255, 255 ),
lineType,
shift,
new Point(0,0) );
}
}
效果图
