measure the length in the image
9 次查看(过去 30 天)
显示 更早的评论
Hi,
How to measure the length of the continous portion of the object shown in the image?
0 个评论
采纳的回答
Image Analyst
2023-12-21
Try this:
% Tests curl for triggering stats analysis of laundry stain removal raw data.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
format compact;
%--------------------------------------------------------------------------------------------------------
% READ IN TEST IMAGE
folder = pwd;
baseFileName = "Image 2.bmp";
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
grayImage = imread(fullFileName);
% Get size
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels == 3
grayImage = grayImage(:,:,1);
end
% Display the image.
subplot(1, 2, 1);
imshow(grayImage);
axis('on', 'image');
impixelinfo;
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Threshold the image.
mask = ~imbinarize(grayImage);
% Take the largest blob
mask = bwareafilt(mask, 1);
% Display the image.
subplot(1, 2, 2);
imshow(mask);
axis('on', 'image');
impixelinfo;
title('Original Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Measure bounding box.
props = regionprops(mask, 'BoundingBox');
bb = props.BoundingBox;
hold on;
rectangle('Position', bb, 'EdgeColor', 'r', 'LineWidth', 2)
theHeight = bb(4);
message = sprintf('The height is %d pixels', theHeight)
title(message, 'FontSize', fontSize, 'Interpreter', 'None');
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Biomedical Imaging 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!