The tutorial is easy to understand

I downloaded the 4.5.2 Version of OpencV for Android, but I didn’t find it

KNN profile

An introduction to

The neighborhood algorithm, or K-nearest neighbor (KNN) classification algorithm is one of the simplest methods in data mining classification technology. And by K nearest neighbors, that means K nearest neighbors, what that means is that each sample can be represented by its closest K neighboring values. The nearest neighbor algorithm is a method to classify each record in the data set.

Net friend big rape cat to KNN algorithm description

S1 is distance

Given an unknown sample point A, calculate the distance between it and each sample point in the training set, S2. Find neighbors. Arrange the calculated distance of S1 in ascending order, and take the first K nearest sample points as A’s neighbors, S3, to determine classification

KNN schematic

The target is a yellow polygon, and the three closest neighbors to the yellow polygon, two are red, one is blue,

Yellow polygons are considered red by the inference that the minority is subordinated to the majority

It can also be said that if you surround yourself with that kind of people, you become that kind of people

Algorithm commonly used distance

Manhattan distance (city block distance)

Euclidean distance

Mahalanobis distance (Minkowski distance)

Cosine distance

Chebyshev distance

Hamming distance

Autojs version

8.8.16-0

This version includes opencV 3.4.3

KNN brief process

  1. The image is a large one, so the first step is to cut the image so that each image contains only one number
  2. training
  3. To predict

training

The training is matching the features to the tags one by one, doing some processing on the data,

I’m going to talk about feature extraction,

Features: Take the value of each pixel of the image, an image is 20X20, which is 400 pixels, and the image is a channel

Key code:

let tempData = Imgcodecs.imread(filePath, 0); // A channel
tempData = tempData.reshape(0.1); // The matrix is changed to one row
tempData.convertTo(tempData, CvType.CV_32F); // The data is changed to floating point
tempData.copyTo(tmp); // Save the feature
trainlabel.put(0.0, trainClasses); / / play tag
knn.train(trainData, Ml.ROW_SAMPLE, trainlabel); / / training
Copy the code

To predict

Forecasting is taking a small piece of data and testing it,

When you train, you keep a small amount of data, you don’t train, you use it for prediction

Prediction, like training,

Take the features of the image, calculate the distance with the trained data,

The value with the highest match is then returned

The key code

let tempData = Imgcodecs.imread(filePath, 0); // A channel
tempData = tempData.reshape(0.1); // The matrix is changed to one row
tempData.convertTo(tempData, CvType.CV_32F); // The data is changed to floating point
let response = knn.findNearest(tempData, k, nearests); // Calculate the best match
Copy the code

statistical

log(Total number of tests: + testNum);
log("Correct classification number: -->" + trueNum);
log("Accuracy:" + (trueNum / testNum) * 100 + "%");
Copy the code

Mat

Mat is a common data type for OpencV. Understanding MAT’s format will help you understand OpencV code

Modify the Mat

The AUTOJS Mat has no at method, so get and PUT are used to modify data

runtime.images.initOpenCvIfNeeded();
log(new org.opencv.core.Mat().getClass());
delete org.opencv.core.Mat;
log(new org.opencv.core.Mat().getClass());
importClass(org.opencv.core.Mat);
importClass(org.opencv.core.CvType);

// 32-bit floating point number 1 channel
let trainlabel = Mat.ones(100.1, CvType.CV_32FC1);

for (var i = 0; i < 100; i++) {
  log("Before modification" + i + ":", trainlabel.get(i, 0));
  let item = util.java.array("float".1);
  item[0] = i;
  log(trainlabel.put(i, 0, item));
  log("After modification" + i + ":", trainlabel.get(i, 0));
}
Copy the code

Print the width and height of the Mat column

let trainlabel = Mat.ones(100.1, CvType.CV_32FC1);
let infoList = [
  "\n"."row: " + trainlabel.rows(),
  "col: " + trainlabel.cols(),
  "height: " + trainlabel.height(),
  "width: " + trainlabel.width(),
];
log(infoList.join("\n"));
// row: 100
// col: 1
// height: 100
// width: 1
Copy the code

Just think of the Mat as a wall,

This is row, this is col,

As many rows as there are,

There are as many cols as there are widths

The Mat parameter is usually before row and after col

First look at what row the data is in, then look at what column the data is in

It’s also in line with human thinking

We also read from left to right and from top to bottom

The ancients probably said, look at the column first, then look at the line,

For the books of the ancients were written vertically

Print the Mat specific data

runtime.images.initOpenCvIfNeeded();
importClass(org.opencv.core.Mat);
importClass(org.opencv.core.CvType);

// 3 rows and 2 columns
let trainlabel = Mat.ones(3.2, CvType.CV_32FC1);
let width = trainlabel.width();
let height = trainlabel.height();
let arr = [];
// Height is the same as the number of rows
// Hegith indicates the number of rows
for (var i = 0; i < height; i++) {
  let childArr = [];
  for (var j = 0; j < width; j++) {
    let item = trainlabel.get(i, j);
    childArr.push(item);
  }
  arr.push(childArr);
}
log(JSON.stringify(arr, null.""));
/ / /
/ / [[1.0], [1.0]].
/ / [[1.0], [1.0]].
/ / [[1.0], [1.0]]
// ]
Copy the code

The statement

This tutorial is for study only and is prohibited for other purposes