clc;
close all;
clearvars;
workspace;
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
folder = pwd;
baseFileName = 'Parking Lot without Cars.jpg';
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
rgbEmptyImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbEmptyImage);
subplot(2, 3, 2);
imshow(rgbEmptyImage, []);
axis('on', 'image');
caption = sprintf('Reference Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
folder = pwd;
baseFileName = 'Parking Lot with Cars.jpg';
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
rgbTestImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbTestImage);
subplot(2, 3, 1);
imshow(rgbTestImage, []);
axis('on', 'image');
caption = sprintf('Test 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';
folder = pwd;
baseFileName = 'Parking Lot Mask.png';
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
maskImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(maskImage);
mask = min(maskImage, [], 3) == 255;
subplot(2, 3, 3);
imshow(mask, []);
axis('on', 'image');
caption = sprintf('Mask Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
diffImage = imabsdiff(rgbEmptyImage, rgbTestImage);
subplot(2, 3, 4);
imshow(diffImage, []);
axis('on', 'image');
caption = sprintf('Difference Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
diffImage = rgb2gray(diffImage);
diffImage(~mask) = 0;
subplot(2, 3, 5);
histogram(diffImage(diffImage>0));
imshow(diffImage, []);
axis('on', 'image');
caption = sprintf('Gray Scale Difference Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
kThreshold = 40;
parkedCars = diffImage > kThreshold;
parkedCars = imfill(parkedCars, 'holes');
parkedCars = bwconvhull(parkedCars, 'objects');
subplot(2, 3, 6);
imshow(parkedCars, []);
impixelinfo;
axis('on', 'image');
caption = sprintf('Parked Cars Binary Image with Threshold = %.1f', kThreshold);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
props = regionprops(mask, parkedCars, 'MeanIntensity', 'Centroid', 'BoundingBox');
centroids = vertcat(props.Centroid);
numRows = 4;
[indexesY, clusterCenterY] = kmeans(centroids(:, 2), numRows);
for k = 1 : numRows
yline(clusterCenterY(k), 'Color', 'g', 'LineWidth', 2);
end
for k = 1 : length(props)
rectangle('Position', props(k).BoundingBox, 'EdgeColor', 'y');
end
[~, sortOrder] = sort(clusterCenterY, 'ascend');
originalIndexes = indexesY;
fprintf('\nOriginal Class Number New Class #\n');
for k = 1 : length(indexesY)
currentClass = indexesY(k);
newClass = find(sortOrder == currentClass);
fprintf(' %d %d\n', newClass, currentClass);
indexesY(k) = newClass;
end
newLabels = 1 : length(props);
pointer = 0;
for k = 1 : numRows
thisClass = find(indexesY == k);
newLabels((pointer + 1) : (pointer + length(thisClass))) = thisClass;
pointer = pointer + length(thisClass);
end
props = props(newLabels);
percentageFilled = [props.MeanIntensity]
centroids = vertcat(props.Centroid);
hFig2 = figure;
imshow(rgbTestImage);
hFig2.WindowState = 'maximized';
hFig2.Name = 'Demo by ImageAnalyst';
hold on;
for k = 1 : length(props)
x = centroids(k, 1);
y = centroids(k, 2);
blobLabel = sprintf('%d', k);
if percentageFilled(k) > 0.10
plot(x, y, 'rx', 'MarkerSize', 30, 'LineWidth', 4);
text(x, y+20, blobLabel, 'Color', 'r', 'FontSize', 15, 'FontWeight', 'bold');
else
plot(x, y, 'g.', 'MarkerSize', 40, 'LineWidth', 4);
text(x, y+20, blobLabel, 'Color', 'g', 'FontSize', 15, 'FontWeight', 'bold');
end
end
title('Marked Spaces. Green Spot = Available. Red X = Taken.', 'FontSize', fontSize);
fprintf('Done running %s.m ...\n', mfilename);
Best Answer