clc;
close all;
clearvars;
workspace;
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
a1 = [134, 51, 143]
a2 = [131, 132, 4]
R0 = 30
folder = pwd;
baseFileName = 'image.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, 2, 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';
[rows, columns, numberOfColorChannels] = size(rgbImage);
outputImage = zeros(rows, columns, numberOfColorChannels, class(rgbImage));
distanceImage = zeros(rows, columns);
for col = 1 : columns
for row = 1 : rows
thisR = double(rgbImage(row, col, 1));
thisG = double(rgbImage(row, col, 2));
thisB = double(rgbImage(row, col, 3));
distanceImage(row, col) = sqrt((thisR - a1(1))^2 + (thisG - a1(2))^2 + (thisB - a1(3))^2);
if distanceImage(row, col) < R0
end
end
end
maxDistance = max(distanceImage(:))
subplot(2, 2, 2);
imshow(distanceImage, []);
axis('on', 'image');
caption = sprintf('Distance Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
colorbar;
drawnow;
hp = impixelinfo();
subplot(2, 2, 3);
histogram(distanceImage, 256);
grid on;
title('Histogram of Color Distances', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize, 'Interpreter', 'None');
ylabel('Count', 'FontSize', fontSize, 'Interpreter', 'None');
fprintf('Done running %s.m ...\n', mfilename);
Best Answer