clc;
workspace
fontSize = 20;
folder = 'C:\Users\shravnathi\Documents\Temporary';
baseFileName = 'origpalm.png';
fullFileName = fullfile(folder, baseFileName);
currentimg=imread(fullFileName);
subplot(2,2,1);
imshow(currentimg);
title('Original Image', 'FontSize', fontSize);
VidImage = currentimg;
height = size(VidImage,1);
width = size(VidImage,2);
out = VidImage;
bin = zeros(height,width);
img_ycbcr = rgb2ycbcr(VidImage);
Cb = img_ycbcr(:,:,2);
Cr = img_ycbcr(:,:,3);
[r,c,v] = find(Cb>=77 & Cb<=127 & Cr>=133 & Cr<=173);
numind = size(r,1);
for i=1:numind
out(r(i),c(i),:) = [0 0 255];
bin(r(i),c(i)) = 1;
end
binaryImage=im2bw(bin,graythresh(bin));
subplot(2,2,2);
imshow(binaryImage);
title('Binary Image', 'FontSize', fontSize);
B = bwboundaries(binaryImage);
binaryImage = imfill(binaryImage, 'holes');
binaryImage = bwareaopen(binaryImage, 5000);
subplot(2,2,3);
imshow(binaryImage);
title('Second Binary Image', 'FontSize', fontSize);
biggestBlob = ExtractNLargestBlobs(binaryImage, 1);
subplot(2,2, 4);
imshow(biggestBlob);
title('Final Image', 'FontSize', fontSize);
[labeledImage, numberOfBlobs] = bwlabel(biggestBlob, 8);
blobMeasurements = regionprops(labeledImage, 'BoundingBox','Area');
allBlobAreas = [blobMeasurements.Area];
subplot(2,2,1);
hold on;
for k = 1 : numberOfBlobs
boundingBox = blobMeasurements(k).BoundingBox;
x1 = boundingBox(1);
y1 = boundingBox(2);
x2 = x1 + boundingBox(3) - 1;
y2 = y1 + boundingBox(4) - 1;
verticesX = [x1 x2 x2 x1 x1];
verticesY = [y1 y1 y2 y2 y1];
plot(verticesX, verticesY, 'r-', 'LineWidth', 2);
end
uiwait(msgbox('Done with demo'));
Best Answer