How to calculate the automatically some distance in the image based on the colour ?

5 views (last 30 days)
Hello all, I am interested to calculate the automatically some distance in the image based on the colour. Is it possible to calculate the distance ? For example i want to calculate the Ha1 , Ha2 , Ha3 and Ha4 in the sample a , Hb1 , Hb2 , Hb3 and Hb4 in the sample b, Hc1 , Hc2 , Hc3 and Hc4 in the sample c, Hd1 , Hd2 , Hd3 and Hd4 in the sample d, He1 , He2 , He3 and He4 in the sample e
please find the general image and image to calculate the distance. I am not expert at MATLAB, but i am interested to learn this code. I will appreciate if any one tell me an code to perform this operation.
Thanks a ton
  5 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 14 Feb 2021
@Kristen Chappel Sorry, I was unable to response this question in due time. Now, you got a response from one among the best people @Image Analyst in image processing domain.
Kristen Chappel
Kristen Chappel on 14 Feb 2021
@KALYAN ACHARJYA NO PROBLEM , I RECEVIED THE RESPONSE FROM IMAGE ANAYLYST AND HE EXPLAINED VERY WELL , BUT WAITING FOR THE MY LAST QUERY. YOU CAN CHECK MY LAST COMMENT AS WELL, IF INTERESTED TO SOLVE

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 13 Feb 2021
Edited: Image Analyst on 13 Feb 2021
Kristen, try this:
% 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 = 'General sample details 1.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
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image full size.
subplot(2, 2, 1);
imshow(rgbImage, []);
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';
%--------------------------------------------------------------------------------------------------------
% Threshold image.
grayImage = rgb2gray(rgbImage);
mask = grayImage < 100;
% Clean up the mask to get only hte 5 stripes we want.
% Get rid of blue surround
mask = imclearborder(mask);
% Take the 5 largest blobs.
mask = bwareafilt(mask, 5);
% Get rid of small tendrils
se = strel('disk', 2, 0);
mask = imopen(mask, se);
% Display mask image.
subplot(2, 2, 2);
imshow(mask, []);
axis('on', 'image');
title('Initial 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.
[labeledImage, numBlobs] = bwlabel(mask);
% For each blob, for each line, get the left edge and right edge then widths.
leftEdges = zeros(rows, 1);
rightEdges = zeros(rows, 1);
widths = zeros(rows, 5);
subplot(2, 2, 3:4);
hold on;
for blob = 1 : numBlobs
thisBlob = ismember(labeledImage, blob);
% Display mask image.
subplot(2, 2, 2);
imshow(thisBlob, []);
axis('on', 'image');
caption = sprintf('Mask of Blob #%d', blob);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hold on;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
for row = 1 : rows
thisRow = thisBlob(row, :);
t = find(thisRow, 1, 'first');
if ~isempty(t)
leftEdges(row) = t;
rightEdges(row) = find(thisRow, 1, 'last');
end
end
widths(:, blob) = rightEdges - leftEdges;
subplot(2, 2, 3:4);
plot(widths, '-', 'LineWidth', 2);
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, 2, 2);
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);
By the way, don't use JPG format for image analysis if you can at all avoid it. Use PNG. JPG introduces artifacts that reduce your precision.
  26 Comments

Sign in to comment.


Jenny Lee
Jenny Lee on 26 Feb 2021
I am using more or less similar image like uploaded by anish khan, but this code not worked for me. @Image Analyst Could you please change your code to according to the Anish Khan image?

Community Treasure Hunt

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

Start Hunting!