Android学习八---OpenCV JAVA API

    OpenCV java API的文档说明在OpenCV-2.4.10-android-sdk/sdk/java/javadoc/index.html的文件夹下。想用java API的方式进行OpenCV4android 应用开发还是挺简单,首先就这些API先熟悉一下,然后对自己要开发的应用设计好流程,需要用到什么的数据结构进行存储,用到什么算法。然后对算法进行了解,输入参数是什么,输出参数是什么。有哪些fields和methods。

    1.Packages:org.opencv.core

Core:

对矩阵的进行基本运算(加减乘除等)的一些函数

CvType:

基本数据类型的定义

CV_16UC3,代表的是16位无符号整形3通道。

Mat:

构造函数

public Mat(int rows,
   int cols,
   int type)
public Mat(int rows,
   int cols,
   int type,
   Scalar s)
public Mat(Mat m,
   Rect roi)
Methods:
  • get
    public double[] get(int row,
               int col)
  • 取得某个坐标的数据,返回值是double,包含的是多个通道数据。
  • eye
    public static Mat eye(Size size,
          int type)
  • 类似matlab中的初始化eye将对角线元素置为1,其他为0.
  • height
    • public int height()
  • 得到矩阵的高
  • width
    • public int width()
  • 得到矩阵的宽
  • public static Mat ones(int rows,
           int cols,
           int type)
  • put
    • public int put(int row,
            int col,
            byte[] data)

API 里非常重要的一个类

MatOfKeyPoint:

存储KeyPoint的Match,继承自Mat,包含Mat的一系列Methods,另外还有

public void alloc(int elemNumber)
public void fromArray(KeyPoint... a)
public void fromList(java.util.List<KeyPoint> lkp)
public java.util.List<KeyPoint> toList()
public KeyPoint[] toArray()

KeyPoint:

用于显著点检测的数据结构,包含的数据域Keypoint的坐标,有意义keypoint的半径。

Point:

点,一般用来表示像素的坐标,包含:double x,double y两个域, 
Method and Description

Point clone()

double dot(Point p)

boolean equals(java.lang.Object obj)

int hashCode()

boolean inside(Rect r)

void set(double[] vals)

java.lang.String toString()

MatOfPoint:

保存Point的Mat,同样继承自Mat,包含Mat的一系列Methods。

Rect:

Rect(int x, int y, int width, int height)

重要的方法 
Method and Description:

double area():返回rect的面积

Point br():返回rect的左上角坐标

Point tl():返回rect的右下角坐标

void set(double[] vals)

Size size()


    2.Packages:org.opencv.imgproc

这个包中包括滤波,计算直方图,颜色转换,边缘检测,二值化,模糊,金字塔运算,调整图像大小等等。

介绍几个比较重要和常用的算法。

    1.对图像进行二值化

    static void 
adaptiveThreshold(Mat src, Mat dst, double maxValue, int adaptiveMethod, int thresholdType, int blockSize, double C)

使用自适应阈值的方式来二值化图像, T(x,y)是对每个像素计算的阈值.

  • 对于 ADAPTIVE_THRESH_MEAN_CT(x,y) 是(x, y)的blockSize x blockSize 的领域均值 减去 C.
  • 对于ADAPTIVE_THRESH_GAUSSIAN_CT(x,y) 是(x, y)的blockSize x blockSize 的领域加权均值 减去 C.
Parameters:
src - 原图像8位单通道图像.
dst -和原图像相同类型的的目标图像.
maxValue - 和thresholdType相关,如果这一参数为 THRESH_BINARY,那么二值化图像像素大于阈值为maxValue,反之参数为THRESH_BINARY_INV,则小于阈值的被赋值为maxValue。
adaptiveMethod - 能够使用哪种自适应阈值算法, ADAPTIVE_THRESH_MEAN_C orADAPTIVE_THRESH_GAUSSIAN_C.
thresholdType - Thresholding type that must be either THRESH_BINARY or THRESH_BINARY_INV.
blockSize - 对于某个像素,计算其阈值所考虑的元素范围: 3, 5, 7, and so on.
C - 从均值中减去的一个常数. 一般是取正值,也可以去0或者负数.

example:

Imgproc.adaptiveThreshold(inputFrame.gray(), mbyte, 255,Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 5, 2);

    2.寻找图像的轮廓

findContours
public static void findContours(Mat image,java.util.List<MatOfPoint> contours,Mat hierarchy,int mode,int method)

寻找二值图像中的轮廓。

Parameters:
image -源图像,8位单通道图像,非零值被当做是1,所以图像是被当作二值图像来对待的。 
contours - 检测到的轮廓,轮廓是由一系列的点构成,存储在java 的list中,每个list的元素是MatOfPoint.
hierarchy - 可选的输出参数,包含着图像的拓扑信息,有和contours相同数量的元素。对于每个contours[i],对应的hierarchy[i][0]hiearchy[i][1]hiearchy[i][2]和 hiearchy[i][3]分别被设置同一层次的下一个,前一个,第一个孩子和父的contour。 如果contour i不存在对应的contours,那么相应的hierarchy[i] 就被设置成负数。
mode - Contour的生成模式
  • CV_RETR_EXTERNAL 只生成最外层的contours.对于所有的contours都有hierarchy[i][2]=hierarchy[i][3]=-1 .
  • CV_RETR_LIST 不使用层次结构得到所有的contours.
  • CV_RETR_CCOMP 使用两个层次结构得到所有的contours .
  • CV_RETR_TREE得到所有的contours,并对contours建立层次结构.
method - Contour 的估计方式.
  • CV_CHAIN_APPROX_NONE stores absolutely all the contour points. That is, any 2 subsequent points (x1,y1) and (x2,y2) of the contour will be either horizontal, vertical or diagonal neighbors, that is, max(abs(x1-x2),abs(y2-y1))==1.
  • CV_CHAIN_APPROX_SIMPLE compresses horizontal, vertical, and diagonal segments and leaves only their end points. For example, an up-right rectangular contour is encoded with 4 points.
  • CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS applies one of the flavors of the Teh-Chin chain approximation algorithm. See [TehChin89] for details.

example:

首先定义存储hierarchy和contours的变量

List<MatOfPoint> contour = new ArrayList<MatOfPoint>();

Mat hierarchy = new Mat();

Imgproc.findContours(mbyte, contour, hierarchy, 
                    Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE); 
                       for (int ind = 0; ind < contour.size(); ind++) {

…………..

}

    3.画出图形轮廓

drawContours
public static void drawContours(Mat image,
                java.util.List<MatOfPoint> contours,
                int contourIdx,
                Scalar color,
                int thickness)

Draws contours outlines or filled contours.

 

3.Packages:org.opencv.features2d

主要是提取二维图像的特征比如MSER,HARRIS,STAR,SURF,SIFT等。下篇更新。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值