imhist for an spm image
3 views (last 30 days)
Show older comments
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;
uint8(double(I) .* (255/1913))
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.
More Answers (2)
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);
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);
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);
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);
title('Histogram of double image', 'FontSize', fontSize);
% Scale x axis manually.
Brett Shoelson on 3 Jun 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);
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.
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!