// Initialize OpencV
runtime.images.initOpenCvIfNeeded();
Copy the code
importClass(org.opencv.core.Scalar);
importClass(org.opencv.core.Point);
importClass(java.util.LinkedList);
importClass(org.opencv.imgproc.Imgproc);
importClass(org.opencv.imgcodecs.Imgcodecs);
importClass(org.opencv.core.Core);
importClass(org.opencv.core.Mat);
importClass(org.opencv.core.MatOfDMatch);
importClass(org.opencv.core.MatOfKeyPoint);
importClass(org.opencv.core.MatOfRect);
importClass(org.opencv.core.Size);
importClass(org.opencv.features2d.DescriptorExtractor);
importClass(org.opencv.features2d.DescriptorMatcher);
importClass(org.opencv.features2d.FeatureDetector);
importClass(org.opencv.features2d.Features2d);
importClass(android.graphics.Matrix);
importClass(org.opencv.android.Utils);
importClass(android.graphics.Bitmap);
Copy the code
// Read the image
var img = Imgcodecs.imread(imgPath);
// Read the grayscale image
var img = Imgcodecs.imread(imgPath, 0);
Copy the code
// OpencV mat to bitmap
function mat2bitmap(img) {
let width = img.width();
let height = img.height();
let bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Utils.matToBitmap(img, bitmap);
return bitmap;
}
Copy the code
/ / zooming bitmap
function zoomBitmap(bitmap, scale) {
let matrix = new Matrix();
matrix.postScale(scale, scale); // The ratio of length and width to zoom in and out
let resizeBmp = Bitmap.createBitmap(bitmap, 0.0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizeBmp;
}
Copy the code
// The image control sets the bitmap
ui.img.setImageBitmap(bitmap);
Copy the code
functionBinary image (imgPath) {
let dst = new Mat();
let maxValue = 255;
let adaptiveMethod = Imgproc.ADAPTIVE_THRESH_MEAN_C;
let thresholdType = Imgproc.THRESH_BINARY_INV;
let blockSize = 3;
let C = 4;
Imgproc.adaptiveThreshold(grayImg, dst, maxValue, adaptiveMethod, thresholdType, blockSize, C);
return dst;
}
Copy the code
function newSize(size) {
if (!Array.isArray(size)) {
size = [size, size];
}
if (size.length == 1) {
size = [size[0], size[0]];
}
return new org.opencv.core.Size(size[0], size[1]);
}
Copy the code
// Define the contour variables
let contours = new java.util.ArrayList();
// Outline length
let len = contours.size();
/ / find outline
Imgproc.findContours(img, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, Point());
/ / contours
Imgproc.drawContours(img, contours, i, Scalar(0.0.0), -1);
// Enclosing the rectangle
let rect = Imgproc.boundingRect(contour);
/ / area
let area = rect.area();
/ wide/high
let width = rect.width;
let height = rect.height;
/ / the top left corner
let left = rect.x;
let top = rect.y;
Copy the code
/ / black hat
Imgproc.morphologyEx(
img1,
img2,
Imgproc.MORPH_BLACKHAT,
Imgproc.getStructuringElement(Imgproc.MORPH_RECT, newSize(5)));Copy the code
The statement
This tutorial is for study only and is prohibited for other purposes