redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
Then create a binary image:
binaryImage = redChannel == 0 & greenChannel == 0 & blueChannel == 0;
Then label pass it into regionprops
blobMeasurements = regionprops(binaryImage, 'BoundingBox');
Then use a loop that for each region, use imcrop() to extract the bounding box from the original rgb image and use imwrite() to save it to a disk file. Here's the relevant code from my tutorial, which I'm sure a smart guy like you can modify - just take out the stuff about what coin type it is, and add a call to imwrite().
message = sprintf('Would you like to crop out each coin to individual images?');
reply = questdlg(message, 'Extract Individual Images?', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'Yes')
figure;
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
for k = 1 : numberOfBlobs
thisBlobsBoundingBox = blobMeasurements(k).BoundingBox;
subImage = imcrop(originalImage, thisBlobsBoundingBox);
if blobMeasurements(k).Area > 2200
coinType = 'nickel';
else
coinType = 'dime';
end
subplot(3, 4, k);
imshow(subImage);
caption = sprintf('Coin #%d is a %s.\nDiameter = %.1f pixels\nArea = %d pixels', ...
k, coinType, blobECD(k), blobMeasurements(k).Area);
title(caption, 'FontSize', 14);
end
Best Answer