How to plot both horizontal and vertical profile for the same image?

1 view (last 30 days)
Hi. I'd like to plot a line profile across the beam and plot both horizontal and vertical profile on the same image. I manage to get one. I cannot plot the vertical profile and make it on the image together. Please give me any suggestion and help.Thank you.
% code
clc;close all;clear all;
img=imread('e_foil_spot.png');
img2=imcrop(img);
figure(1), imshow(img2);
c= improfile; hold on;d=improfile; % get the lime profile across
b=c';
% Plot the line profile on the same image
figure(1); imshow(img2); colormap('jet');
hold on; plot(b/(0.03*max(b)),'color', 'g','LineWidth', '10');
hold on; plot(d/(0.03*max(d)),'color', 'y','LineWidth', '10');
hold off

Accepted Answer

Image Analyst
Image Analyst on 7 Feb 2018
Edited: Image Analyst on 7 Feb 2018
Try this:
grayImage = imread('moon.tif');
[rows, columns, numberOfColorChannels] = size(grayImage);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
imshow(grayImage, []);
axis on;
verticalProfile = mean(grayImage, 2);
horizontalProfile = mean(grayImage, 1);
hold on;
% Plot horizontal profile.
x = 1 : columns;
amplitude = 2; % Scaling factor
y = rows - amplitude * horizontalProfile;
plot(x, y, 'r-', 'LineWidth', 2);
% Plot vertical profile.
y = 1 : rows;
amplitude = 1; % Scaling factor
x = columns - amplitude * verticalProfile; % To plot on RIGHT side
x = amplitude * verticalProfile; % To plot on LEFT side
plot(x, y, 'm-', 'LineWidth', 2);
If you'd rather, you can do
verticalProfile = grayImage(:, columnIndex);
horizontalProfile = grayImage(rowIndex, :);
where you specify the row and column where you want the profile to go through.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!