-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_focus.cpp
38 lines (29 loc) · 1009 Bytes
/
image_focus.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//> g++ -o image_focus image_focus.cpp $(pkg-config --libs --cflags opencv4)
#include </usr/include/opencv4/opencv2/opencv.hpp>
#include <iostream>
double variance_of_laplacian(cv::Mat image) {
cv::Mat lap;
cv::Laplacian(image, lap, CV_64F);
cv::Scalar mu, sigma;
cv::meanStdDev(lap, mu, sigma);
return sigma.val[0] * sigma.val[0];
}
int main(int argc, char** argv) {
if (argc != 2) {
std::cout << "Usage: ./focus_check <image_path>\n";
return -1;
}
cv::Mat src = cv::imread(argv[1], cv::IMREAD_GRAYSCALE);
if (src.empty()) {
std::cout << "Could not open or find the image\n";
return -1;
}
double fm = variance_of_laplacian(src);
std::cout << "Focus measure: " << fm << "\n";
if (fm < 100.0) { // Threshold is hypothetical and should be determined based on your specific requirements.
std::cout << "The image is blurry.\n";
} else {
std::cout << "The image is in focus.\n";
}
return 0;
}