Android Learning Eight---OpenCV JAVA API

Source: Internet
Author: User
Tags scalar

The documentation for the OpenCV Java API is described in the Opencv-2.4.10-android-sdk/sdk/java/javadoc/index.html folder.

Want to use Java API way to opencv4android application development is very simple, first of all these APIs first familiar with, and then to develop the application to design a good process, need to use what the data structure to store, use what algorithm. Then we understand the algorithm, what the input parameters are, and what the output parameters are. What fields and methods are there?

1.packages:org.opencv.coreCore:

Some functions for basic operation of matrices (subtraction, etc.)

Cvtype:

Definition of a basic data type

CV_16UC3For example, a 16-bit unsigned 3 channel is represented.

Mat:

constructor function

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)
  • Gets the data for a coordinate, the return value is double, and contains multiple channel data.
  • Eye
    public static Mat eye (size size,      int type)
  • An initial eye similar to MATLAB set the diagonal element to 1 and the other to 0.
  • Height
    • public int height ()
  • Get the high of the Matrix
  • Width
      • public int width ()
  • Get the width of the matrix
  • public static Mat ones (int rows,       int cols,       int type)
  • Put
      • public int put (int row,      int col,      byte[] data)

A very important class in the API

Matofkeypoint:

Storage KeyPoint Match, inherited from Mat, contains a series of methods of Mat, plus

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:

A data structure used for significant point detection, containing the coordinates of the data field KeyPoint, with a meaningful keypoint radius.

Point :

Point, commonly used to represent the coordinates of a pixel, contains: Double x,double y two fields,
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:

The mat that holds the point is also inherited from the mat, which contains a series of methods of the mat.

Rect:

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

Important method
Method and Description:

double area():返回rect的面积

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

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

void set(double[] vals)

Size size()

2.packages:org.opencv.imgproc

This package includes filtering, calculation histogram, color conversion, edge detection, binary, blur, pyramid operations, resizing images, and so on.

The paper introduces several important and common algorithms.

1. Binary image

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

The adaptive threshold is used to two the value of the image, and T (x, y) is the threshold computed for each pixel.

    • For ADAPTIVE_THRESH_MEAN_C , T ( x , y ) is the blockSize x blockSize field mean minus C .
    • For ADAPTIVE_THRESH_GAUSSIAN_C , T ( x , y ) is the blockSize x blockSize of the field weighted mean minus C .
Parameters:
src -Original image 8-bit single-channel image.
dst -the same type of target image as the original image.
maxValue -related to Thresholdtype, if this parameter is THRESH_BINARY , then two value image pixel is greater than the threshold value of MaxValue, the inverse parameter is THRESH_BINARY_INV , then the value less than the threshold is assigned to MaxValue.
adaptiveMethod -which adaptive threshold algorithm can be used, ADAPTIVE_THRESH_MEAN_C or ADAPTIVE_THRESH_GAUSSIAN_C .
thresholdType -thresholding type that must is either THRESH_BINARY or THRESH_BINARY_INV .
blockSize -For a pixel, the range of elements considered for calculating its thresholds: 3, 5, 7, and so on.
C -A constant subtracted from the mean. Generally, you can take a positive value or go to 0 or a negative number.

Example

Imgproc.adaptivethreshold (Inputframe.gray (), MByte, 255,imgproc.adaptive_thresh_gaussian_c, Imgproc.THRESH_BINARY _INV, 5, 2);

2. Look for the outline of the imagefindcontours
public static void Findcontours (Mat image,java.util.list<matofpoint> contours,mat hierarchy,int Mode,int method)

Look for the contours in the binary image.

Parameters:
image -source image, 8-bit single-channel image, non-0 value is considered 1, so the image is treated as a two-value image.
contours -The detected contour, which is composed of a series of points, is stored in the list of Java, and the elements of each list are matofpoint.
hierarchy -Optional output parameters that contain the topological information of the image, with the same number of elements as the contours. For each contours[i] , corresponding hierarchy[i][0] , hiearchy[i][1] , hiearchy[i][2]和 hiearchy[i][3]分别 is set the same level of the next, the previous one, the first child and the parent's contour. If the contour i does not have a corresponding contours, then the corresponding is hierarchy[i] set to negative.
mode -Contour generation mode
  • cv_retr_external Only the outermost contours is generated. For all contours, there is a hierarchy[i][2]=hierarchy[i][3]=-1 .
  • cv_retr_ccomp uses two hierarchies to get all contours.
cv_chain_approx_none stores absolutely all The contour points. That's, any 2 subsequent points (x1,y1) and (x2,y2) of the contour would be either horizontal, V Ertical or diagonal neighbors, that's, Max (ABS (X1-X2), ABS (Y2-Y1)) ==1 .
  • 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

    First define the variables that store hierarchy and 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. Draw a graphic outlinedrawcontours
    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

    It mainly extracts the features of two-dimensional image, such as Mser,harris,star,surf,sift. The next article is updated.

    Android Learning Eight---OpenCV JAVA API

    Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.