clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
folder = pwd;
baseFileName = 'microgripper_grayscale.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
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = grayImage(:, :, 2);
end
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
grayImage = grayImage(1:160,50:250);
subplot(2, 2, 2);
imshow(grayImage, []);
axis on;
title('Cropped Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 2, 3);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
horizontalProfile = mean(grayImage, 1);
subplot(2, 2, 4);
plot(horizontalProfile, 'LineWidth', 2);
grid on;
title('Average Horizontal Profile', 'FontSize', fontSize, 'Interpreter', 'None');
binaryImage = horizontalProfile > 100;
labeledRegions = bwlabel(binaryImage);
measurements = regionprops(labeledRegions, 'Area');
gripperGap = measurements(2).Area;
message = sprintf('The average gap between the gripper fingers is %.3f pixels', gripperGap);
uiwait(helpdlg(message));
Best Answer