How can I automatically crop an image in an elliptical shape?

3 views (last 30 days)
I have taken photographs of Petri dishes. The photographs are rectangular in shape, but I need to crop them such that the image is entirely of the Petri dish and therefore roughly circular in shape.
I see that imcrop can be used to rectilinearly crop images, but is there an elliptical analog to this?
Just for some background, I am working on a Matlab code that will automatically count the number of bacterial colonies on a Petri dish by inputting an image of the dish into the imfindcircles function.
Alternatively, is there a simple-ish way of having Matlab automatically detect the boundaries of the Petri dish, draw an outline around it, and then omit all parts of the image that are not inside of the drawn shape? My function depends on cropping the image precisely around the Petri dish boundaries.
Attached are a few examples of the images I am working with.
Thank you very much in advance for your help!

Accepted Answer

KSSV
KSSV on 13 Apr 2017
I=imread('dish.jpg');
h = imshow(I) ;
%%call imellipse
% set some positon for ellipse
x = 10;
y = 10;
d1 = 100;
d2 = 100;
e = imellipse(gca, [x y d1 d2]);
% wait to change the positon
position = wait(e);
% ipdate the positon
pos = getPosition(e);
x = pos(1);
y = pos(2);
d1 = pos(3);
d2 = pos(4);
%
BW = createMask(e,h);
% here assuming your image is uint8
BW = uint8(BW);
I2 = I.*BW; % everything outside circle to black
imwrite(I2,'out.png','Transparency',0);
there would be further elegant solution. Especially Image Analyst will answer.
  2 Comments
jstarr
jstarr on 13 Apr 2017
Edited: jstarr on 13 Apr 2017
Hi KSSV,
Thanks for the quick response, but I am looking for something a little bit more automatic for my implementation.
Essentially, I am trying to develop a function that reads in a jpeg image of a Petri dish and then automatically detects the boundaries of the dish and crops out the rest of the image accordingly, without requiring any user input. I have hundreds of images to run through at once, which is why the automation feature is so important (it would take forever otherwise).
I didn't know about the imellipse function, so thanks for bringing that to my attention! I'll definitely try experimenting with that.
jstarr
jstarr on 14 Apr 2017
Given that all of my pictures were taken from the same height and the dishes appear roughly circular in my images, I was able to use the code you provided above in conjunction with the imfindcircles function to get something that is automatic enough for my purposes. I'll paste my code down below in case you're curious. Thanks again!
I = im2bw(imread('16320641-2017-04-11-193956.jpg'));
h = imshow(I);
[centers, radii] = imfindcircles(I, [850/2 900/2], 'Sensitivity', 0.98); %A radius range that will work for all my images
e = imellipse(gca,[centers(1)-radii(1), centers(2)-radii(1), 2*radii(1), 2*radii(1)]);
BW = createMask(e,h);
% here assuming your image is uint8
BW = uint8(BW);
I2 = I.*BW; % everything outside circle to black
imwrite(I2,'out.png','Transparency',0);
imshow(I2)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!