imhist for an spm image
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Walter Roberson
2012-6-3
uint8(double(I) .* (255/1913))
2 个评论
Walter Roberson
2012-6-3
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.
更多回答(2 个)
Image Analyst
2012-6-3
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)]);
Brett Shoelson
2012-6-3
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 个评论
Image Analyst
2012-6-3
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!