Get current axes containing image
ax = imgca
ax = imgca(fig)
the current axes that contains an image. The current axes can be in a regular figure
window or in an Image Tool window. Note that the current axes that contains an image
might not be the same as the most recently accessed axes.
ax = imgca
If no figure contains an axes that contains an image, then
imgca creates a new axes.
Read a grayscale image into the workspace.
I = imread('coins.png'); imshow(I)
Convert the image into a binary image.
bw = imbinarize(I); imshow(bw)
Fill holes in the binary objects, then calculate the centroids of the objects.
bw2 = imfill(bw,'holes'); s = regionprops(bw2, 'centroid'); centroids = cat(1,s.Centroid);
Display the original image and a plot of the centroids in the same figure window. Note that the current axes contains the plot of the centroids, not the displayed image.
subplot(1,2,1) imshow(I) subplot(1,2,2) plot(centroids(:,1),centroids(:,2),'*') axis image
The direction of the y-axis is reversed for images. For equivalent comparison of the image and the plot of the centroids, reverse the y-axis direction of the plot. To get the most recent axes, which contains the plot of the centroids, use the
h = gca; h.YDir = 'reverse';
imgca to get the most recent axes containing an image. Note that this axes is not the most recent axes. Overlay the centroids in red asterisks on the image.
hIm = imgca; hold(hIm,'on') plot(hIm,centroids(:,1),centroids(:,2),'r*') hold(hIm,'off')
imgca can be useful in returning the axes object in the
Image Tool. You cannot retrieve this axes using