標籤:
OpenCV java API的文檔說明在OpenCV-2.4.10-android-sdk/sdk/java/javadoc/index.html的檔案夾下。
想用java API的方式進行OpenCV4android 應用開發還是挺簡單,首先就這些API先熟悉一下,然後對自己要開發的應用設計好流程,需要用到什麼的資料結構進行儲存,用到什麼演算法。然後對演算法進行瞭解,輸入參數是什麼,輸出參數是什麼。有哪些fields和methods。
1.Packages:org.opencv.coreCore:
對矩陣的進行基本運算(加減乘除等)的一些函數
CvType:
基礎資料型別 (Elementary Data Type)的定義
如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
得到矩陣的高
- width
得到矩陣的寬
public static Mat ones(int rows, int cols, int type)
put
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 or
ADAPTIVE_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等。下篇更新。
Android學習八---OpenCV JAVA API