Project

General

Profile

how to localize squares in a grid image using stage information?

Added by Quanfu Fan almost 4 years ago

I was trying to figure out how to map a square image back to its parent grid image. I have tried computing the x- and y- offsets by the difference between the stage positions of the square and its corresponding grid image (divided by the pixel size), but it doesn't seem to work well. Does anyone know how this is done in the web tool? What other information would I need in order to localize squares in a grid image? Thank you!


Replies (12)

RE: how to localize squares in a grid image using stage information? - Added by Anchi Cheng almost 4 years ago

If you are doing this manually, you can make your imageviewer display detailed image information (accessed by clicking i on the menu of the image in the viewer)
See Common_Features.

If you do so on a square image that is mapped to a grid image, you should find "delta row" and "delta column" in the Data tree under "target". These are position
of the center of this square (target position) on its parent image (grid image in this case). The unit is pixel and 0,0 would correspond to the center of the image.

That is all you need.

Image viewer follows the same data tree with mysql query to position the square box on the grid image.

If you want to do this in a python program, the easiest is to use sinedon that is part of leginon package. For example, If I know my square image_filename (without the extension part) and session_name
and have leginon library in your environment setup. In python command line

from leginon import leginondata
# get session object first.  results=1 because we know there should only be one unique session.
session_obj =  leginondata.SessionData(name=session_name).query(results=1)[0]
# limiting the image query by session so it is more efficient
image_obj = leginondata.AcquisitionImageData(session=session_obj, filename=image_filename).query(results=1)[0]
# with the image_obj, you can access any object referenced through it.
print(image_obj['target']['delta column'])
print(image_obj['target']['delta row'])
# you can get the parent grid image object, too
print(image_obj['target']['image']

RE: how to localize squares in a grid image using stage information? - Added by Quanfu Fan almost 4 years ago

Hi, Anchi,
Thank you so much for the help! It works perfectly.

Quanfu

RE: how to localize squares in a grid image using stage information? - Added by Quanfu Fan almost 4 years ago

Hi Anchi,
Could you please help me with another question? Is there any way to get the size of a square in its corresponding square image (i.e. the yellow bounding box indicating the position of the square in the web tool)? I noticed that this size could be different from image to image. Thank you!

RE: how to localize squares in a grid image using stage information? - Added by Anchi Cheng almost 4 years ago

This is more complex. The pixel size of the image may be binned from camera sensor perspective. Low magnification pixel size calibration is usually not great and we don't really spend much time trying to get that right. Using python and sinedon, I recommend using functions and classes in calibrationclient.py

Step 1: create an instance of an object that has logger attribute. You will see that calibrationclient classes output error to self.logger some times.

standard python logger or your own that will print info, debug, error.

You can get image pixel size through PixelSizeCalibrationClient

from leginon import calibrationclient
pcal = calibrationclient.PixelSizeCalibrationClient(your_app_with_logger)
# let's say you want to know the pixel size of the most recent image_obj
image_obj = leginondata.AcquisitionImageData().query(results=1)[0]
# scope reference from image_obj is where microscope related metadata are recorded
# camera reference from image_obj is where camera related metadata are recorded
scope_meta = image_obj['scope']
camera_meta = image_obj['camera']
# This pixel size is unbinned and assume a square pixel.  The unit is meter
campixelsize = pcal.getPixelSize(scope_meta['magnification'],scope_meta['tem'],camera_meta['ccdcamera'])
# Find binning, a dictionary of x and y
binning = camera_meta['binning']
# We've never has a binning x different from binning y, but for completeness sake
pixelsize = {'x':campixelsize*binning['x'],'y':campixelsize*binning['y']}

Vector mapping

If you want to know how to map a pixel position on square image on grid image, you can use pixelToPixel in StageCalibrationClient to
get a pixel vector transformation through the transformation matrix. You can read calibrationclient.py code for how that is done.
Very similar. We use the term tem to mean the microscope instrument object, ccdcamera to mean the digital camera instrument object.

RE: how to localize squares in a grid image using stage information? - Added by Quanfu Fan almost 4 years ago

Hi Anchi,
Thank you so much for the quick response! If I understand correctly, the square size is not stored in the data tree and needs to be computed using the camera pixel size and binning information. what are the corresponding names of camera pixel size and binning in the data tree? What I need is just a bounding box for each square in a grid image, as shown below. Thanks again for the help.

RE: how to localize squares in a grid image using stage information? - Added by Anchi Cheng almost 4 years ago

Data tree represents all the data objects you can access through image_obj. So you get dimension and binning there. The problem is pixel size calibration. It is not referenced in there, so you need to query for it in the database.

For making bounding box, the vector mapping is what you need. Image report also shows the calibration matrix for that imaging condition. The stage matrix transforms vector on unbinned camera sensor to physical coordinate in meters.

You can use the math described in pixelToPixel function to do your transformation. It is just matrix multiplication and dot product.

Essentially, you want to get four vectors on gr image to represent the four corners of the bounding box with their origin at the center of the sq image.

RE: how to localize squares in a grid image using stage information? - Added by Anchi Cheng almost 4 years ago

For example, my input vector on sq image for the left bottom corner (with image origin on top-left) would be

(row, col) = (image_obj['camera']['dimension']['y']*image_obj['camera']['binning']['y']//2, image_obj['camera']['dimension']['x']*image_obj['camera']['binning']['x']//2)

RE: how to localize squares in a grid image using stage information? - Added by Quanfu Fan almost 4 years ago

Many thanks for the help, Anchi! Really appreciate it.

RE: how to localize squares in a grid image using stage information? - Added by Quanfu Fan over 3 years ago

Hi Anchi,
Can I ask one more related question? How would you map a hole image back to its corresponding square image? delta_row and delta_col only provide the location of the hole image , but not its size on the square image. Also, the orientation of a hole image doesn't seem to be always aligned with that of its parent square. Thank you!

RE: how to localize squares in a grid image using stage information? - Added by Anchi Cheng over 3 years ago

The orientation comes naturally in the same way you use pixelToPixel to map vector of your hl image corner. The transform at one mag and itransform in the other use calibration matrices that should allow you to do a affine transformation of the whole hl image if that is what you are looking to do.

RE: how to localize squares in a grid image using stage information? - Added by Quanfu Fan over 3 years ago

Thank you so much for the quick response, Anchi! I am pretty new to the web tool and hasn't programmed anything with it. Is this information stored in the data tree?

RE: how to localize squares in a grid image using stage information? - Added by Anchi Cheng over 3 years ago

Not the tree itself, it is in the same page, though. Under the title of "Calibrations". You want the ones for stage position. It is a transformation between meters and unbinned pixel, at the imaging mag on camera.

    (1-12/12)