Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Histogram thresholding to get the threshold point

Asked by Muhammad Ali Qadar on 28 Apr 2013

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

0 Comments

Muhammad Ali Qadar

2 Answers

Answer by Image Analyst on 28 Apr 2013
Accepted 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?

3 Comments

Muhammad Ali Qadar on 28 Apr 2013

in fact difference of histogram will help me to get the threshold point in this article its written that peak value of difference of histogram can be taken as threshold point, its written here Manoj K Kowar and Sourabh Yadav"Brain Tumor Detction and Segmentation Using Histogram Thresholding " IJEAT 2012

Image Analyst on 28 Apr 2013

Well I don't have access to that paper. Yes you can get bright objects from histogram thresholding, so for that, I agree with the paper's title. However I don't agree that taking the histogram of the right and left halves of the image, and then subtracting the histograms will get you anything worthwhile. And, of course you could easily make up some obvious counter examples, like a centered bright ball or square on a dark background. The histogram of both halves would be identical, and subtracing the histograms would give all zero, and you couldn't get any threshold at all. I submit that you have misunderstood the algorithm in the paper. But anyway, here is what I think you were trying to do:

clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
imtool close all;  % Close all imtool figures if you have the Image Processing Toolbox.
clear;  % Erase all existing variables. Or clearvars if you want.
workspace;  % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Users\Mark\Documents\Temporary';
baseFileName = 'brain tumor.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
	% File doesn't exist -- didn't find it there.  Check the search path for it.
	fullFileName = baseFileName; % No path this time.
	if ~exist(fullFileName, 'file')
		% Still didn't find it.  Alert user.
		errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
		uiwait(warndlg(errorMessage));
		return;
	end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
	grayImage = rgb2gray(grayImage);
end
% Display the original gray scale image.
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage, 256);
subplot(2, 3, 4);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Extract left half and display histogram
middleColumn = floor(columns/2);
leftHalfImage = grayImage(:, 1:middleColumn);
subplot(2, 3, 2);
imshow(leftHalfImage, []);
title('left Half Image', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCountL, grayLevelsL] = imhist(leftHalfImage, 256);
subplot(2, 3, 5);
bar(pixelCountL);
grid on;
title('Histogram of left half image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Extract right half and display histogram
middleColumn = floor(columns/2);
rightHalfImage = grayImage(:, middleColumn+1:end);
subplot(2, 3, 3);
imshow(rightHalfImage, []);
title('Right Half Image', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCountR, grayLevelsR] = imhist(rightHalfImage, 256);
subplot(2, 3, 6);
bar(pixelCountR);
grid on;
title('Histogram of right half image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Now let's subtract the histograms (for some reason);
diffHistogram = int16(pixelCountL - pixelCountR);
% Display it
figure; % new figure
subplot(2, 2, 1);
bar(diffHistogram, 'BarWidth', 1);
title('Difference of Histograms', 'FontSize', fontSize);
% Now let's do an Otsu thresholding on it.
thresholdLevel = 255 * graythresh(diffHistogram)  % Find Otsu threshold level
% Place vertical bar on histogram to show threshold
yl = ylim();
line([thresholdLevel, thresholdLevel], [yl(1), yl(2)], ...
	'Color', 'r', 'LineWidth', 2);
grid on;
% Find pixels above the threshold
aboveThreshold = grayImage > thresholdLevel;
subplot(2, 2, 2);
imshow(aboveThreshold, []);
title('Above threshold', 'FontSize', fontSize);

But this is not a very robust algorithm at all. For one thing, it assumes that the image in a normal patient would be symmetrical about the center line. Also, as you can see there is a lot of "debris" in the image that is not the tumor. I can think of several very simple ways that would be better - more flexible and robust.

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.

Image Analyst
Answer by Iman Ansari on 28 Apr 2013

Hi.

% code
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;
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

2 Comments

Muhammad Ali Qadar on 28 Apr 2013

I want to detect the Tumor region but its still giving me image like this http://www.flickr.com/photos/95383933@N08/8689796446/in/photostream

can you tell me is there anyother way using this method I can detect on Dark Gray Region that is tumor.

Muhammad Ali Qadar on 28 Apr 2013

http://www.flickr.com/photos/95383933@N08/8689838816/in/photostream/lightbox/

Lets see above is the Original image can I get the white region from this image, using threshold

Iman Ansari

Contact us