700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > OpenCV4.3 Java 编程入门:Core 组件中的数据结构与方法

OpenCV4.3 Java 编程入门:Core 组件中的数据结构与方法

时间:2020-07-15 22:55:28

相关推荐

OpenCV4.3 Java 编程入门:Core 组件中的数据结构与方法

opencv javadoc

文章目录

1. java packages1.1 org.opencv.core2 Core 中常用的数据结构2.1 Point 类:像素的位置2.2 Scalar 类:像素的颜色2.3 Size 类:图的尺寸2.4 Rect 类:矩形区域2.5 Mat 类3 Core 中常用的函数4 Mat 中的方法

1. java packages

1.1 org.opencv.core

核心功能模块,包括:

OpenCV基本数据结构,动态数据结构,绘图函数,数组操作相关函数;辅助功能

2 Core 中常用的数据结构

2.1 Point 类:像素的位置

Point 类表示二维坐标系下的点,用来描述某个像素点的位置信息,是由图像坐标 x 和 y 指定的 2D 点。

public class Point {public double x, y;public Point(double x, double y) {this.x = x;this.y = y;}public Point() {this(0, 0);}public Point(double[] vals) {this();set(vals);}public void set(double[] vals) {if (vals != null) {x = vals.length > 0 ? vals[0] : 0;y = vals.length > 1 ? vals[1] : 0;} else {x = 0;y = 0;}}public Point clone() {return new Point(x, y);}public double dot(Point p) {return x * p.x + y * p.y;}}

2.2 Scalar 类:像素的颜色

Scalar 类表示具有4个元素的数组,在 OpenCV 中被用来表示像素的颜色,如 RGB 颜色值。构造函数 Scalar(double v0, double v1, double v2),其中 v0 为红色分量,v1 为绿色分量,v2 为蓝色分量。

public class Scalar {public double val[];public Scalar(double v0, double v1, double v2, double v3) {val = new double[] {v0, v1, v2, v3 };}public Scalar(double v0, double v1, double v2) {val = new double[] {v0, v1, v2, 0 };}public Scalar(double v0, double v1) {val = new double[] {v0, v1, 0, 0 };}public Scalar(double v0) {val = new double[] {v0, 0, 0, 0 };}public Scalar(double[] vals) {if (vals != null && vals.length == 4)val = vals.clone();else {val = new double[4];set(vals);}}public void set(double[] vals) {if (vals != null) {val[0] = vals.length > 0 ? vals[0] : 0;val[1] = vals.length > 1 ? vals[1] : 0;val[2] = vals.length > 2 ? vals[2] : 0;val[3] = vals.length > 3 ? vals[3] : 0;} elseval[0] = val[1] = val[2] = val[3] = 0;}public static Scalar all(double v) {return new Scalar(v, v, v, v);}public Scalar clone() {return new Scalar(val);}public Scalar mul(Scalar it, double scale) {return new Scalar(val[0] * it.val[0] * scale, val[1] * it.val[1] * scale,val[2] * it.val[2] * scale, val[3] * it.val[3] * scale);}public Scalar mul(Scalar it) {return mul(it, 1);}public Scalar conj() {return new Scalar(val[0], -val[1], -val[2], -val[3]);}public boolean isReal() {return val[1] == 0 && val[2] == 0 && val[3] == 0;}}

2.3 Size 类:图的尺寸

Size 类用来表示图的大小,构造函数 Size(double width, double height)。

public class Size {public double width, height;public Size(double width, double height) {this.width = width;this.height = height;}public Size() {this(0, 0);}public Size(Point p) {width = p.x;height = p.y;}public Size(double[] vals) {set(vals);}public void set(double[] vals) {if (vals != null) {width = vals.length > 0 ? vals[0] : 0;height = vals.length > 1 ? vals[1] : 0;} else {width = 0;height = 0;}}public double area() {return width * height;}public boolean empty() {return width <= 0 || height <= 0;}public Size clone() {return new Size(width, height);}}

2.4 Rect 类:矩形区域

