思路:
把图片转为hsv
得到Mat中各个通道的均值,
提取亮度通道的平均值
console.time("导入类"); runtime.images.initOpenCvIfNeeded(); importClass(org.opencv.core.MatOfByte); importClass(org.opencv.core.Scalar); importClass(org.opencv.core.Point); importClass(org.opencv.core.CvType); importClass(java.util.List); importClass(java.util.ArrayList); 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(org.opencv.core.MatOfPoint2f); importClass(org.opencv.core.MatOfPoint); importClass(org.opencv.android.Utils); console.timeEnd("导入类"); // 第一种写法, 参数为文件路径 function getBrightnessHsv(imgPath) { let hsv = new Mat(); let src = Imgcodecs.imread(imgPath); Imgproc.cvtColor(src, hsv, Imgproc.COLOR_RGB2HSV); let avg = Core.mean(hsv); hsv.release(); src.release(); let result = avg.val[2]; log("the average brightness(HSV) is " + result); } getBrightnessHsv(); // 第二种写法, 参数为autojs的图片 function getBrightnessHsv(img) { let bitmap = img.getBitmap(); let src = new Mat(); Utils.bitmapToMat(bitmap, src); let hsv = new Mat(); Imgproc.cvtColor(src, hsv, Imgproc.COLOR_RGB2HSV); let avg = Core.mean(hsv); hsv.release(); src.release(); bitmap.recycle(); let result = avg.val[2]; log("the average brightness(HSV) is " + result); } getBrightnessHsv();
声明
部分内容来自网络
本教程仅用于学习, 禁止用于其他用途