MATLAB: Distance measurement in image based on intensity difference

@image analyst#measurement in imagesImage Processing Toolboximage segmentation

Hi,
I have a serious of images similar to the one attached here. I intend to measure the distance of particular portion of the image from the bottom as shown as yellow arrow lines in the attached figure.. If somebody came across this kind of problem, please shed some light on this

Best Answer

Try this. If it works you can "Accept this answer" and you might want to consider going over your prior posts and "Accepting" some of them to let people know they're solved and to give the people who helped you "reputation points.":
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
%--------------------------------------------------------------------------------------------------------

% READ IN IMAGE
folder = pwd;
baseFileName = 'B00015.jpg';
% Get the full filename, with path prepended.
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 the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.

subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
% Display histogram
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Histogram of original gray image', 'FontSize', fontSize);
%--------------------------------------------------------------------------------------------------------
% SEGMENTATION OF IMAGE
threshold = 60;
mask = grayImage > threshold;
mask = imfill(mask, 'holes');
mask = bwareafilt(mask, 1);
% Display the image.
subplot(2, 2, 3);
imshow(mask, []);
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
% Measure Bounding Box.
props = regionprops(mask, 'BoundingBox');
% Display the original image again so we can overlay graphics on it.
subplot(2, 2, 4);
imshow(grayImage, []);
title('Original Image with Overlays', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
% Now show the bounding box
hold on;
rectangle('Position', props.BoundingBox, 'LineWidth', 2, 'EdgeColor', 'r');
% Find the last row
lastRow = floor(props.BoundingBox(2) + props.BoundingBox(4))
% Compute the distance between the last row and the bottom of the image.
distance = rows - lastRow % Add 1 if you want.
% Find out all the columns that are in the blob.
[blobRows, blobColumns] = find(mask);
lastRow = max(blobRows) % Should be the same as above.
% Find out columns where the blob is in the last row
lastColumns = find(mask(lastRow, :))
% Get the mean of these to get the mean x (column) value.
meanX = mean(lastColumns);
% Draw a yellow line up to there.
line([meanX, meanX], [columns, lastRow], 'Color', 'y', 'LineWidth', 2);
% Put up text next to the line half way up.
textLabel = sprintf(' Last Row at %d.\n Distance = %d', lastRow, distance);
yt = (rows + lastRow)/2
text(meanX, yt, textLabel, 'FontSize', 13, 'FontWeight', 'bold', ...
'Color', 'y', 'VerticalAlignment', 'middle');