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等。下篇更新。

發佈了80 篇原創文章 · 獲贊 61 · 訪問量 50萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章