format long g;
format compact; captionFontSize = 14;
FileName = 'dirty_water.jpg'; originalImage = imread(FileName);
[rows, columns, numberOfColorChannels] = size(originalImage);
if numberOfColorChannels > 1 promptMessage = sprintf('Your image file has %d color channels.\nThis demo was designed for grayscale images.\nDo you want me to convert it to grayscale for you so you can continue?', numberOfColorChannels); button = questdlg(promptMessage, 'Continue', 'Convert and Continue', 'Cancel', 'Convert and Continue');
if strcmp(button, 'Cancel') fprintf(1, 'Finished running micellesDemo.m.\n'); return;
end
originalImage = rgb2gray(originalImage);
end
h1=figure(1);
subplot(2, 2, 1);
imshow(originalImage); set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
[pixelCount, grayLevels] = imhist(originalImage);
subplot(2, 2, 2); bar(pixelCount);
title('Histogram of original image', 'FontSize', captionFontSize); xlim([0 grayLevels(end)]);
grid on;
thresholdValue = 100; binaryImage = originalImage > thresholdValue;
binaryImage = imfill(binaryImage, 'holes');
maxYValue = ylim; line([thresholdValue, thresholdValue], maxYValue, 'Color', 'r');
annotationText = sprintf('Thresholded at %d gray levels', thresholdValue);
text(double(thresholdValue + 5), double(0.5 * maxYValue(2)), annotationText, 'FontSize', 10, 'Color', [0 .5 0]); text(double(thresholdValue - 70), double(0.94 * maxYValue(2)), 'Background', 'FontSize', 10, 'Color', [0 0 .5]); text(double(thresholdValue + 50), double(0.94 * maxYValue(2)), 'Foreground', 'FontSize', 10, 'Color', [0 0 .5]);
imshow(binaryImage); title('Image converted into Binary after thresholding', 'FontSize', captionFontSize);
labeledImage = bwlabel(binaryImage, 8); subplot(2, 2, 3); imshow(labeledImage, []);
title('Micelle groups', 'FontSize', captionFontSize); micellMeasurements = regionprops(labeledImage, originalImage, 'all'); numberOfmicell = size(micellMeasurements, 1);
subplot(2, 2, 4); imshow(originalImage);
title('Outlines, from bwboundaries()', 'FontSize', captionFontSize); axis image;
hold on; boundaries = bwboundaries(binaryImage); numberOfBoundaries = size(boundaries, 1); for k = 1 : numberOfBoundaries thisBoundary = boundaries{k}; plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2); end; hold off;
textFontSize = 14;
labelShiftX = -7;
micelleECD = zeros(1, numberOfmicell);
fprintf(1,'micelle # Mean Intensity Area Perimeter Centroid Diameter\n');
for k = 1 : numberOfmicell
thismicellesPixels = micellMeasurements(k).PixelIdxList;
meanGL = mean(originalImage(thismicellesPixels));
meanGL2008a = micellMeasurements(k).MeanIntensity;
micelleArea = micellMeasurements(k).Area;
micellePerimeter = micellMeasurements(k).Perimeter;
micelleCentroid = micellMeasurements(k).Centroid;
micelleECD(k) = sqrt(4 * micelleArea / pi);
fprintf(1,'#%2d %17.1f %11.1f %8.1f %8.1f %8.1f % 8.1f\n', k, meanGL, micelleArea, micellePerimeter, micelleCentroid, micelleECD(k));
text(micelleCentroid(1) + labelShiftX, micelleCentroid(2), num2str(k), 'FontSize', textFontSize, 'FontWeight', 'Bold');
end
Best Answer