How to draw multiple lines on a image and calculate the all length for multiple image?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nbillah
el 1 de Mzo. de 2019
Editada: John Kelly
el 7 de Mzo. de 2019
How to draw multiple lines on a image and calculate the all length for multiple image?
11 comentarios
Image Analyst
el 2 de Mzo. de 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.
Respuesta aceptada
Image Analyst
el 2 de Mzo. de 2019
Editada: John Kelly
el 7 de Mzo. de 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 comentarios
Image Analyst
el 2 de Mzo. de 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
Más respuestas (1)
Ver también
Categorías
Más información sobre Image Processing and Computer Vision en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!