close all;
clear;
clc;
workspace;
format longg;
format compact;
fontSize = 20;
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
return;
end
end
grayImage = uint8(mat2gray(peaks(400)));
[rows columns numberOfColorBands] = size(grayImage);
subplot(2, 1, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
button = 1;
while button ~= 2
button = menu('Choose an action', 'Find Intersection', 'Exit');
if button == 2
break;
end
subplot(2, 1, 1);
cla reset;
imshow(grayImage, []);
axis on;
title('Left-click first point. Right click last point.', 'FontSize', fontSize);
[x, y, profile] = improfile();
hold on;
plot(x, y, 'r-', 'LineWidth', 2);
title('Binary Image', 'FontSize', fontSize);
distanceInPixels = sqrt((x(1)-x(end))^2 + (y(1)-y(end))^2);
subplot(2,1,2);
plot(profile, 'LineWidth', 2);
grid on;
caption = sprintf('Intensity Profile Along Line\nThe distance = %f pixels', ...
distanceInPixels);
title(caption, 'FontSize', fontSize);
ylabel('Gray Level', 'FontSize', fontSize);
xlabel('Pixels Along Line', 'FontSize', fontSize);
ylim([0 1.5]);
dif = diff(profile);
nonZeroElements = find(dif > 0);
subplot(2, 1, 1);
hold on;
plot(x(nonZeroElements), y(nonZeroElements),...
'ro', 'MarkerSize', 10);
nonZeroElements2 = find(dif < 0);
subplot(2, 1, 1);
hold on;
plot(x(nonZeroElements2), y(nonZeroElements2),...
'ro', 'MarkerSize', 10);
end
Best Answer