MATLAB Answers

distance measurement in image based on intensity difference

10 views (last 30 days)
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


Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 21 Aug 2020
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;
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);
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.
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
% Display histogram
subplot(2, 2, 2);
grid on;
title('Histogram of original gray image', 'FontSize', fontSize);
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');
% 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');
% 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');


Turbulence Analysis
Turbulence Analysis on 21 Sep 2020
Hi, Here it is ....
Please let me know if you face any issues while reading .im7 files.
Turbulence Analysis
Turbulence Analysis on 27 Nov 2020
This in continuation with our previous discussions on having the two color bars during the image superimposition.
Now, I able to display the two images with different colormap by converting the image file read from .im7 to rgb. However, I am not succesful in showing two colorbars.
For instance, in the attached file, one image is diplayed with 'hot' map and other with 'jet'. But, at the end, both the colorbar switches to 'hot' pattern. Could you please hlep me with this ??
%%% First image
h = (I.Frames{1}.Components{1}.Planes {1,1})';
h1 =fix(h/10);
h1(h1==0) = 1 ;
rgb1 = ind2rgb(h1,jet(256));
G = imagesc(flipud(rgb1));
c = colorbar(ax1,'Location', 'east','FontSize',20,'TickLabelInterpreter', 'latex');
c.Title.Interpreter = 'latex';
c.Title.FontSize = 16;
G.AlphaData = 0.5;
set(gca, 'YDir','normal')
axis equal
hold on;
%%%% second image
h2 = (I1.Frames{2,1}.Components{1,1}.Planes {1,1})';
h3 =fix(h2/10);
h3(h3==0) = 1 ;
rgb2 = ind2rgb(h3,hot(256));
GH = imagesc(flipud(rgb2));
c1 = colorbar(ax2,'Location', 'west','FontSize',20,'TickLabelInterpreter', 'latex');
GH.AlphaData = 0.5;
set(gca, 'YDir','normal')
axis equal

Sign in to comment.

More Answers (1)

jonas on 19 Aug 2020
Edited: jonas on 22 Aug 2020
I would first binarize the image and then go columnwise to find the last white pixel.
RGB = imread('B00001.jpg');
GRAY = rgb2gray(RGB);
BW = imbinarize(GRAY);
imshow(BW);hold on
y = nan(size(BW,2),1);
for i = 1:size(BW,2)
lastcol = find(BW(:,i) == true,1,'last');
if ~isempty(lastcol)
y(i) = lastcol;
x = 1:size(BW,2);


Turbulence Analysis
Turbulence Analysis on 28 Nov 2020
Okay, thanks. Atleast, with RGB I can able to display images with two different color patterns..
Furthermore, I intend to read images placed inside folder and subfolder within a for loop.. Is there a way to achieve this within a single for loop..
For e.g, the first part of code needs to read image from folder named 'A' and second part needs to read from subfolder 'A1'
%%%%%%% Needs to be read from folder A
for f = 1:1:1 if (f>=1) && (f<=9) fname_strt = 'B0000' ; elseif (f>=10) && (f<=99) fname_strt='B000'; elseif (f>=100) && (f<=999) fname_strt='B00'; else fname_strt='B0'; end fname_end = num2str(f); fname = strcat(fname_strt,fname_end,'.jpg'); I=imread(fname); G = imagesc(x,y,flipud(I)); G.AlphaData = 0.5; hold on;
%%%%%%% Needs to be read from subfolder A1 which is inside A
if (f>=1) && (f<=9)
fname_strt = 'B0000' ;
elseif (f>=10) && (f<=99)
elseif (f>=100) && (f<=999)
fname_end = num2str(f);
fname = strcat(fname_strt,fname_end,'.jpg');
H1 = imagesc(flipud(h));
H1.AlphaData = 0.5;
axis equal
hold on;

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!