gca is a function which returns the handle to the current axes. Similarly, h_im is a handle to the image object created by imshow. You can read more about working with handles from Mathworks here.
The number you see when examining handles is not related to the properties (size, etc) of the image - the value might have to do with type of object, but basically they're identifiers used by Matlab to keep track of open graphics objects. So when you call imellipse you're passing in a handle to the axes where the ellipse will appear, and returning another handle in e
.
e = imellipse(gca,[55 10 120 120]);
Alternatively in place of gca
you could use another handle pointing to a different axes. For example, if you had:
a1 = subplot(1,2,1), h1 = imshow(img1)
a2 = subplot(1,2,2), h2 = imshow(img2)
Then you could use either a1
or a2
in place of gca depending on which image you wanted to create the roi in.
If you close the figure containing the image and your ellipse, those handles will be deleted - which is how you got the error 'Invalid or deleted object'. Once you've used createMask
and returned the mask you want, the figure can be closed.