// 初始化opencv runtime.images.initOpenCvIfNeeded();
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);
// 读取图片 var img = Imgcodecs.imread(imgPath); // 读取灰度图 var img = Imgcodecs.imread(imgPath, 0); // 写入图片 Imgcodecs.imwrite(imgPath, img);
// opencv的mat转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; }
// 缩放bitmap function zoomBitmap(bitmap, scale) { let matrix = new Matrix(); matrix.postScale(scale, scale); //长和宽放大缩小的比例 let resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); return resizeBmp; }
// 图片控件设置bitmap ui.img.setImageBitmap(bitmap);
function 二值化图片(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; }
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]); }
// 定义轮廓变量 let contours = new java.util.ArrayList(); // 轮廓长度 let len = contours.size(); // 找轮廓 Imgproc.findContours(img, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, Point()); // 画轮廓 Imgproc.drawContours(img, contours, i, Scalar(0, 0, 0), -1); // 外接矩形 let rect = Imgproc.boundingRect(contour); // 面积 let area = rect.area(); // 宽高 let width = rect.width; let height = rect.height; // 左上角 let left = rect.x; let top = rect.y;
// 黑帽 Imgproc.morphologyEx( img1, img2, Imgproc.MORPH_BLACKHAT, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, newSize(5)) ); // 开运算 // 开运算是通过先对图像腐蚀再膨胀实现的。 // 腐蚀图像中的小白点, 膨胀后小白点直接就没有了, 扩大黑色部分 let openMat = new Mat(); Imgproc.morphologyEx(src1, openMat, Imgproc.MORPH_OPEN, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, newSize(3)));
// 绘制文字 // Scalar(0, 0, 255, 255) bgra let fontFace = 3; // 字体 (如cv::FONT_HERSHEY_PLAIN) let fontScale = 2; // 尺寸因子,值越大文字越大 let thickness = 3; Imgproc.putText(img.mat, "" + i, Point(point.x, point.y), fontFace, fontScale, new Scalar(0, 0, 255, 255), thickness);
// 绘制矩形 Imgproc.rectangle( img.mat, new Point(eye.boundingRect.tl.x, eye.boundingRect.tl.y), new Point(eye.boundingRect.br.x, eye.boundingRect.br.y), new Scalar(0, 0, 255, 255), 6 );
//逼近曲线 //对图像轮廓点进行多边形拟合 let new_mat = new MatOfPoint2f(contour.toArray()); let approxCurve_temp = new MatOfPoint2f(); Imgproc.approxPolyDP(new_mat, approxCurve_temp, 7, true);
// 画圆 radius = 60; color = new Scalar(0, 255, 0, 255); // bgra thickness = 2; // 如果是正数,表示组成圆的线条的粗细程度。否则,-1表示圆是否被填充 Imgproc.circle(img.mat, Point(point.x, point.y), radius, color, thickness);
// mat转bitmap output = Bitmap.createBitmap(resultWidth, resultHeight, Bitmap.Config.ARGB_8888); org.opencv.android.Utils.matToBitmap(outputMat, output);
// 透视变换 let startM = getStartM(enlargedCoordinates); let endM = getEndM(); let perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM); let outputMat = new Mat(); Imgproc.warpPerspective(img.mat, outputMat, perspectiveTransform, new Size(resultWidth, resultHeight)); /* -------------------------------------------------------------------------- */ function getStartM(pointList) { let topLeft = Point(pointList[3].x, pointList[3].y); let topRight = Point(pointList[2].x, pointList[2].y); let bottomLeft = Point(pointList[0].x, pointList[0].y); let bottomRight = Point(pointList[1].x, pointList[1].y); let source = new ArrayList(); source.add(topLeft); source.add(topRight); source.add(bottomLeft); source.add(bottomRight); let startM = Converters.vector_Point2f_to_Mat(source); return startM; } function getEndM() { let ocvPOut1 = new Point(0, 0); let ocvPOut2 = new Point(resultWidth, 0); let ocvPOut3 = new Point(0, resultHeight); let ocvPOut4 = new Point(resultWidth, resultHeight); let dest = new ArrayList(); dest.add(ocvPOut1); dest.add(ocvPOut2); dest.add(ocvPOut3); dest.add(ocvPOut4); let endM = Converters.vector_Point2f_to_Mat(dest); return endM; }
声明
部分内容来自网络
本教程仅用于学习, 禁止用于其他用途