Get Z co-ordinate from tiled image

Discuss questions and projects related to processing of imaging data here
Post Reply
Timothy Band
Posts: 3
Joined: Tue Oct 04, 2022 1:53 pm

Get Z co-ordinate from tiled image

Post by Timothy Band »

My macro calculates a point on the slide and moves the stage to this point, I would now like to bring the image into focus. Sometimes autofocus is not sufficient for this because the slide does not have a lot of contrast. However, if I can focus approximately then autofocus will finish the job fine, so I need an approximate z value for this position on the slide.

Happily I already have a tiled image for this portion of the slide, so surely I can extract the nearest Z-value obtained during the capture of this tiled image?

Is there a nice way to do this within a macro?

Thanks for any help!
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: Get Z co-ordinate from tiled image

Post by CarlZeissMicroscopy3 »

Hello Timothy Band,

as far as I understood your case:

Code: Select all

imageWithZ = Zen.Application.ActiveDocument

imageTile = imageWithZ.CreateSubImage('M(1)|Z(1)|C(1)|S(1)')

print imageTile.Metadata.FocusPositionMicron
CreateSubImage('M(1)|Z(1)|C(1)|S(1)') takes
M(1): the first tile image (most probably a loop using imageWithZ.Metadata.TilesCount is needed)
Z(1): of the first plane of a z-stack
C(1): using the first channel
S(1): of the first Scene

Parameters can be reduced when complexity is lower or added when higher.

Hope this helps ...
Timothy Band
Posts: 3
Joined: Tue Oct 04, 2022 1:53 pm

Re: Get Z co-ordinate from tiled image

Post by Timothy Band »

Thanks, that's really interesting! I suppose I can also work out the X/Y co-ordinates in pixels and do something like:

Code: Select all

image.CreateSubImage("X({0})|Y({1})|C(1)|S(1)".format(pixelX, pixelY)).Metadata.FocusPositionMicron
will that give me the focus of the tile at that position? Or will it still be the "overall" focus position?
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: Get Z co-ordinate from tiled image

Post by CarlZeissMicroscopy3 »

Hello Timothy Band,

to clarify the situation a bit let’s consider the acquisition process itself:
The microscope moves to a certain position in x,y and z to take the image. As the camera takes the image as a whole the z coordinate is valid for the whole plane of the image taken.

This is the reason why the M-Index is taken like
imageTile = imageWithZ.CreateSubImage('M(1)…')
to get the FocusPosition.

The M-Index addresses the images taken in a sequence.

(On the 2D-View you can click on the ‘2 D Display’ tab and then activate ‘Show Tile Boundaries’. Then the tiles are shown in the image. Please be aware that zooming into the image shows the tiles of the image pyramid and not the ‘original’ tiles = camera frames)

As you already ‘suspected’ you will get the "overall" focus position when using
image.CreateSubImage("X({0})|Y({1}…)
because there is no other z information available.

Please take into consideration that using X,Y instead of M can lead to ‘unexpected results’ when tiles overlap or if the image has no physical image (gap) at the area investigated with X,Y.
Timothy Band
Posts: 3
Joined: Tue Oct 04, 2022 1:53 pm

Re: Get Z co-ordinate from tiled image

Post by Timothy Band »

Great to know, thanks!
Jeffree Star
Posts: 3
Joined: Fri Jul 28, 2023 10:41 am

Re: Get Z co-ordinate from tiled image

Post by Jeffree Star »

Yes, you can use the tiled immaculate grid image you captured to extract the approximate Z-value for the position on the slide. This process is known as "image-based autofocus."
Jase Ronald
Posts: 1
Joined: Fri Aug 04, 2023 5:36 pm

Re: Get Z co-ordinate from tiled image

Post by Jase Ronald »

To achieve accurate focus on the slide image in situations where autofocus alone might not be sufficient due to low contrast, you can implement focus bracketing in your macro. This involves capturing a series of images with incremental changes in the z-value while keeping the XY position constant. Analyze the sharpness of each image of rainguardroofs using an appropriate image analysis algorithm or tool to determine the approximate z-value where the image appears sharpest.
Bekean Loinse
Posts: 2
Joined: Mon Apr 03, 2023 8:57 am

Re: Get Z co-ordinate from tiled image

Post by Bekean Loinse »

This involves extracting Z-values from a tiled image captured at that poeltl unlimited position. This can be done using image processing techniques, although it might require some custom scripting or programming depending on the tools you're using.
Asif Naveed
Posts: 6
Joined: Thu Aug 03, 2023 10:29 pm

Re: Get Z co-ordinate from tiled image

Post by Asif Naveed »

That's a clever workaround you're thinking of! To my knowledge, if the software you're using for slide imaging stores the Z-values in the metadata of each tile, you should be able to extract this data within a macro. What you could do is create a function that reads the metadata and identifies the nearest X,Y point to your calculated point, then grabs the corresponding Z-value from that tile.

Autofocus can be tricky with low contrast slides, so leveraging your existing tiled image for a "best guess" Z-value seems like a solid plan. If you've already got the tiled image, you're halfway there.

I'm not sure which language or software you're using, but if it were something like Python, you could likely use a library like PyTiff to pull out this information from the metadata. From there, you could use that Z-value as your starting point for autofocus.

Hope yacine apk this helps, and let us know how it goes!
Post Reply