Trying to automatically crop an image into several smaller images based on segmentation

Hi all,
Apologies in advance, I'm not well versed in Matlab.
I'm trying to crop a segmented image into several smaller pieces that only contain the a segmented piece of the original image. For example, if an image has 3 apples and I'm interested in the apples, I am aiming to cut out each apple into a seperate image. The result I'm hoping for is 3 seperate images of apples, with anything else in the crop blackened (or whitened) out.
Why I'm trying to do this is so I can pass each resultant image into a CNN for analysis, as each segmented piece/cropped image is a type of object that needs to be classified.
Below is an image of mine that I'm trying to process. Any ideas would be useful! Thanks in advance.

8 Commenti

That was my original plan, but I'm a bit stumped on how to automatically generate the coordinates for the polygon
What is format of the data? Please attach it
The image format I've used is JPEG, but I can convert it to anything else. I've attached the image (with the mesh from Otsu's method) in the main body of my question, but I can attach the original image if that's what you want to see?
What about dilation?
  • imdilate() image to close contours
  • bwlabel() image to select separate regions
that seems to work pretty well, thank you! I get the following image after dilating the mesh and using bwlabel(), but I'm uncertain on how to proceed from here to crop the labelled regions. Apologies if this is an obvious question...but is there a way to automate this?
Can you plot/draw green pixels (contours/boundaries) on clear image?

Accedi per commentare.

 Risposta accettata

Something like this
clc,clear
I = imread('image.jpeg');
I1 = im2bw(I);
I2 = imdilate(I1,ones(5)); % imdilate to close contours
I3 = imerode(I2,ones(4)); % restore thickness
[L,n] = bwlabel(I3); % label regions/areas
subplot(121)
imshow(I2)
title('imdilate')
subplot(122)
imshow(I3)
title('restore thickness of contours')
figure
for i = 2:n
I4 = L==i; % select region
I5 = imfill(I4,'holes'); % fill contour/region
imshow(cat(3,I5*255,I3*255,I3*0)) % display
pause(1)
end

3 Commenti

I think this is pretty close to what I need to do, so thank you!
Just a couple of questions: instead of filling I5, is there a way to layer it over the original image and then crop it out?
thanks for all your help thus far by the way!
Sure. Try this:
I4 = L==i; % select region
I5 = imfill(I4,'holes'); % fill contour/region
I6 = I.*repmat(uint8(I4),[1 1 3]);
imshow(I6) % display cropped region

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by