MATLAB: Auto cropping of pollen

auto croppingImage Processing Toolboximage segmentation

Ι want ot do the following:
I have this image and i want to create ROI of a pollen automatically.If i imcrop for example a small region with ,let say 5 pollens, i want to take 5 cropped images
of the pollens ,with the pollens centered and specific dimensions of the ROI.For example this image
Summarizing :
i imcrop and i get as many cropped images as the pollens are included inside the imcropped
image.How can i do it???

Best Answer

First of all, take a black shot with no pollen in there, and divide your image by that blank shot to do background correction. I'm attaching a demo.
After that, you can do thresholding and cropping like I do in my Image Processing Tutorial: My File Exchange
You might want to convert your image to a binary image using the Color Thresholder on the Apps tab of the tool ribbon. Use the Export button to export the function to do the binarization. Basically to crop each, you'd do
props = regionprops(binaryImage, 'BoundingBox');
figure % Bring up new figure.
spRows = ceil(sqrt(length(props)));
for k = 1 : length(props)
thisBB = props(k).BoundingBox;
subImage = imcrop(originalImage, thisBB);
subplot(spRows, spRows, k);
imshow(subImage);
end
If you want a specific width and margin to your bounding boxes, then take thisBB and modify it. Like, to make it 50 pixels wider and 75 pixels taller:
thisBB(1) = thisBB(1) - 50;
thisBB(3) = thisBB(3) + 100;
thisBB(2) = thisBB(2) - 75;
thisBB(4) = thisBB(4) + 150;
Or, to make it always 200 pixels wide and tall, do this:
midx = thisBB(1) + thisBB(3)/2; % Right side plus half the width.
thisBB(1) = midx - 100;
thisBB(3) = thisBB(1) + 199;
midy = thisBB(2) + thisBB(4)/2;
thisBB(2) = midy - 100;
thisBB(4) = thisBB(2) + 199;