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 eitherTHRESH_BINARY
orTHRESH_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。 如果contouri
不存在對應的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建立層次結構.
- CV_RETR_EXTERNAL 只生成最外層的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.
- CV_CHAIN_APPROX_NONE stores absolutely all the contour points. That is, any 2 subsequent points
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等。下篇更新。