Plotting a profile for a line on an image

Dear all
I have an x-ray image (in the .tif format) of an object and I would like to draw a line across a part of the image and then plot the profile. I can do this step by step on ImageJ but I was wondering if I could do this in Matlab.
Thank you

 採用された回答

Image Analyst
Image Analyst 2013 年 2 月 11 日

0 投票

Use improfile(). If you want me to post a demo, let me know.

5 件のコメント

Image Analyst
Image Analyst 2013 年 2 月 11 日
% Code to spatially calibrate and image.
% Code asks user to draw a line and then to specify the length
% of the line in real world units. It then calculates a spatial calibration factor.
% User can then draw lines and have them reported in real world units.
% Take out the next two lines if you're transferring this to your program.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the gray scale image.
subplot(2, 1, 1);
imshow(grayImage, []);
title('Gray Scale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1], 'Menubar', 'none');
% Give a name to the title bar.
set(gcf,'name','Spatial Calibration Demo','numbertitle','off')
% Initialize
units = 'pixels';
spatialCalibration = 1.0;
button = 1;
while button ~= 3
% Get which action the user wants to do.
button = menu('Choose an action', 'Calibrate', 'Measure', 'Exit');
if button == 3
% Bail out because they clicked Exit.
break;
end
subplot(2, 1, 1);
% Clear any existing lines from the axes
axesHandlesToChildObjects = findobj(gca, 'Type', 'line');
if ~isempty(axesHandlesToChildObjects)
delete(axesHandlesToChildObjects);
end
% Make caption the instructions.
title('Left-click first point. Right click last point.', 'FontSize', fontSize);
% Ask user to plot a line.
[x, y, profile] = improfile();
hold on;
% Plot the line the user just drew.
plot(x,y, 'r-', 'LineWidth', 3);
% Restore caption.
title('Gray Scale Image', 'FontSize', fontSize);
% Calculate distance
distanceInPixels = sqrt((x(1)-x(end))^2 + (y(1)-y(end))^2);
% Plot it.
subplot(2,1,2);
plot(profile);
grid on;
% Initialize
realWorldNumericalValue = distanceInPixels;
caption = sprintf('Intensity Profile Along Line\nThe distance = %f pixels', ...
distanceInPixels);
title(caption, 'FontSize', fontSize);
ylabel('Gray Level', 'FontSize', fontSize);
xlabel('Pixels Along Line', 'FontSize', fontSize);
if button == 1
% They want to calibrate.
% Ask user for a number.
userPrompts = {'Enter true size:','Enter units:'};
defaultValues = {'180', 'cm'};
titleBar = 'Enter known distance';
caUserInput = inputdlg(userPrompts, titleBar, 2, defaultValues);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Initialize.
realWorldNumericalValue = str2double(caUserInput{1});
units = char(caUserInput{2});
% Check for a valid integer.
if isnan(realWorldNumericalValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
message = sprintf('I said it had to be an number.\nI will use %d and continue.', distanceInPixels);
uiwait(warndlg(message));
realWorldNumericalValue = distanceInPixels;
units = 'pixels';
spatialCalibration = 1.0;
% continue; % Skip to end of loop.
end
spatialCalibration = realWorldNumericalValue / distanceInPixels;
end
realWorldDistance = distanceInPixels * spatialCalibration;
caption = sprintf('Intensity Profile Along Line\nThe distance = %f pixels = %f %s', ...
distanceInPixels, realWorldDistance, units);
title(caption, 'FontSize', fontSize);
ylabel('Gray Level', 'FontSize', fontSize);
xlabel('Pixels Along Line', 'FontSize', fontSize);
end
UCL
UCL 2013 年 2 月 11 日
Thank you will try tomorrow!
UCL
UCL 2013 年 2 月 12 日
I get this : Undefined function 'imshow' for input arguments of type 'uint8'
when trying to perform imshow(grayImage, []); line.
Thank you
UCL
UCL 2013 年 2 月 12 日
Sorry. I just noticed that the PC im working on doesnt have the image processing toolbox. Is there any other way around this?
I tried doing this:
close all; clear all; clc;
% Define parameters % Path / Location of the Image path='U:\My pictures\'; % Filename of the image filename='Wire_A_segment.tif';
bob=strcat(path,filename)
A=imread(bob);
figure imagesc(A) colormap(gray) colorbar axis image
% Horizontal Profile Plot Hor_Profile_Plot=A(200,:); % e.g. the profile plot of the 200th row
figure plot(Hor_Profile_Plot)
But this is manual .
Image Analyst
Image Analyst 2013 年 2 月 13 日
You can use image() instead of imshow().

サインインしてコメントする。

その他の回答 (1 件)

UCL
UCL 2013 年 2 月 11 日

0 投票

Thank you for your reply. yes that would be great. My image is in the .tif format. I want to plot a profile of a horizontal line on the image.
THanx

3 件のコメント

Image Analyst
Image Analyst 2013 年 2 月 11 日
See my comment. In the future you can comment on my answers by posting a comment to them, rather than posting your comment as a brand new, independent Answer to your question.
shruti jain
shruti jain 2020 年 1 月 23 日
i have an image .i want to draw line profile perpendicular to this image .how to draw
it .please help me
Image Analyst
Image Analyst 2020 年 1 月 24 日
Call improfile() and then plot().

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

質問済み:

UCL
2013 年 2 月 11 日

コメント済み:

2020 年 1 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by