MATLAB Answers

Triangle method of thresholding - The right way to go for this image?

47 views (last 30 days)
Joe Perkins
Joe Perkins on 11 Apr 2017
Edited: Joe Perkins on 12 Apr 2017
Hi - I'm after some advice with the triangle method (TM) for this image? Is it the right way to go? I used the TM demo to try and find a method of automatically identifying the threshold value to remove the background. TM gave me the blue line value, I know from manually playing around with an interactive tool that the threshold I want is at approx 40, the local minima in the bimodal distribution.
Many thanks!
%% Read image RGB image and convert to gray level %Reads RGB Ib=imread('LaLomaCoalOil.png'); %% [lehisto x]=imhist(Ib); [level]=triangle_th(lehisto,256); I_bw=im2bw(Ib,level);
function [level]=triangle_th(lehisto,num_bins)
% Find maximum of histogram and its location along the x axis [h,xmax]=max(lehisto); xmax=round(mean(xmax)); %can have more than a single value! h=lehisto(xmax);
% Find location of first and last non-zero values. % Values<h/10000 are considered zeros. indi=find(lehisto>h/10000); fnz=indi(1); lnz=indi(end);
% Pick side as side with longer tail. Assume one tail is longer. lspan=xmax-fnz; rspan=lnz-xmax; if rspan>lspan % then flip lehisto lehisto=fliplr(lehisto'); a=num_bins-lnz+1; b=num_bins-xmax+1; isflip=1; else lehisto=lehisto'; isflip=0; a=fnz; b=xmax; end
% Compute parameters of the straight line from first non-zero to peak % To simplify, shift x axis by a (bin number axis) m=h/(b-a);
% Compute distances x1=0:(b-a); y1=lehisto(x1+a); beta=y1+x1/m; x2=beta/(m+1/m); y2=m*x2; L=((y2-y1).^2+(x2-x1).^2).^0.5;
% Obtain threshold as the location of maximum L. level=find(max(L)==L); level=a+mean(level);
% Flip back if necessary if isflip level=num_bins-level+1; end
level=level/num_bins;

  0 Comments

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 11 Apr 2017
I don't think it would be robust enough. Depending on the exact heights of the bins, sometimes it might give you 60 while other times it would give you 40. Why not see what graythresh() gives you?

  1 Comment

Joe Perkins
Joe Perkins on 12 Apr 2017
Thanks for the answer. The comparison to greythresh() is on the same image but it picked a threshold value way of from the desired 40, similarily to the TM. Thanks

Sign in to comment.