Browse Source

模糊检测开发完成

DevYK 3 năm trước cách đây
mục cha
commit
f0fa26ec95

+ 0 - 20
cmake-build-debug/CMakeFiles/ImageBlurDetection_debug.dir/CXX.includecache

@@ -6,26 +6,6 @@
 
 #IncludeRegexTransform: 
 
-/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/include/darwin/jni_md.h
-
-/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/include/jni.h
-stdio.h
--
-stdarg.h
--
-jni_md.h
-/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/include/jni_md.h
-
-/Users/devyk/Data/Project/sample/github_code/OpenCVSample/jni/ImageBlurDetection.cpp
-ImageBlurDetection.h
-/Users/devyk/Data/Project/sample/github_code/OpenCVSample/jni/ImageBlurDetection.h
-
-/Users/devyk/Data/Project/sample/github_code/OpenCVSample/jni/ImageBlurDetection.h
-jni.h
-/Users/devyk/Data/Project/sample/github_code/OpenCVSample/jni/jni.h
-../opencv/image_blur_detection.h
-/Users/devyk/Data/Project/sample/github_code/OpenCVSample/opencv/image_blur_detection.h
-
 /Users/devyk/Data/Project/sample/github_code/OpenCVSample/opencv/image_blur_detection.cpp
 image_blur_detection.h
 /Users/devyk/Data/Project/sample/github_code/OpenCVSample/opencv/image_blur_detection.h

BIN
cmake-build-debug/CMakeFiles/ImageBlurDetection_debug.dir/main.cpp.o


BIN
cmake-build-debug/CMakeFiles/ImageBlurDetection_debug.dir/opencv/image_blur_detection.cpp.o


+ 2 - 2
cmake-build-debug/Testing/Temporary/LastTest.log

@@ -1,3 +1,3 @@
-Start testing: Feb 23 22:26 CST
+Start testing: Feb 23 23:29 CST
 ----------------------------------------------------------
-End testing: Feb 23 22:26 CST
+End testing: Feb 23 23:29 CST

+ 3 - 2
main.cpp

@@ -4,7 +4,8 @@
 
 
 static void *detection_thread(void *arg) {
-    const char *filename = "/Users/devyk/Downloads/IMG_3067.PNG";
+//    const char *filename = "/Users/devyk/Downloads/IMG_3067.PNG";
+    const char *filename = "/Users/devyk/Downloads/black.png";
     FILE *fp = fopen(filename, "rb");
     if (!fp) return reinterpret_cast<void *>(-1);
     fseek(fp, 0L, SEEK_END);
@@ -13,7 +14,7 @@ static void *detection_thread(void *arg) {
     rewind(fp);
     uint8_t *data = (uint8_t *) malloc(sizeof(uint8_t) * size);
     size_t size2 = fread(data, size, 1, fp);
-    printf("图片模糊度检测 %d \n", image_blur_detection("/Users/devyk/Downloads/IMG_3067.PNG"));
+//    printf("图片模糊度检测 %d \n", image_blur_detection("/Users/devyk/Downloads/IMG_3067.PNG"));
     printf("图片模糊度检测 %d \n", bytes_blur_detection(data,size));
     free(data);
 //    printf("图片模糊度检测 %d \n", image_blur_detection("/Users/devyk/Downloads/black.png"));

+ 43 - 1
opencv/image_blur_detection.cpp

@@ -35,10 +35,52 @@ int doDetection(cv::Mat cvImage) {
             cv::Laplacian(grayCVImage, laplacianImage, CV_64F);
             cv::Scalar mu, sigma;
             cv::meanStdDev(laplacianImage, mu, sigma);
+            int blur_value = sigma.val[0] * sigma.val[0];
+            blur_value = blur_value < 100 ? image_color_ratio(grayCVImage) : blur_value;
             cvImage.release();
             grayCVImage.release();
-            return sigma.val[0] * sigma.val[0];
+            return blur_value;
         }
     }
     return -1;
 }
+
+//定义比较的函数
+static bool cmp_value(const std::pair<long, long> left, const std::pair<long, long> right) {
+    return left.second < right.second;
+}
+
+int image_color_ratio(cv::Mat image) {
+    int num = 0;//记录颜色的像素点
+    std::map<long, long> colors;
+    for (int i = 0; i < image.rows; ++i) {
+        for (int j = 0; j < image.cols; ++j) {
+            int gray_data = image.at<uchar>(i, j);
+            if (colors.count(gray_data) == 0) {
+                colors[gray_data] = 1;
+            } else {
+                long a = ++colors[gray_data];
+                colors[gray_data] = a;
+            }
+        }
+    }
+    if (!colors.empty()) {
+        auto maxCount = 0L;
+        if (colors.size() > 1) {
+            maxCount = max_element(colors.begin(), colors.end(), cmp_value)->second;
+        } else {
+            std::map<long, long>::iterator iter;
+            iter = colors.begin();
+            while (iter != colors.end()) {
+                maxCount = iter->second;
+                break;
+            }
+        }
+
+        float i = maxCount * 1.0f / (image.rows * image.cols);
+        if (i >= DEFAULT_PIX_THRESHOLD) {
+            num = DEFAULT_BLUR_THRESHOLD + 1;
+        };
+    }
+    return num;
+}

+ 12 - 1
opencv/image_blur_detection.h

@@ -5,6 +5,15 @@
 #ifndef JNI_IMAGE_BLUR_DETECTION_H
 #define JNI_IMAGE_BLUR_DETECTION_H
 
+/**
+ * 默认模糊阀值
+ */
+#define DEFAULT_BLUR_THRESHOLD  100
+/**
+ * 默认像素值阀值
+ */
+#define DEFAULT_PIX_THRESHOLD  0.7f
+
 #include "opencv2/opencv.hpp"
 #include "opencv2/imgcodecs.hpp"
 #include "vector"
@@ -13,7 +22,9 @@ int image_blur_detection(const char *filename);
 
 int doDetection(cv::Mat mat);
 
-int bytes_blur_detection(uint8_t *data,int len);
+int bytes_blur_detection(uint8_t *data, int len);
+
+int image_color_ratio(cv::Mat image);
 
 
 #endif //JNI_IMAGE_BLUR_DETECTION_H

BIN
output/bin/ImageBlurDetection_debug