petersaj / AP_histology

Histology processing
63 stars 20 forks source link

Transform and coordinates #7

Closed Antorithms closed 12 months ago

Antorithms commented 2 years ago

Hi Andrew,

Thanks a lot for your pipeline. It works amazingly. I have a two questions. One is more practical, do you have any suggestion on code to transform image coordinates of points (e.g. segmented cells or something) into CCFatlas coordinates?

Secondly, i was wondering about the nature of the transform you use? Is it piecewise linear?

Thanks a lot in advance for your help. Antonio

petersaj commented 2 years ago

I just put together a function to transform image coordinates to CCF coordinates if you want to pull the updated repo - it's called AP_histology2ccf, takes a cell array of points corresponding to each slice and transforms it to CCF space. Want to give that a try at let me know if it works alright for your purposes?

The transform is just affine, so it's the same linear transform across the whole slice

Antorithms commented 2 years ago

Dear Andrew, It works! Thanks a lot.

Would there be a way then to know in which specific brain region from the Allen Brain atlas each point would sit? Basically as you would do for probe trajectories but for single points.

Thanks a bunch for your super prompt reply and update !

Best, Antonio

petersaj commented 2 years ago

Great - here's an example of how to do that, could you try this and see if it works for you?

ccf_points = AP_histology2ccf(histology_points,slice_path);
% Concatenate points and round to nearest integer coordinate
ccf_points_cat = round(cell2mat(ccf_points));
% Get indicies from subscripts
ccf_points_idx = sub2ind(size(av),ccf_points_cat(:,1),ccf_points_cat(:,2),ccf_points_cat(:,3));
% Find annotated volume (AV) values at points
ccf_points_av = av(ccf_points_idx);
% Get areas from the structure tree (ST) at given AV values
ccf_points_areas = st(ccf_points_areas,:).safe_name;

It might be worth adding that to the histology2ccf function, though I guess different people would want to use that information in different ways, so not totally sure what the most flexible output would be. Maybe just outputting the AV values in addition to the coordinates?

Antorithms commented 2 years ago

Also these lines work! Thanks a lot. I am having a problem though because i think as i go caudal in my sections something weird is happening to the location of points in ccf space. Registration as visualized by AP_view_aligned_histology is good nonetheless the points get more and more dorsal and misaligned with the region they should be in (same is observed when i try to plot in brainrender, with neurons progressively falling out of the brain from the dorsal surface). I guess this is related to how i process point location but i was wondering if to troubleshoot you could help me plot point locations on av slices for each registered section so to have a way to understand what's going on. Thanks a lot for your amazing help.

Update: I think the issue lies in the padding/rotation, centering! I am segmenting cells in a highres image which is then processed through AP_process_histology and AP_rotate_histology to generate the rotated, centered, padded, (flipped) slice images that are then registered. Is there a way to retrieve the transform and get coordinates of points in the highres image to their corresponding coordinates in the rotated, centered, padded slice images? I think this will solve the issue! Thanks again.

petersaj commented 2 years ago

Ah that's the same problem from Julien's thread in the other open issue! At the moment it doesn't store that information - I had originally envisioned working with the slice images only (and if full-res is required, just not downsampling in the first processing step).

I can see a couple ways to fix this:

1) One solution is to not downsample the images in AP_process_histology, then the resulting padded/rotated images would be where the later processing happens (also for what it's worth - AP_rotate_histology is an optional step, just makes them a little nicer to flip through)

2) Are the downsampled/flattened/color-balanced slice images from AP_process_histology useful? If so, I could just store information necessary to align the original images but keep everything else the same

3) If the slice images aren't useful, and all processing would only ever be done on the original files, it's probably more elegant to temporarily downsample/color-balance slice images for CCF alignment, and then have processing code to index and align the slices from the original slide images.

Would one of these be best for your usage?

Antorithms commented 2 years ago

Thanks a lot again for your super prompt reply!! With downsampling basically images become useless for quantification so option 1 is for sure not possible. I think the processing works very smoothly on downsampled images so I guess 2 or 3 would be the best option. I think overall option 2 should work and should be way easier and more adaptable also to other use cases, since it's just an addon that you would use in specific cases and not for example if you just want to get probe location. What we could do is give it a shot just storing the padding, rotation, flipping and so on information in a cell array which would be saved by AP_rotate_histology. This way basically adding the resize factor as input one should be able to go from coordinates in the original image to coordinates in the downsampled, rotated, padded, flipped image. Thanks again, Antonio

petersaj commented 2 years ago

For option 1 I meant not downsampling at all by setting resize_factor = 1 in AP_process_histology, so that'd give you flattened/color-balanced/rotated slice images at full resolution. But that's probably not the best general solution because the original files would have properties that'd be worth going back to.

I think you're right saving that processing information is easiest for now, I already save some info on image location so I can just add onto that - I can probably get to this within the next two weeks.

Antorithms commented 2 years ago

