How can I threshold an image?

278 Views Asked by At

I need to apply thresholding to an image which has already gone through a homomorphic filter.

My threshold value must be the mean + standard-deviation of image intensity.

I used the thresholding code by Jan Motl as follows:

function J = bernsen_thres(I)
    T = thres_val(I);

    J = bernsen(I, [T T],20,'replicate');
end

function T = thres_val(I)
    mn = mean(I(:));
    sd = std(double(I(:)));
    thres = round((mn+sd));

    if(is_odd(thres))
        T = thres;
    else
        T = thres+1;
    end

 function ret = is_odd(val)
    if(mod(val,2) == 0);
        ret = 0;
    else
        ret = 1;
    end

I used the homomorphic filter code from Steve Eddins as follows,

clear_all();

I = gray_imread('cameraman.png');
I = steve_homo_filter(I);

Ithres = bernsen_thres(I);

imshowpair(I, Ithres, 'montage')

But the output is totally black,

enter image description here

What should I do to fix this?

1

There are 1 best solutions below

0
On BEST ANSWER

Okay. I solved the problem.

clear_all();

I = gray_imread('cameraman.png');    

Ihmf = mat2gray(steve_homo_filter(I));

T = thres_val(I)/255

Ithres = Ihmf > T;

imshowpair(Ihmf, Ithres, 'montage');

There were two issues,

  1. Intensities of the output of the homomorphic filter were not between 0 and 1. I fixed that by applying mat2gray().

  2. Output of thres_val() was 181 in this case. That value was divided by 255 in order to bring it between 0 and 1.