Anderson85 asked . 2020-07-03

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

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

image processing , matlab

Expert Answer

Neeta Dsouza answered . 2024-12-24 17:15:01

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));


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.