CBICA / HistoReg

Framework for registration of sequential digitized histology slices
Other
28 stars 7 forks source link

How to convert NIfTI Image to JPG or PNG format? #22

Closed elephanttt closed 3 years ago

elephanttt commented 3 years ago

I encountered pathology images in NIfTI format for the first time, How can I convert it to the jpg or png format for further processing?

ludovenet commented 3 years ago

Hello, thank you for your interest in our method !

If you want to convert NifTI images to jpg or png ones, you can use c2d : https://sourceforge.net/p/c3d/git/ci/master/tree/doc/c3d.md

The command should look like : $Path_to_c2d_executable Your_image.nii -o Your_image.png

Let me know if it helps, Best, Ludovic Venet.

sarthakpati commented 3 years ago

As an added note, c2d is present in the ${install_dir}/bin once you complete the make install/strip step.

elephanttt commented 3 years ago

I wrote a simple piece of python code to convert the NifTI images to jpg ones. It is strange that full_resolution RGB registeredImage.jpg was not being converted correctly(an all white image was generated), nevertheless full_resolution new_source.jpg and new_target.jpg were converted correctly. This is my code. import nibabel as nib import numpy as np from PIL import Image fn = nib.load('E:/output/CD68_registered_to_CD4/Saved_NIFTIs/full_resolution/registeredImage.nii.gz') img = fn.get_fdata().astype(np.float32).squeeze() img[:,:,0]=255-np.uint8(255*img[:,:,0]) img[:,:,1]=255-np.uint8(255*img[:,:,1]) img[:,:,2]=255-np.uint8(255*img[:,:,2]) a = Image.fromarray(np.uint8(img)) a.save('E:/output/CD68_registered_to_CD4/Saved_NIFTIs/full_resolution/registeredImage.jpg') what is the problem with that?

elephanttt commented 3 years ago

when I use c2d command, errors happen like that "ITK Exception: itk::ExceptionObject (0000001F0935D400) Location: "void __cdecl itk::PNGImageIO::WriteSlice(const class std::basic_string<char,struct std::char_traits,class std::allocator > &,const void *)" File: E:\HistoReg\bin\ITK-source\Modules\IO\PNG\src\itkPNGImageIO.cxx Line: 567 Description: PNG supports unsigned char and unsigned short

Unknown exception caught by convert3d" Can you give me a hand?

sarthakpati commented 3 years ago

Hi @elephanttt, did you fix this?

elephanttt commented 3 years ago

I have installed the HistoReg correctly. but run the example that perform registration from CD68.jpg to CD4.jpg with the command line ${installDir}/bin/HistoReg -m ${HistoReg_source}/Data/Images/CD68.jpg -f ${HistoReg_source}/Data/Images/CD4.jpg -o ${HistoReg_source}/Data/Output/ -F --PNG , the output registrated image with .png format is a still a gray-scale image that shows nothing.

elephanttt commented 3 years ago

I think if convert the R/G/B (three channels) image to gray-scale image before registration, the output R/G/B registered image can't be obtained theoretically. The rgb2gray operation is not a one-to-one mapping. Its unique inverse function doesn't exist. (It has multiple inverse functions)

sarthakpati commented 3 years ago

Pinging @ludovenet