# Histogram thresholding to get the threshold point

101 views (last 30 days)
Muhammad Ali Qadar on 28 Apr 2013
Commented: Image Analyst on 18 Jan 2017
hi,
I have been on image segmentation, till now I have divided image into two parts then I have taken histogram of those two parts, after substracting two histograms
1. I needed to choose threshold Value?
2. I want to compare each pixel value with threshold value of a zero matrix of same size as image
3. and if threshold value is less than pixel value it woould be assigned 0
What have I done that is not correct upto some extent is given below
% im=rgb2gray(x);
im=x(:,:,1);
[q r]=size(im);
s=r/2;
for i=1:q
for j=1:s
%n1(i,j)=im(i,j);
end
end
for k=1:q
for m=s:r
% n2(k,m)=im(k,m);
end
end
if true
%code
n1 = im(:, 1 : end/2); %image(x,y,c) c is matrix displayed as image
n2 = im(:, end/2+1 : end );%indicate last array index
figure, imshow(n1)
figure, imshow(n2)
figure, imhist(n1)
figure, imhist(n2)
if true
%code
diff=imhist(n2)-imhist(n1);
figure, bar(diff,'r')
thresh_level = graythresh(diff); %find best threshold level
c=zeros(size(im));
[r c1] = size(im);
allpix=im(r, c1);
for i=1:r
for j=1:c1
if allpix(i,j)> thresh_level
c=255;
else
c=0;
end
end
end
figure, imshow(c)
end

Image Analyst on 28 Apr 2013
I can't understand any rationale for that algorithm. What do you think thresholding the difference of the histograms will get you? What do you even think the difference of the histograms represents? Where did you come up with such an algorithm?
Muhammad Ali Qadar on 2 May 2013
well said, here is the paper link http://www.ijeat.org/attachments/File/V1Issue4/D0235031412.pdf/ May be I have read some paper that don't have any reputation, or may be the algorithm is designed just to show off the some work have been done, you got the nature of paper I have read. I would be glad to know if you can share what others ways you have that would be better than this method? However, thanks a lot.

### More Answers (2)

Iman Ansari on 28 Apr 2013
Hi.
% code
% im=rgb2gray(x);
im=x(:,:,1);
[q r]=size(im);
s=r/2;
% for i=1:q
% for j=1:s
% %n1(i,j)=im(i,j);
% end
% end
% for k=1:q
% for m=s:r
% % n2(k,m)=im(k,m);
% end
% end
if true
%code
n1 = im(:, 1 : end/2); %image(x,y,c) c is matrix displayed as image
n2 = im(:, end/2+1 : end );%indicate last array index
figure, imshow(n1)
figure, imshow(n2)
figure, imhist(n1)
figure, imhist(n2)
if true
%code
diff=imhist(n2)-imhist(n1);
figure, bar(diff,'r')
thresh_level = graythresh(diff); %find best threshold level
c=zeros(size(im));
[r c1] = size(im);
allpix=im;
allpix(allpix>thresh_level*255)=255;
allpix(allpix<=thresh_level*255)=0;
c=allpix;
% for i=1:r
% for j=1:c1
% if allpix(i,j)> thresh_level
% c=255;
% else
% c=0;
% end
% end
% end
figure, imshow(c)
end
end
Image Analyst on 18 Jan 2017
Take your output image use it as a mask and add something to it. Something like
shadows = output > 250; % Find white areas
Or multiply by a factor
brightnessFactor = 1.5;

Alex Taylor on 7 Nov 2016
Edited: Alex Taylor on 7 Nov 2016
If you are trying to divide the 1-D feature space of grayscale values into 2 classes, that is exactly what the traditional Otsu thresholding algorithm does.
This algorithm is implemented in the MATLAB Image Processing Toolbox as greythresh:
This is the standard approach to global thresholding for binary image segmentation problems. I haven't looked at your paper.