/
focusmetrics.cpp
executable file
·183 lines (173 loc) · 5.93 KB
/
focusmetrics.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "focusmetrics.h"
/************************************************
Auxiliary Functions
************************************************/
bool FM::FocusMetric::check_image( cv::Mat image ) const {
if(
image.empty() ||
image.rows < 1 ||
image.cols < 1
) {return false;}
return true;
}
/************************************************
Main focus measurement functions
************************************************/
//_______________________________________________
double FM::ThreshGradient::measure_focus( cv::Mat image ) const {
assert( check_image( image ) );
image.convertTo( image, CV_8UC1 );
double sum = 0;
for( int x = 0; x < image.cols; x++ ) {
for( int y = 0; y < image.rows; y++ ) {
uchar pixelXY = image.at<uchar>( y, x );
uchar pixelXY1;
if( ( x + 1 ) < image.cols ) {
pixelXY1 = image.at<uchar> ( y, x + 1 );
} else {
pixelXY1 = ( uchar )0;
}
uchar alce = ( int ) pixelXY1 - ( int ) pixelXY;
alce = alce * alce;
if( ( int ) alce >= ( int ) threshold )
sum += ( int ) alce;
}
}
return sum;
}
//_______________________________________________
double FM::BrennerGradient::measure_focus( cv::Mat image ) const {
assert( check_image( image ) );
image.convertTo( image, CV_8UC1 );
double sum;
for( int x = 0; x < image.cols; x++ ) {
for( int y = 0; y < image.rows; y++ ) {
uchar pixelXY = image.at<uchar>( y, x );
uchar pixelXY1;
if( ( x + 2 ) < image.cols ) {
pixelXY1 = image.at<uchar> ( y, x + 2 );
} else {
pixelXY1 = ( uchar )0;
}
uchar alce = ( int ) pixelXY1 - ( int ) pixelXY;
alce = alce * alce;
if( ( int ) alce >= ( int ) threshold )
sum += ( int ) alce;
}
}
return sum;
}
//_______________________________________________
double FM::ImagePower::measure_focus( cv::Mat image ) const {
assert( check_image( image ) );
image.convertTo( image, CV_8UC1 );
double sum;
for( int x = 0; x < image.cols; x++ ) {
for( int y = 0; y < image.rows; y++ ) {
uchar pixelXY = image.at<uchar>( x, y );
if( ( int )pixelXY >= ( int ) threshold ) {
sum += ( int )pixelXY * ( int ) pixelXY;
}
}
}
return sum;
}
//_______________________________________________
double FM::TenenbaumGradient::measure_focus( cv::Mat image ) const {
assert( check_image( image ) );
image.convertTo( image, CV_8UC1 );
cv::Mat processedX, processedY, total;
cv::Sobel( image, processedX, CV_16S, 1, 0 );//Calculates sobel for X cordinates
cv::pow( processedX, 2, processedX );//Squares the result
cv::Sobel( image, processedY, CV_16S, 0, 1 );//Calculates sobel for Y cordinates
cv::pow( processedY, 2, processedY );
cv::add( processedX, processedY, total );
cv::Scalar totalSum = cv::sum( total );
return totalSum[0];
}
//_______________________________________________
double FM::LaplacianEnergy::measure_focus( cv::Mat image ) const {
assert( check_image( image ) );
image.convertTo( image, CV_8UC1 );
cv::Mat processed;
cv::Laplacian( image, processed, CV_8U );
cv::pow( processed, 2, processed );
return cv::sum( processed )[0];
}
//_______________________________________________
double FM::ThresholdedHistogram::measure_focus( cv::Mat image ) const {
assert( check_image( image ) );
image.convertTo( image, CV_8UC1 );
ImageHistogram * histogram = new ImageHistogram( bins, image );
int smallerBin = bins + 1, biggestBin = 0;
for( int bin = 1; bin <= bins; bin++ ) {
if( histogram->pixelsAboveThreshold( bin, threshold ) ) {
smallerBin = bin < smallerBin ? bin : smallerBin;
biggestBin = bin > biggestBin ? bin : biggestBin;
}
}
int pixelPerBin = floor( 256 / bins );
int smallerPixelRange = ( -1 * ( pixelPerBin ) ) + smallerBin * pixelPerBin;
int biggestPixelRange = pixelPerBin * biggestBin;
return ( double )biggestPixelRange - smallerPixelRange;
}
//_______________________________________________
double FM::NormalizedVariance::measure_focus( cv::Mat image ) const {
assert( check_image( image ) );
image.convertTo( image, CV_8UC1 );
double mean = cv::mean( image )[0];
double sum = 0;
for( int row = 0; row < image.rows; row++ ) {
for( int column = 0; column < image.cols; column ++ ) {
uchar pixel = image.at<uchar>( row, column );
sum += std::pow( ( pixel - mean ), 2 );
}
}
sum = sum / mean;
return sum;
}
//_______________________________________________
double FM::AutoCorrelation::measure_focus( cv::Mat image ) const {
assert( check_image( image ) );
image.convertTo( image, CV_8UC1 );
double part1 = 0, part2 = 0;
for( int row = 0; row < image.rows; row++ ) {
for( int column = 0; column < image.cols; column ++ ) {
if( row + 1 < image.rows ) {
part1 += image.at<uchar>( row, column ) * image.at<uchar>( row + 1, column );
}
if( row + 2 < image.rows ) {
part2 += image.at<uchar>( row, column ) * image.at<uchar>( row + 2, column );
}
}
}
return std::abs( part1 - part2 );
}
//_______________________________________________
double FM::StandartDeviationCorrelation::measure_focus( cv::Mat image ) const {
assert( check_image( image ) );
image.convertTo( image, CV_8UC1 );
double mean = cv::mean( image )[0], part1;
for( int row = 0; row < image.rows; row++ ) {
for( int column = 0; column < image.cols; column ++ ) {
if( row + 1 < image.rows ) {
part1 += std::abs( image.at<uchar>( row, column ) * image.at<uchar>( row + 1, column ) - mean );
}
}
}
return part1;
}
//_______________________________________________
double FM::PixelCount::measure_focus( cv::Mat image )const {
assert( check_image( image ) );
image.convertTo( image, CV_8UC1 );
int sum = 0;
for( int row = 0; row < image.rows; row++ ) {
for( int column = 0; column < image.cols; column ++ ) {
if( image.at<uchar>( row, column ) <= threshold ) {
sum++;
}
}
}
return sum;
}