imhist for an spm image

2 vues (au cours des 30 derniers jours)
Fatma Gargouri
Fatma Gargouri le 3 Juin 2012
Hello,
I'm working on spm images and their intensity are between 0 and 1913!! I would first of all create an histogram for this image.. so could I modify the imhist code and have you an idea how to creat this histogram ?
second, I would convert their values to [0 255] but it doesn't work
I = imread('spmimg.img'); X=(I.*255)./1913;
thank you

Réponse acceptée

Walter Roberson
Walter Roberson le 3 Juin 2012
uint8(double(I) .* (255/1913))
  2 commentaires
Fatma Gargouri
Fatma Gargouri le 3 Juin 2012
thank you
it works so good :))
but what is the difference between X=(I.*255)./1913;
and your code ?
what is the secret of uint8 ? :D
( I am a beginner )
Walter Roberson
Walter Roberson le 3 Juin 2012
Grouping (255/1913) is just efficiency.
double(I) takes care of the fact that your image is probably one of the integer data classes. Unexpected things happen when you do arithmetic on integer classes (well, unexpected until you learn to expect them -- they make sense when you read the documentation.)
uint8() converts the result to an integer data type that imhist() can deal with properly. imhist() looks at the data type in order to figure out what the expected range of values should be.

Connectez-vous pour commenter.

Plus de réponses (2)

Image Analyst
Image Analyst le 3 Juin 2012
What is the class of I (very bad name to pick, by the way)? If it's uint16, just call imhist. If it's something else you need to convert I to 0-1, not 0-255. I see no reason to convert to 255 unless you're just interested in seeing the general shape of the histogram but with incorrect values for the bin values. Try this to get accurate bin values.
maxValue = max(I(:));
X = I/maxValue;
[pixelCounts grayLevels] = imhist(X);
originalGrayLevels = grayLevels * maxValue;
Next, please study this demo of mine. You might find it informative:
% function double_image_histogram()
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Read in standard MATLAB gray scale demo image.
grayImage = imread('cameraman.tif');
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Let's get its histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 3);
bar(pixelCount);
grid on;
xlim([0 grayLevels(end)]); % Scale x axis manually.
title('Original Grayscale Histogram', 'FontSize', fontSize);
% Now let's synthesize an image that is a floating point
% image and has values from -12345 to +54321.
originalMinValue = double(min(min(grayImage)))
originalMaxValue = double(max(max(grayImage)))
originalRange = originalMaxValue - originalMinValue;
% Get a double image in the range -127 to +128
desiredMin = -45678;
desiredMax = 54321;
desiredRange = desiredMax - desiredMin;
dblImageS = desiredRange * (double(grayImage) - originalMinValue) / originalRange + desiredMin;
% So now we have a double image that is our "starting image."
% However can't use imhist on this. We need to scale to 0-1.
minValueS = min(min(dblImageS))
maxValueS = max(max(dblImageS))
rangeS = maxValueS - minValueS;
dblImage = (dblImageS - minValueS) / rangeS;
% Check to verify that range is now 0-1.
minValueNorm = min(min(dblImage))
maxValueNorm = max(max(dblImage))
subplot(2, 2, 2);
imshow(dblImage, []);
title('Double Image', 'FontSize', fontSize);
% Let's get its histogram into 256 bins.
[pixelCountD grayLevelsD] = imhist(dblImage, 256);
% But now grayLevelsD goes from 0 to 1.
% We want it to go from the original range, so we need to scale.
originalDoubleGrayLevels = rangeS * grayLevelsD + minValueS;
subplot(2, 2, 4);
plot(originalDoubleGrayLevels, pixelCountD);
grid on;
title('Histogram of double image', 'FontSize', fontSize);
% Scale x axis manually.
xlim([originalDoubleGrayLevels(1) originalDoubleGrayLevels(end)]);
  1 commentaire
Fatma Gargouri
Fatma Gargouri le 3 Juin 2012
thank you I will study this demo it s very helpful :))

Connectez-vous pour commenter.


Brett Shoelson
Brett Shoelson le 3 Juin 2012
I might suggest that you look at the documentation for IMADJUST. It's made for this. In fact, Image Analyst, after you created your synthetic image on could have cast it directly to the range [0,1] with a single call to IMADJUST:
>> newDblImage = imadjust(dblImage,[min(dblImage(:)); max(dblImage(:))],[0 1]);
>> newUint8 = im2uint8(newDblImage);
  2 commentaires
Fatma Gargouri
Fatma Gargouri le 3 Juin 2012
thx I looked at the documentation for IMADJUST and it seems very simple :))
your code works :) thx
Image Analyst
Image Analyst le 3 Juin 2012
Yes, I was just using the way that he was familiar with already. imadjust is good, and there is also the mat2gray() function in base MATLAB for those unfortunate people who don't have the Image Processing Toolbox.

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by