本节内容我们将学习:
在本节教程中,我们将着重使用两个类cv::Point 和cv::Scalar 。
用x和y坐标代表二维图像中的一个点,可以用如下代码定义:
Point point = new Point();
point.x = 10;
point.y = 8;
或者
Point point1 = new Point(10, 8);
Scalar scalar = new Scalar(0, 0, 0);
public class Main {
private static final int W = 400;
public static void main(String[] args) {
System.loadLibrary("libs/"+ Core.NATIVE_LIBRARY_NAME);
String atom_window = "Drawing 1:Atom";
String rook_window = "Drawing 2:rook";
Mat atom_image = Mat.zeros(W, W, CvType.CV_8UC3);
Mat rook_image = Mat.zeros(W, W, CvType.CV_8UC3);
MyEllipse(atom_image,90.0);
MyEllipse(atom_image,0.0);
MyEllipse(atom_image,45.0);
MyEllipse(atom_image,-45.0);
MyFilledCircle(atom_image,new Point(W/2,W/2));
MyPolygon(rook_image);
rectangle(rook_image,
new Point(0,7*W/8),
new Point(W,W),
new Scalar(0,255,255),
-1,
8,
0);
MyLine( rook_image, new Point( 0, 15*W/16 ), new Point( W, 15*W/16 ) );
MyLine( rook_image, new Point( W/4, 7*W/8 ), new Point( W/4, W ) );
MyLine( rook_image, new Point( W/2, 7*W/8 ), new Point( W/2, W ) );
MyLine( rook_image, new Point( 3*W/4, 7*W/8 ), new Point( 3*W/4, W ) );
imshow(atom_window,atom_image);
moveWindow(atom_window,0,200);
imshow(rook_window,rook_image);
moveWindow(rook_window,W,200);
waitKey();
System.exit(0);
}
private static void MyEllipse(Mat img, double angle){
int thickness = 2;
int lineType = 8;
int shift = 0;
ellipse(img,
new Point(W/2,W/2),
new Size(W/4,W/16),
angle,
0.0,
360.0,
new Scalar(255,0,0),
thickness,
lineType,
shift);
}
public static void MyFilledCircle(Mat img,Point center){
int thickness = 2;
int lineType = 8;
int shift = 0;
circle(img,
center,
W/32,
new Scalar(0,0,255),
thickness,
lineType,
shift);
}
private static void MyPolygon(Mat img){
int lineType = 8;
int shift = 0;
Point[] rook_points = new Point[20];
rook_points[0] = new Point( W/4, 7*W/8 );
rook_points[1] = new Point( 3*W/4, 7*W/8 );
rook_points[2] = new Point( 3*W/4, 13*W/16 );
rook_points[3] = new Point( 11*W/16, 13*W/16 );
rook_points[4] = new Point( 19*W/32, 3*W/8 );
rook_points[5] = new Point( 3*W/4, 3*W/8 );
rook_points[6] = new Point( 3*W/4, W/8 );
rook_points[7] = new Point( 26*W/40, W/8 );
rook_points[8] = new Point( 26*W/40, W/4 );
rook_points[9] = new Point( 22*W/40, W/4 );
rook_points[10] = new Point( 22*W/40, W/8 );
rook_points[11] = new Point( 18*W/40, W/8 );
rook_points[12] = new Point( 18*W/40, W/4 );
rook_points[13] = new Point( 14*W/40, W/4 );
rook_points[14] = new Point( 14*W/40, W/8 );
rook_points[15] = new Point( W/4, W/8 );
rook_points[16] = new Point( W/4, 3*W/8 );
rook_points[17] = new Point( 13*W/32, 3*W/8 );
rook_points[18] = new Point( 5*W/16, 13*W/16 );
rook_points[19] = new Point( W/4, 13*W/16 );
MatOfPoint matPt = new MatOfPoint();
matPt.fromArray(rook_points);
ArrayList<MatOfPoint> ppt = new ArrayList<>();
ppt.add(matPt);
fillPoly(img,
ppt,
new Scalar(255,255,255),
lineType,
shift,
new Point(0,0));
}
private static void MyLine(Mat img,Point start, Point end){
int thickness = 2;
int lineType = 8;
int shift = 0;
line(img,
start,
end,
new Scalar(0,0,0),
thickness,
lineType,
shift);
}
}
首先我们创建两个窗体和Mat对象用于显示保存我们需要绘制的两个图像:
String atom_window = "Drawing 1:Atom";
String rook_window = "Drawing 2:rook";
Mat atom_image = Mat.zeros(W, W, CvType.CV_8UC3);
Mat rook_image = Mat.zeros(W, W, CvType.CV_8UC3);
我们创建不同的方法去绘制不同的图形。在本例中绘制原子需要用到MyEllipse 和 MyFilledCircle:
MyEllipse(atom_image,90.0);
MyEllipse(atom_image,0.0);
MyEllipse(atom_image,45.0);
MyEllipse(atom_image,-45.0);
MyFilledCircle(atom_image,new Point(W/2,W/2));
绘制国际象棋需要用到MyLine, rectangle 和a MyPolygon:
MyPolygon(rook_image);
rectangle(rook_image,
new Point(0,7*W/8),
new Point(W,W),
new Scalar(0,255,255),
-1,
8,
0);
MyLine( rook_image, new Point( 0, 15*W/16 ), new Point( W, 15*W/16 ) );
MyLine( rook_image, new Point( W/4, 7*W/8 ), new Point( W/4, W ) );
MyLine( rook_image, new Point( W/2, 7*W/8 ), new Point( W/2, W ) );
MyLine( rook_image, new Point( 3*W/4, 7*W/8 ), new Point( 3*W/4, W ) );
接下来让我们看看每个方法内部是怎么实现的:
private static void MyLine(Mat img,Point start, Point end){
int thickness = 2;
int lineType = 8;
int shift = 0;
line(img,
start,
end,
new Scalar(0,0,0),
thickness,
lineType,
shift);
}
new Scalar(0,0,0),
绘制一条黑色的线段 private static void MyEllipse(Mat img, double angle){
int thickness = 2;
int lineType = 8;
int shift = 0;
ellipse(img,
new Point(W/2,W/2),
new Size(W/4,W/16),
angle,
0.0,
360.0,
new Scalar(255,0,0),
thickness,
lineType,
shift);
}
public static void MyFilledCircle(Mat img,Point center){
int thickness = 2;
int lineType = 8;
int shift = 0;
circle(img,
center,
W/32,
new Scalar(0,0,255),
thickness,
lineType,
shift);
}
private static void MyPolygon(Mat img){
int lineType = 8;
int shift = 0;
Point[] rook_points = new Point[20];
rook_points[0] = new Point( W/4, 7*W/8 );
rook_points[1] = new Point( 3*W/4, 7*W/8 );
rook_points[2] = new Point( 3*W/4, 13*W/16 );
rook_points[3] = new Point( 11*W/16, 13*W/16 );
rook_points[4] = new Point( 19*W/32, 3*W/8 );
rook_points[5] = new Point( 3*W/4, 3*W/8 );
rook_points[6] = new Point( 3*W/4, W/8 );
rook_points[7] = new Point( 26*W/40, W/8 );
rook_points[8] = new Point( 26*W/40, W/4 );
rook_points[9] = new Point( 22*W/40, W/4 );
rook_points[10] = new Point( 22*W/40, W/8 );
rook_points[11] = new Point( 18*W/40, W/8 );
rook_points[12] = new Point( 18*W/40, W/4 );
rook_points[13] = new Point( 14*W/40, W/4 );
rook_points[14] = new Point( 14*W/40, W/8 );
rook_points[15] = new Point( W/4, W/8 );
rook_points[16] = new Point( W/4, 3*W/8 );
rook_points[17] = new Point( 13*W/32, 3*W/8 );
rook_points[18] = new Point( 5*W/16, 13*W/16 );
rook_points[19] = new Point( W/4, 13*W/16 );
MatOfPoint matPt = new MatOfPoint();
matPt.fromArray(rook_points);
ArrayList<MatOfPoint> ppt = new ArrayList<>();
ppt.add(matPt);
fillPoly(img,
ppt,
new Scalar(255,255,255),
lineType,
shift,
new Point(0,0));
}
rectangle(rook_image,
new Point(0,7*W/8),
new Point(W,W),
new Scalar(0,255,255),
-1,
8,
0);