How to draw multiple lines on a image and calculate the all length for multiple image?

13 views (last 30 days)
How to draw multiple lines on a image and calculate the all length for multiple image?
  11 Comments
Nbillah
Nbillah on 2 Mar 2019
I already attached my original image. your one is also right. I have 24 slices like this from one object.
Image Analyst
Image Analyst on 2 Mar 2019
You did not attach the original image. You attached one with blue lines annotating it, and one with some other image stitched onto the right of it. See my answer below.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 2 Mar 2019
Edited: John Kelly on 7 Mar 2019
Try this:
clc; % Clear the command window.
clear all;
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'rnfl.png';
folder = pwd;
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, '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
%=======================================================================================
% Read in demo image.
grayImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
% Display image.
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis on;
caption = sprintf('Original Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Ask user to draw a line across the image.
message = sprintf('Draw the baseline across the gap.\nLeft click the left point.\nRight click the right point.');
text(5, 250, message, 'Color', 'r', 'FontSize', 16);
uiwait(msgbox(message));
[cx, cy, mask] = improfile();
hold on;
plot(cx, cy, 'r-', 'LineWidth', 2);
%===================================================================================
% Get a histogram
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Histogram of gray scale image', 'FontSize', fontSize, 'Interpreter', 'None');
% Convert to binary
binaryImage = grayImage > 29;
% Display image.
subplot(2, 2, 3);
imshow(binaryImage, []);
impixelinfo;
axis on;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Take the 2 largest blobs.
binaryImage = bwareafilt(binaryImage, 2);
% % fill any small holes in lines (not sure if needed)
% binaryImage = imfill(binaryImage,'holes');
% Display image.
subplot(2, 2, 3);
imshow(binaryImage, []);
impixelinfo;
axis on;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Draw line across binary image also.
hold on;
plot(cx, cy, 'r-', 'LineWidth', 2);
% For every cx, find the delta Y between cy and the top-most white pixel of the binary image.
deltaY = zeros(1, columns);
hold on;
for k = 1 : length(cx)
thisX = round(cx(k));
thisY = round(cy(k));
% Extract column of binaryImage.
thisColumn = binaryImage(:, thisX);
topLineOfBinaryImage = find(thisColumn, 1, 'first');
% Skip it if it's above the red line they drew.
if topLineOfBinaryImage < thisY
continue;
end
deltaY(thisX) = topLineOfBinaryImage - thisY;
if rem(thisX, 5) == 0
% Make blue line every 5 columns. We still have deltaY for every column though.
line([thisX, thisX], [thisY, topLineOfBinaryImage], 'Color', 'c');
end
end
% Get the mean height.
meanHeight = mean(deltaY(deltaY>0))
% Display plot.
subplot(2, 2, 4);
plot(deltaY, 'b-', 'LineWidth', 2);
grid on;
message = sprintf('Delta Y vs. Column. The mean height = %f', meanHeight);
title(message, 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Column', 'FontSize', fontSize, 'Interpreter', 'None');
ylabel('Delta Y', 'FontSize', fontSize, 'Interpreter', 'None');
% Draw a line across the plot at the mean height.
hold on;
line(xlim, [meanHeight, meanHeight], 'color', 'r', 'LineWidth', 2);
message = sprintf('The mean height = %f', meanHeight);
uiwait(helpdlg(message));
  2 Comments
Nbillah
Nbillah on 2 Mar 2019
Hi Image Analyst, thank you so much. The code is really helpful for me. You calculate the mean height. One question to ask, to see the variation of the length, should I calculate the variance of deltaY? And do the same process for all 24 slices, can I write like below?
baseFileName = dir(fullfile(F,'name*.png'))
Thank you again.
Image Analyst
Image Analyst on 2 Mar 2019
Use var():
meanVariance = var(deltaY(deltaY>0))
Or std() if you want the standard deviation.
meanStDev = std(deltaY(deltaY>0))
To process a sequence of files, see the FAQ

Sign in to comment.

More Answers (1)

Nbillah
Nbillah on 2 Mar 2019
Thank you Image Analyst for all codes and help. If you dont mind would you please delete the result image from your comment ? This works belongs to something confidential. And I dont know why the variance comes so high? Doesn't it calculate variance of the lengths only ?

Community Treasure Hunt

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

Start Hunting!