Reputation: 51
How to get only Otsu Threshold value using OpenCV like Matlab function graythresh.
For example, if I write this code I get binary image like using Matlab function im2bw:
Mat bw;
uint8_t data[14] = { 10, 10, 10, 10, 10, 10, 20, 20, 54, 54, 54, 54, 55, 55 };
Mat M(1, 14, CV_8UC1, &data);
threshold(M, bw, 0, 1, CV_THRESH_BINARY | CV_THRESH_OTSU);
Upvotes: 3
Views: 6343
Reputation: 275
If you want to avoid to execute the threshold and you just want the value, do what is inside OpenCV:
double getThreshVal_Otsu_8u( const cv::Mat& _src )
{
cv::Size size = _src.size();
if ( _src.isContinuous() )
{
size.width *= size.height;
size.height = 1;
}
const int N = 256;
int i, j, h[N] = {0};
for ( i = 0; i < size.height; i++ )
{
const uchar* src = _src.data + _src.step*i;
for ( j = 0; j <= size.width - 4; j += 4 )
{
int v0 = src[j], v1 = src[j+1];
h[v0]++; h[v1]++;
v0 = src[j+2]; v1 = src[j+3];
h[v0]++; h[v1]++;
}
for ( ; j < size.width; j++ )
h[src[j]]++;
}
double mu = 0, scale = 1./(size.width*size.height);
for ( i = 0; i < N; i++ )
mu += i*h[i];
mu *= scale;
double mu1 = 0, q1 = 0;
double max_sigma = 0, max_val = 0;
for ( i = 0; i < N; i++ )
{
double p_i, q2, mu2, sigma;
p_i = h[i]*scale;
mu1 *= q1;
q1 += p_i;
q2 = 1. - q1;
if ( std::min(q1,q2) < FLT_EPSILON || std::max(q1,q2) > 1. - FLT_EPSILON )
continue;
mu1 = (mu1 + i*p_i)/q1;
mu2 = (mu - q1*mu1)/q2;
sigma = q1*q2*(mu1 - mu2)*(mu1 - mu2);
if ( sigma > max_sigma )
{
max_sigma = sigma;
max_val = i;
}
}
return max_val;
}
Place it in your own namespace, or in an anonymous namespace, to avoid possible conflicts.
Upvotes: 3
Reputation: 5354
The function cv::threshold()
returns the computed threshold value if you set THRESH_OTSU
flag.
double thres_val = cv::threshold(M, bw, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
See cv::threshold() for more information.
Upvotes: 4