plotting intensity distributions on an image

I have an image and i can calculate sum of intensities along x and y axis using following code. i want to plot the sum of intensities as histograms on the image along x and y axis, like the example attached. Many thanks for the help
X1=imread('image.png');
intensity_x=sum(X1); %row vector containing the sum of each column.
intensity_y=sum(X1,2); %column vector containing the sum of each row.

4 comentarios

Ameer Hamza
Ameer Hamza el 12 de Nov. de 2020
Can you show an example?
Sumera Yamin
Sumera Yamin el 12 de Nov. de 2020
Like attached figure
Hello Sumera, still the question is not clear, as the text descriptions of thr questions, it may be-
X1=imread('image.png');
intensity_x=sum(X1); %row vector containing the sum of each column.
intensity_y=sum(X1'); %column vector containing the sum of each row.
plot(intensity_x,intensity_y);
or Are you looking for intensity surf plot, as below?
Please describe the question clearly with an example of 3x3 matrix?
Sumera Yamin
Sumera Yamin el 13 de Nov. de 2020
Editada: Sumera Yamin el 13 de Nov. de 2020
Hi, many thanks for your answer, i am looking for a plot like the attached picture with original question i attached a sample image here.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 13 de Nov. de 2020
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
fprintf('Beginning to run %s.m ...\n', mfilename);
echo off;
%===============================================================================
% Read in gray scale image.
baseFileName = 'moon.tif';
grayImage = imread(baseFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(grayImage);
% If it's RGB instead of grayscale, convert it to gray scale.
if numberOfColorBands > 1
grayImage = rgb2gray(grayImage);
end
% Display the original image.
subplot(2, 2, 4);
imshow(grayImage);
axis on;
caption = sprintf('Original Image : %s', baseFileName);
title(caption, 'FontSize', fontSize);
impixelinfo;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.1, 1, 0.9]);
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels == 3
grayImage = rgb2gray(grayImage);
end
% Plot sum of columns -- vertically sum all columns within each row.
verticalSum = sum(grayImage, 2);
subplot(2, 2, 3); % Switch to the axes to the left of the image axes.
plot(verticalSum, 1:rows, 'b-', 'LineWidth', 2);
axis ij; % Invert vertical axis to match the image which has row 1 at the top.
ylabel('Row', 'FontSize', fontSize);
xlabel('Sum of Gray Levels', 'FontSize', fontSize);
ylim([1, rows]);
grid on;
% Plot sum of rows -- vertically sum all rows within each column.
horizontalSum = sum(grayImage, 1);
subplot(2, 2, 2); % Switch to the axes above the image axes.
plot(1 : columns, horizontalSum, 'b-', 'LineWidth', 2);
xlabel('Column', 'FontSize', fontSize);
ylabel('Sum of Gray Levels', 'FontSize', fontSize);
xlim([1, columns]);
grid on;

7 comentarios

Sumera Yamin
Sumera Yamin el 14 de Nov. de 2020
hi, this works fine. Thanks you very much. just one last thing remaining. How can i make projections fit with the dimensions of original image (like the sample image attached with the question).
You can adjust the plot sizes with the 'Position' property. Like
ax1 = subplot(2, 2, 1);
ax1.Position = [x, y, width, height]; % Put in values to get the exact size you want.
Sumera Yamin
Sumera Yamin el 16 de Nov. de 2020
many thanks for your answers. They are really helpful. One last thing, if we read image, with format
"511x646x3 uint8", what does "x3" means and how is it different from images having format "511x646"?
The x3 means that there are three 511 rows-by-646 column arrays. They represent the red, green, and blue color channels of an RGB image. If it did not have the x3, it would be a monochrome, gray scale image, not color.
redChannel = rgbImage(:, :, 1); % The red channel is the first plane (slice)
greenChannel = rgbImage(:, :, 2); % The green channel is the second plane (slice)
blueChannel = rgbImage(:, :, 3); % The blue channel is the third plane (slice)
Sumera Yamin
Sumera Yamin el 16 de Nov. de 2020
Editada: Sumera Yamin el 16 de Nov. de 2020
thanks. if instead of analyzing an image file, i want to analyze the image data(imported as a matrix). How do i load a variable with image data? imread always takes in afile name not a variable as a matrix, representing image data (because if i analyze an image file, the projections also includes the axis tickmarks and labels in the image and this does not give me correct information.)
Image Analyst
Image Analyst el 16 de Nov. de 2020
An image file will not have tick marks and axes unless you saved the image that way for example if you used saveas() instead of imwrite(). So use imread() to read in the image. Just make sure the image was saved in such a way that it's really the actual image not a screenshot like saveas() or exportgraphics() will give you.
Sumera Yamin
Sumera Yamin el 16 de Nov. de 2020
Editada: Sumera Yamin el 16 de Nov. de 2020
right, i was using saveas() to sve my images. works fine now...many thanks for your useful answers.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 13 de Nov. de 2020
I gave you an answer here in this link
What I've done in this case is to have two additional axes, one on the left of the image axes, and one below or above it. I plot the vertical profile along the one on the left, with x and y switched, and the horizontal profile along the axes underneath the image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels == 3
grayImage = rgb2gray(grayImage);
end
verticalSum = sum(grayImage, 2);
horizontalSum = sum(grayImage, 1);
axes(handles.axesLeft); % Switch to the axes to the left of the image axes.
plot(verticalSum, 1:rows, 'b-', 'LineWidth', 2);
grid on;
axes(handles.axesBelow); % Switch to the axes below the image axes.
plot(1 : columns, horizontalSum, 'b-', 'LineWidth', 2);
grid on;

3 comentarios

Sumera Yamin
Sumera Yamin el 13 de Nov. de 2020
Editada: Sumera Yamin el 13 de Nov. de 2020
hi, thanks for your answer. i use above lines of code and the matlab gives me following error "Undefined variable "handles" or class "handles.axesLeft"." sorry for being so naive, how do i assign value to handles?
Image Analyst
Image Analyst el 13 de Nov. de 2020
I guess you're not using a GUIDE GUI. Just replace handles.axesLeft with whatever the handles to those axes are. Not the main image axes, but the two additional ones you set up beside it to show the profiles.
Sumera Yamin
Sumera Yamin el 13 de Nov. de 2020
yes, i am not using guide gui, and i still dont understand what is meant by "handels to those axis". I have not set up any. Also if i replace '.axesleft', how would matlb know, where to plot ?

Iniciar sesión para comentar.

Categorías

Más información sobre Printing and Saving en Centro de ayuda y File Exchange.

Preguntada:

el 12 de Nov. de 2020

Editada:

el 16 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by