is their any way to calculate automatically this parameter from the image ?

1 view (last 30 days)
Hi all,
I am looking for the calculate the the parameter mentioned in the image, Is that possible to calculate from the image ?
(I have attached the original image and other one attached to understand you that which value i need to calculate, (Black width, highlighted with the lime colour))Thank you.
  3 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 27 Feb 2021
Of course it's possible. Try this. Adapt as needed:
% Demo by Image Analyst, February, 2021.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'original image.JPG';
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);
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels == 3
grayImage = rgb2gray(grayImage);
end
% Display the test image full size.
subplot(2, 3, 1);
imshow(grayImage, []);
axis('on', 'image');
caption = sprintf('Original Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% 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.
hFig1.Name = 'Demo by Image Analyst';
%--------------------------------------------------------------------------------------------------------
% Get histogram
subplot(2, 3, 2);
imhist(grayImage);
grid on;
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
% Threshold image.
mask = grayImage < 75;
% Clean up the mask to get only the stripes we want.
mask(:, 1 : 762) = false;
mask(:, 2615 : end) = false;
% Get rid any blob not 1500 wide
labeledImage = bwlabel(mask);
props = regionprops(labeledImage, 'BoundingBox');
allBB = vertcat(props.BoundingBox);
widths = allBB(:, 3);
wideBlobs = find(widths > 1500);
% Extract only those that are wide enough.
mask = ismember(labeledImage, wideBlobs);
% Display mask image.
subplot(2, 3, 3);
imshow(mask, []);
axis('on', 'image');
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hold on;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage, numberOfBlobs] = bwlabel(mask, 8);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
subplot(2, 3, 4);
imshow(coloredLabelsImage);
title('Labeled Blobs', 'FontSize', fontSize, 'Interpreter', 'None');
% Get all the blob properties. Can only pass in originalImage in version R2008a and later.
blobMeasurements = regionprops(labeledImage, grayImage, 'all');
numberOfBlobs = size(blobMeasurements, 1);
% For each blob, for each line, get the left edge and right edge then widths.
leftEdges = zeros(1, columns);
rightEdges = zeros(1, columns);
widths = zeros(columns, numberOfBlobs);
for blob = 1 : numberOfBlobs
thisBlob = ismember(labeledImage, blob);
% Display mask image.
subplot(2, 3, 5);
imshow(thisBlob, []);
axis('on', 'image');
caption = sprintf('Mask of Blob #%d of %d', blob, numberOfBlobs);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
for col = 1 : columns
thisCol = thisBlob(:, col);
t = find(thisCol, 1, 'first');
if ~isempty(t)
leftEdges(col) = t;
rightEdges(col) = find(thisCol, 1, 'last');
end
end
widths(:, blob) = (rightEdges - leftEdges)';
subplot(2, 3, 6);
plot(widths, '-', 'LineWidth', 1);
hold on;
grid on;
drawnow;
legendStrings{blob} = sprintf('Blob #%d', blob);
end
grid on;
title('Widths', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Row', 'FontSize', fontSize);
ylabel('Width in Pixels', 'FontSize', fontSize);
legend(legendStrings);
% Display mask image again.
subplot(2, 3, 5);
imshow(mask, []);
axis('on', 'image');
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hold on;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
fprintf('Done running %s.m\n', mfilename);
  9 Comments

Sign in to comment.

More Answers (1)

Hannah Paul
Hannah Paul on 26 Feb 2021
I tried with segmentation but not able to overcome with the problem. Seems like its not possible.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!