From ca97197ae300e3046e39efb6022e72d6bf660145 Mon Sep 17 00:00:00 2001 From: divenswu Date: Wed, 16 Aug 2023 15:42:51 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E6=B7=BB=E5=8A=A0=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E7=9A=84=E6=A8=A1=E7=B3=8A=E5=BA=A6=EF=BC=8C=E5=B9=B6=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C0-100=E7=9A=84=E9=87=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../face/search/core/utils/QualityUtil.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 face-search-core/src/main/java/com/visual/face/search/core/utils/QualityUtil.java diff --git a/face-search-core/src/main/java/com/visual/face/search/core/utils/QualityUtil.java b/face-search-core/src/main/java/com/visual/face/search/core/utils/QualityUtil.java new file mode 100644 index 0000000..abfabac --- /dev/null +++ b/face-search-core/src/main/java/com/visual/face/search/core/utils/QualityUtil.java @@ -0,0 +1,70 @@ +package com.visual.face.search.core.utils; + +import org.opencv.core.*; +import org.opencv.imgproc.Imgproc; + +public class QualityUtil { + + public static int blurForLaplacian(Mat image){ + Mat imgDesc = null, imgResize = null; + Mat img2gray = null, laplacian = null; + MatOfDouble std = null, median = null; + try { + //裁剪图片的中心位置区域 + if(image.width() > image.height()){ + int offset = (image.width() - image.height()) / 2; + imgDesc = new Mat(image.height(), image.height(), CvType.CV_8UC3); + Mat imgROI = new Mat(image, new Rect(offset, 0, image.height(), image.height())); + imgROI.copyTo(imgDesc); + }else if(image.height() < image.width()){ + int offset = (image.height() - image.width()) / 2; + imgDesc = new Mat(image.width(), image.width(), CvType.CV_8UC3); + Mat imgROI = new Mat(image, new Rect(0, offset, image.height(), image.height())); + imgROI.copyTo(imgDesc); + }else{ + imgDesc = new Mat(image.width(), image.height(), CvType.CV_8UC3); + image.copyTo(imgDesc); + } + //将图片都转换为112X112的分辨率 + imgResize = new Mat(); + Imgproc.resize(imgDesc, imgResize, new Size(112,112), 0, 0, Imgproc.INTER_AREA); + //图像处理 + img2gray = new Mat(); + Imgproc.cvtColor(imgResize, img2gray, Imgproc.COLOR_BGR2GRAY); + laplacian = new Mat(); + Imgproc.Laplacian(img2gray, laplacian, CvType.CV_64F); + //获取模糊分 + std= new MatOfDouble(); + median = new MatOfDouble(); + Core.meanStdDev(laplacian, median , std); + double var = Math.pow(std.get(0,0)[0], 2); + //将模糊分设置到0-100的值域中 + var = Math.min(var, 1500); + double score = Math.sin(var / 1500 * Math.PI * 0.5); + int normal = Double.valueOf(score * 100).intValue(); + return Math.max(Math.min(normal, 100), 0); + }catch (Exception e){ + throw new RuntimeException(e); + }finally { + if(null != std){ + std.release(); + } + if(null != median){ + median.release(); + } + if(null != laplacian){ + laplacian.release(); + } + if(null != img2gray){ + img2gray.release(); + } + if(null != imgResize){ + imgResize.release(); + } + if(null != imgDesc){ + imgDesc.release(); + } + } + } + +}