Plotting image color values-How to crop image

10 views (last 30 days)
Adrian
Adrian on 14 Nov 2023
Edited: DGM on 14 Nov 2023
I am processing a time-averaged image (the sequence is 20 images), and I am now plotting the colors in the averaged image. When plotting all of the colors, I get a lot of noise (unwanted bright colors). How can I only plot the colors in the pixels 82 to 102 (in the y axis)?
clc
clear all
%CHANGE THE FIRST IMAGE OF THE SEQUENCE HERE
filename = 'fleet1us_13010.tif';
%number of consecutive images you want to average:
images=20;
%set colormap
cmap = jet(256);
I0 = imread(filename);
lastTwoDigits = str2double(filename(end-5:end-4));
sumImage = double(I0); % Inialize to first image. The double function is used to convert
% the image data from the default integer data type to double-precision, which allows for more accurate arithmetic operations.
for i=(lastTwoDigits+1):(lastTwoDigits+images) % Read remaining images.
TifImage = imread(['fleet1us_130',num2str(i),'.tif']);
sumImage = sumImage + double(TifImage);
end
meanImage = sumImage / images;
% Get the image size in pixels.
[height, width] = size(meanImage);
%indicate pixel size (from calibration measurements)
pixelsize = 42.74;
figure;
% Display the mean image with the pixel scale.
imshow(meanImage, cmap);
axis on
set(gca, 'YDir', 'normal'); %set y axis direction (it was flipped for some reason)
% Add a title to the figure.
title('FLEET (60 kHz)');
colorbar; %add colorbar
% Get the color value of each pixel in the mean image.
meanImagePixelColors = meanImage(:);
% Create a new figure.
figure;
% Plot the color values of each pixel in the mean image.
plot(meanImagePixelColors);
% Set the x-axis label.
xlabel('Pixel Index');
% Set the y-axis label.
ylabel('Color Value');
% Add a title to the graph.
title('Graph of Color Values of Each Pixel in Mean Image');

Answers (1)

DGM
DGM on 14 Nov 2023
Edited: DGM on 14 Nov 2023
Barring the potential problems of using double() instead of im2double(), I see one problem with how you're using imshow().
If you have a simple single-channel intensity image, and you want it displayed in pseudocolor, you'd do it like
A = magic(10)/pi - 15; % a floating-point image on some arbitrary scale
% A is just an array of arbitrary numeric values
imshow(A,[]) % display the image colormapped with respect to its extrema
% you can also specify a particular desired range (e.g. [50 150])
colormap(jet(256))
colorbar
That's what the documentation refers to as "scaled" color data mapping. That's how imagesc() does it. The way you're trying to use imshow() is doing 'direct' CDataMapping. That's how you'd handle an indexed color image, not general intensity images. The fact that your results are averages tells me that they aren't indices.
[B map] = imread('canoe.tif'); % an indexed-color image and its color table
% B is an integer-valued array of row indices into map
imshow(B,map) % display the index array by direct indexing into map
colorbar
If we were to treat an intensity image as an index array, chances are we'd just get junk.
imshow(A,jet(256)) % pretend the values in A are row indices
colorbar
Since your map length is 256, and most of your data is around 100, you can expect that most of the image is solid cyan.

Products

Community Treasure Hunt

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

Start Hunting!