Rect 类的成员变量有 x,y,width,height,分别为左上角点(Point)的坐标和矩形的宽和高。

常用的成员函数有:

size(): 返回 Size() 对象,表示矩形区域的大小;tl(): 返回左上角的点对应的 Point 对象;br(): 返回右下角的点对应的 Point 对象;area() : 矩形的面积contains(Point p): 判断给定的点,是否在矩形内;inside(Rect rect): 判断给定的矩形是否在本矩形内;

public class Rect {public int x, y, width, height;public Rect(int x, int y, int width, int height) {this.x = x;this.y = y;this.width = width;this.height = height;}public Rect() {this(0, 0, 0, 0);}public Rect(Point p1, Point p2) {x = (int) (p1.x < p2.x ? p1.x : p2.x);y = (int) (p1.y < p2.y ? p1.y : p2.y);width = (int) (p1.x > p2.x ? p1.x : p2.x) - x;height = (int) (p1.y > p2.y ? p1.y : p2.y) - y;}public Rect(Point p, Size s) {this((int) p.x, (int) p.y, (int) s.width, (int) s.height);}}

2.5 Mat 类

Mat 类是用于保存图像以及其他矩阵数据的数据结构,默认情况下的尺寸是0。

从 OpenCV 踏入 2.0 时代,使用 Mat 类数据结构作为主打之后,OpenCV 开始像 Matlab 一样,上手越发方便,甚至有些函数名都跟 Matlab 一样,比如 imread,imwrite,imshow 等。

使用 Mat 类,开发人员不需要再手动开辟空间,也不必考虑空间的释放。

Mat 类由两个数据部分组成:矩阵头(包含矩阵尺寸、存储方法、存储地址等信息)和一个指向存储所有像素值的矩阵的指针。

构造方法:

public class Mat {public final long nativeObj;public Mat(long addr) {if (addr == 0)throw new UnsupportedOperationException("Native object address is NULL");nativeObj = addr;}// 无参构造函数public Mat() {nativeObj = n_Mat();}// 行,列,通道类型public Mat(int rows, int cols, int type) {nativeObj = n_Mat(rows, cols, type);}// data: 指向用户数据的指针。接受数据参数的矩阵构造函数不分配矩阵数据。相反,它们只是初始化指向指定数据的矩阵头,// 这意味着不复制任何数据。此操作非常高效,可以使用OpenCV函数处理外部数据。外部数据不会自动解除分配,因此您应该妥善保管。public Mat(int rows, int cols, int type, ByteBuffer data) {nativeObj = n_Mat(rows, cols, type, data);}public Mat(int rows, int cols, int type, ByteBuffer data, long step) {nativeObj = n_Mat(rows, cols, type, data, step);}public Mat(Size size, int type) {nativeObj = n_Mat(size.width, size.height, type);}public Mat(int[] sizes, int type) {nativeObj = n_Mat(sizes.length, sizes, type);}public Mat(int rows, int cols, int type, Scalar s) {nativeObj = n_Mat(rows, cols, type, s.val[0], s.val[1], s.val[2], s.val[3]);}public Mat(Size size, int type, Scalar s) {nativeObj = n_Mat(size.width, size.height, type, s.val[0], s.val[1], s.val[2], s.val[3]);}public Mat(int[] sizes, int type, Scalar s) {nativeObj = n_Mat(sizes.length, sizes, type, s.val[0], s.val[1], s.val[2], s.val[3]);}public Mat(Mat m, Range rowRange, Range colRange) {nativeObj = n_Mat(m.nativeObj, rowRange.start, rowRange.end, colRange.start, colRange.end);}public Mat(Mat m, Range rowRange) {nativeObj = n_Mat(m.nativeObj, rowRange.start, rowRange.end);}public Mat(Mat m, Range[] ranges) {nativeObj = n_Mat(m.nativeObj, ranges);}public Mat(Mat m, Rect roi) {nativeObj = n_Mat(m.nativeObj, roi.y, roi.y + roi.height, roi.x, roi.x + roi.width);}}

3 Core 中常用的函数

4 Mat 中的方法

doc

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。