clc;
close all;
clearvars;
workspace;
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
folder = pwd;
baseFileName = 'warped_image2.jpeg';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileNameOnSearchPath = baseFileName;
if ~exist(fullFileNameOnSearchPath, 'file')
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);
subplot(2, 1, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Reference Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
hFig1.Name = 'Demo by Image Analyst';
[mask, maskedRGBImage] = createMask(rgbImage);
mask = imfill(mask, 'holes');
windowSize = 7;
f = ones(windowSize)/windowSize^2;
mask = imfilter(mask, f);
subplot(2, 1, 2);
imshow(mask, []);
axis('on', 'image');
caption = sprintf('Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
colorbar;
drawnow;
hp = impixelinfo();
bottomRows = zeros(1, columns);
for col = 1 : columns
thisCol = mask(:, col);
r = find(thisCol, 1, 'last');
if ~isempty(r)
bottomRows(col) = r;
end
end
lastColumn = find(bottomRows > 0, 1, 'last')
bottomLine = max(bottomRows)
hold on;
yline(bottomLine, 'Color', 'g', 'LineWidth', 2)
plot(lastColumn, bottomRows(lastColumn), 'mo', 'MarkerSize', 15, 'LineWidth', 2)
text(lastColumn, bottomRows(lastColumn), 'Right most point ', 'HorizontalAlignment', 'right', 'Color', 'm', 'FontSize', 15, 'FontWeight', 'bold');
distance = bottomLine - bottomRows(lastColumn)
goodIndexes = bottomRows > 0;
c = 1:columns;
x = c(goodIndexes);
y = bottomRows(goodIndexes);
coefficients = polyfit(x, y, 2);
xFit = c;
yFit = polyval(coefficients, xFit);
plot(xFit, yFit, 'r-', 'LineWidth', 2)
yLast = yFit(lastColumn);
plot(lastColumn, yLast, 'r+', 'MarkerSize', 50, 'LineWidth', 2)
text(lastColumn, yLast, 'Fitted y at Right most point ', 'HorizontalAlignment', 'right', 'Color', 'r', 'FontSize', 15, 'FontWeight', 'bold');
fittedDistance = bottomLine - yLast
caption = sprintf('Magenta-to-green distance = %.1f. Red-to-green distance = %.1f', distance, fittedDistance);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
fprintf('Done running %s.m ...\n', mfilename);
function [BW,maskedRGBImage] = createMask(RGB)
I = rgb2lab(RGB);
channel1Min = 5.530;
channel1Max = 99.753;
channel2Min = -14.832;
channel2Max = 45.687;
channel3Min = -73.763;
channel3Max = -28.389;
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
maskedRGBImage = RGB;
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
Best Answer