MATLAB: Histogram thresholding to get the threshold point

Image Processing Toolboximage segmentation

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
x=imread('tumor.jpg');
% 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

Best Answer

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?
Where did you upload your image?