• 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_C, T(x,y) (x, y)的blockSize x blockSize 的领域均值 减去 C.
    • 对于ADAPTIVE_THRESH_GAUSSIAN_C, T(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等。下篇更新。

  • 相关阅读:
    题解——洛谷P3812【模板】线性基
    题解——洛谷P2781 传教(线段树)
    题解——洛谷P1962 斐波那契数列(矩阵乘法)
    题解——洛谷P3390 【模板】矩阵快速幂(矩阵乘法)
    题解——牛客网OI赛制测试赛2
    题解——code[vs] 1506 传话(传递闭包)
    题解——Codeforces Round #508 (Div. 2) T3 (贪心)
    题解——Codeforces Round #508 (Div. 2) T2 (构造)
    题解——Codeforces Round #508 (Div. 2) T1 (模拟)
    题解——Codeforces Round #507 (based on Olympiad of Metropolises) T2(模拟)
  • 原文地址:https://www.cnblogs.com/dawnminghuang/p/4183599.html
Copyright © 2020-2023  润新知