clc;
close all;
imtool close all;
clear;
workspace;
format longg;
format compact;
fontSize = 20;
folder = 'C:\Users\Srikanta\Documents';
baseFileName = 'tissue.png';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
grayImage = rgb2gray(imcrop(grayImage, [90 140 400 110]));
[rows columns numberOfColorBands] = size(grayImage);
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
[pixelCount grayLevels] = imhist(grayImage);
pixelCount(1) = 0;
subplot(2, 2, 2);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
binaryImage = grayImage > 10;
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
binaryImage = imclose(binaryImage, true(5));
binaryImage = imfill(binaryImage, 'holes')
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Cleaned Binary Image', 'FontSize', fontSize);
topRow = zeros(columns, 1);
bottomRow = zeros(columns, 1);
midpoints = zeros(columns, 1);
for column = 1 : columns
oneColumn = binaryImage(:, column);
if sum(oneColumn) > 0
topRow(column) = find(binaryImage(:, column), 1, 'first');
bottomRow(column) = find(binaryImage(:, column), 1, 'last');
midpoints(column) = (topRow(column) + bottomRow(column)) / 2;
end
end
hold on;
plot(1:columns, midpoints, 'r-', 'LineWidth', 2);
Best Answer