allenCCF
allenCCF copied to clipboard
affine transform result is very much skewed depending on the size of images used
I'm pretty sure this will not be responded, but I report this anyway, hoping that this could save someone else's precious time.
I think I found an error in the code.
In sliceBrowser.m
, when the tissue image is big, imresize()
is called and the image is shrunk to the same size as the reference atlas image eg. [800, 1140] for coronal image.
This shrunk image will be used for the affine transform later.
https://github.com/cortex-lab/allenCCF/blob/0bbff55fc906fd3f023da81ce1d0e4b8726d4fd0/Browsing%20Functions/sliceBrowser.m#L110C1-L124C47
function ud = updateSliceImage(ud)
title_ending = '';
processed_image_name = ud.processed_image_names{ud.slice_num};
current_slice_image = flip(imread(fullfile(ud.processed_images_folder, processed_image_name)),1);
% reduce to 3 channels at most
color_channels = min( 3, size(current_slice_image,3));
current_slice_image = current_slice_image(:,:,1:color_channels);
% reduce to reference atlas size
if size(current_slice_image,1) > ud.ref_size(1)+2 || size(current_slice_image,2) > ud.ref_size(2)+2
disp(['shrinking image to reference size ' num2str(ud.ref_size(1)) ' x ' num2str(ud.ref_size(2)) ' pxl'])
current_slice_image = imresize(current_slice_image, ud.ref_size);
end
set(ud.im, 'CData', current_slice_image);
However in AtlasTransformBrowser.m
, when the result of the transform are saved as an image, the image is directly loaded with imread()
and used for transformation by imwarp()
without resizing before. So the transfrom will give you very strange results depending on the aspect ratio of your tissue image. Perhaps imref2d
was meant to avoid this problem but in my hands that's not working and curr_slice_trans
is very much skewed.
https://github.com/cortex-lab/allenCCF/blob/0bbff55fc906fd3f023da81ce1d0e4b8726d4fd0/Browsing%20Functions/AtlasTransformBrowser.m#L696C9-L700C101
current_slice_image = imread(fullfile(save_location, [slice_name '.tif']));
% current_slice_image = flip(get(ud_slice.im, 'CData'),1);
R = imref2d(size(ud.ref));
curr_slice_trans = imwarp(current_slice_image, ud.transform, 'OutputView',R);
imwrite(curr_slice_trans, fullfile(folder_transformations, [slice_name '_transformed.tif']))
It is commented out, but current_slice_image = flip(get(ud_slice.im, 'CData'),1);
will give you the image in the size of [840, 1140] and should work better. Otherwise we need to add imresize()
around here.
Another way to fix this is to get rid of imresize()
altogether.
Although it looks terribly wrong and confusing, the transform points are saved correctly, so the actual damage seems small.
A: Slice Viewer. Note that the image is shrunk horizontally due to imresize()
. For the purpose of validating the performance of SharpTrack, I am using the images from the printed atlas instead of microscopic images. B: Atlas viewer. Overlaid with the transformed slice image. Note that the affine transformation is successful overall. C: The exported TIF image found in the transformations
folder from the same transform as in panel C. The scaling of the transform is messed up because of the ignorance of the effect of imresize()
to the size of [840, 1140]. The original image I used had the dimension of [969, 1761]. D: The exported TIF image after a fix by uncommenting current_slice_image = flip(get(ud_slice.im, 'CData'),1)
. This issue may not be obvious if your tissue image has roughly the same aspect ratio as the atlas image or they are smaller than the atlas image (because, in that case, imresize()
will not be used).