That sounds great, I think I will also try to get the info out of rotate_histology and let you know in case I succeed!

Antorithms commented 2 years ago

Dear Andrew, Finally after a few trials on my own I decided to avoid padding and rotation to finally identify the point coordinates from the original to the downsampled image and it works perfectly. Thanks a lot for your help.

On a side note, one thing i think could be extremely useful and is probably relatively straightforward for you knowing the code, is to add a region outline overlay on the Allen brain atlas image with the possibility to toggle at the step in which slice selection happens. Also would be cool to have the number of the Allen brain atlas slice as title in the same gui so you can move a set number of slices (each being 10um) forward or backwards easily by just reading that. Let me know what you think and thanks again.

Best, Antonio

petersaj commented 2 years ago

Great that it works, sorry for the delay it's currently a hectic time!

It would be great to have some kind of overlay at the slice selection stage, but I haven't come up with a good way to do it. Since they're unaligned (and possibly very different sizes), it would need to be at least auto-aligned to have an overlay, but that's a slow function so it'd be a real pain to refresh. I can put it on the list of things to try though, or happy to try anything else if you had something in mind.

Adding slice position title is a good shout, I've just pushed that change: image

Antorithms commented 2 years ago

Thanks a lot for the super quick update :-) and no worries for the delay!

For the overlay I meant to have it on the Allen brain image (right side of the gui) not the actual histology section to align. Sorry for not being clear, I guess that should be way easier!

Best, Antonio

petersaj commented 2 years ago

Ah gotchya - I can look into that. Out of curiosity, could you describe how that would help? When I'm just matching anatomy I'm not sure I'd necessarily want to know how areas were parsed to avoid bias

Antorithms commented 2 years ago

If you have for example a TH staining on your histology section you can readily see the anatomical arrangement of neurons in the locus coeruleus and try to find the perfect section where the outline matches the staining with the region outline overlay to toggle. I hope it's clear! Thanks a lot again, Antonio

Antorithms commented 2 years ago

Hey Andrew,

Thanks a lot for your super help throughout.

One more cool add-on to facilitate alignment could be to add in the grab_histology_ccf function the ability to retrieve single slice tilt information for saved slices when moving back and forth with keys 1 and 2. What do you think?

With a pointer on where to make changes, I could also try myself! Let me know!

Super thanks again, Antonio

petersaj commented 2 years ago

At the moment it assumes that the tilt is the same for all the slices and just grabs different slices along that vector (so if you change the tilt between slices, it will only use tilt it's set at while saving).

This should be the case with any normal histology, do you have slices on different slice axes?

Antorithms commented 2 years ago

I see, thanks a lot for the tilt info.

So for whole brain reconstruction the embedding in oct for cryopreservation can sometimes generate changes in tilt at the brainstem level and for that a slice specific (or group of slices) tilt might be very valuable.

Thanks for your super prompt reply!

petersaj commented 2 years ago

Ah interesting, didn't know it would cause that.

Is this something that you'd like to use right away? If you've got time, next month I'm hoping to overhaul some of the way things are stored (add transform needed to go back to original image, replace large atlas slices with small atlas coordinates)

Antorithms commented 2 years ago

Dear Andrew, Thanks as usual for your feedback and help.

Having the outline on the ccf during grab_histology and the ability to change the tilt section by section would be a major game changer for us!

Having the first feature soon would be great (i don't know how hard it would be, it's slightly hard for me to get in the gui code).

For the section by section tilt waiting some time should be no issue. I will also try to change grab_histology myself and let you know: the output of that function already has tilt information for all single sections but now it is the same across all sections, right?

Truly thanks for the amazing support.

Antorithms commented 2 years ago

Dear Andrew, just an update, we finally solved the tilt problem by splitting each sample in parts and it's working quite well making the registration less of a burden :-)

I though still feel that having the possibility to toggle region outlines in the grab histology template volume image would be an amazing acceleration for our workflow! Let me know if this is somehow feasible for you or if you would have any pointer on how to do it !

Super thanks in advance for all of your help with this!

petersaj commented 2 years ago

Ah great! Does that mean you break up your original slide image and get a separate alignment for each part? And you've gotten around the code currently assuming the tilt is always the same?

One quick-fix for region outlines is to toggle back and forth between the template volume and the annotated volume like in the figure below, would that help or would it be much more useful to have outlines?

image

petersaj commented 2 years ago

Actually nevermind, it was pretty simple to just include both - so now if you hit the 'm' key, it'll toggle the atlas view between TV/AV/TV with AV outlines, want to pull the current repo and try it out?

image

Antorithms commented 2 years ago

Dear Andrew, this is amazing, I will pull the repo tonight and try it right away! For the tilt assuming we have for example 40-60 sections to register we basically split the sections in groups of 15-20 and this basically allows us to keep the tilt equal for these small groups and change it across groups when needed! Again , as usual, super thanks for your help!