image_blur_detection.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //
  2. // Created by 阳坤 on 2022/2/23.
  3. //
  4. #include "image_blur_detection.h"
  5. int image_blur_detection(const char *filename) {
  6. const char *temp_image_url = filename;
  7. cv::Mat cvImage = cv::imread(temp_image_url);
  8. return doDetection(cvImage);
  9. }
  10. int bytes_blur_detection(uint8_t *pData, int len) {
  11. cv::Mat img;
  12. std::vector<uchar> data;
  13. for (int i = 0; i < len; ++i) {
  14. data.push_back(pData[i]);
  15. }
  16. img = cv::imdecode(data, cv::IMREAD_COLOR);
  17. cv::flip(img, img, -1);
  18. img.channels();
  19. return doDetection(img);
  20. }
  21. int doDetection(cv::Mat cvImage) {
  22. //如果读入图像失败
  23. if (cvImage.empty()) {
  24. return -1;
  25. } else {
  26. cv::Mat grayCVImage;
  27. cv::cvtColor(cvImage, grayCVImage, cv::COLOR_BGR2GRAY);
  28. if (!grayCVImage.empty()) {
  29. cv::Mat laplacianImage;
  30. cv::Laplacian(grayCVImage, laplacianImage, CV_64F);
  31. cv::Scalar mu, sigma;
  32. cv::meanStdDev(laplacianImage, mu, sigma);
  33. cvImage.release();
  34. grayCVImage.release();
  35. return sigma.val[0] * sigma.val[0];
  36. }
  37. }
  38. return -1;
  39. }