clc;
close all;
imtool close all;
clear;
workspace;
format long g;
format compact;
fontSize = 16;
folder = pwd;
baseFileName = 'map.jpg';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorBands] = size(rgbImage);
if numberOfColorBands > 1
grayImage = rgbImage(:, :, 1);
else
grayImage = rgbImage;
end
subplot(2, 2, 1);
imshow(rgbImage);
axis on;
impixelinfo;
caption = sprintf('Original Image : %s', baseFileName);
title(caption, 'FontSize', fontSize);
impixelinfo;
g = gcf;
g.WindowState = 'maximized';
g.NumberTitle = 'off';
g.Name = 'Demo by Image Analyst'
drawnow;
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Cropped Image Histogram', 'FontSize', fontSize);
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
threshold = 240;
xline(threshold, 'Color', 'r', 'LineWidth', 2);
mask = grayImage > threshold;
subplot(2, 2, 3);
imshow(mask);
title('Initial Mask', 'FontSize', fontSize);
drawnow;
mask = imfill(mask, 'holes');
mask = bwareafilt(mask, 1);
subplot(2, 2, 3);
imshow(mask);
title('Final Mask', 'FontSize', fontSize);
drawnow;
boundaries = bwboundaries(mask);
boundaries = boundaries{1};
xb = boundaries(:, 2);
yb = boundaries(:, 1);
numPoints = nnz(grayImage);
numPointsNeeded = 300;
xp = columns * rand(1, numPoints);
yp = rows * rand(1, numPoints);
subplot(2, 2, 4);
imshow(rgbImage);
drawnow;
numPointsInside = 0;
insideBoundary = false(numel(xp), 1);
for k = 1 : numel(xp)
insideBoundary(k) = inpolygon(xp(k), yp(k), xb, yb);
if insideBoundary(k)
numPointsInside = numPointsInside + 1;
if numPointsInside >= numPointsNeeded
break;
end
end
end
x = xp(insideBoundary);
y = yp(insideBoundary);
hold on;
plot(x, y, 'r.', 'MarkerSize', 10);
caption = sprintf('Original image with %d locations', numPointsInside);
title(caption, 'FontSize', fontSize);
msgbox('Done! Thank you Image Analyst!');
0 件のコメント
サインインしてコメントする。