keflavich / image_registration

Image Registration for Astronomy
MIT License
154 stars 53 forks source link

Help registering two images of different sizes and pixel scales #21

Open AlexaVillaume opened 6 years ago

AlexaVillaume commented 6 years ago

Hello,

First, thank you for writing and making public a python image registration tool. I'm trying to use it to align two images from two different instruments and telescopes. They are of two very different FoVs and pixel scales. To deal with that, before I did the registration, I tried resampling the wider FoV image to be on the smaller FoV image's WCS (using the reproject package). I then tried to use the reprojected image to get offsets between the wider and narrower image such that I can correct the original image. That doesn't seem to work. The offsets for the reprojected image are not valid for the original image. I also tried creating a cutout from the original wider FoV image, getting offsets from that, and then trying to correct the original image. But I have the same problem as the other method.

I'd appreciate any suggestions about how I might get the wider FoV image aligned with the smaller FoV image.

I'm copying my code to show what I've attempted to do:

Attempt 1:

kcwi = fits.open('KCWIData/sci_2Dmap.fits')
hst  = fits.open('hst_hvii.fits')

# Need to remake both images to only be 2D (RA and DEC)
new_hdr = hst[0].header
new_hdr['NAXIS'] = 2
new_hdr.remove('NAXIS3')
new_hdr.remove('CTYPE3')
tmp     = fits.PrimaryHDU(hst[0].data[0,:,:], header=new_hdr)
new_hst = fits.HDUList([tmp])

new_hdr = kcwi[0].header
new_hdr.remove('CTYPE3')
new_hdr.remove('CUNIT3')
new_hdr.remove('CNAME3')
new_hdr.remove('CRVAL3')
new_hdr.remove('CRPIX3')
new_hdr.remove('CD3_3')
tmp      = fits.PrimaryHDU(kcwi[0].data, header=new_hdr)
new_kcwi = fits.HDUList([tmp])

# Reproject the HST image to be on the WCS of KCWI
reproj_hst, footprint = reproject_interp(new_hst[0], new_kcwi[0].header)
# Find the 2D difference between the reprojected HST image and KCWI
xoff, yoff, exoff, eyoff = chi2_shift(new_kcwi[0].data, reproj_hst,
                                      upsample_factor='auto')

# Use those correction factors to correct the original HST image
# NOTE: This doesn't work
#corrected_hst = shift.shiftnd(reproj_hst, (-yoff, -xoff))
corrected_hst = shift.shiftnd(new_hst[0].data, (-yoff, -xoff))

Attempt 2:

# Need to make the HST image the same size as the KCWI image
w        = wcs.WCS(new_hdr)
cutout = Cutout2D(hst[0].data[0,:,:], position, size, wcs=w)
tmp      = fits.PrimaryHDU(cutout.data, header=new_hdr)
new_hst = fits.HDUList([tmp])

# Find the 2D difference between the reprojected HST image and KCWI
xoff, yoff, exoff, eyoff = chi2_shift(new_kcwi[0].data, new_hst[0].data,
                                      upsample_factor='auto')

# Use those correction factors to correct the original HST image
# NOTE: This doesn't work
corrected_hst = shift.shiftnd(hst[0].data, (-yoff, -xoff))
keflavich commented 6 years ago

Have a look at the tools in FITS_tools, particularly register_fits. It gives WCS offsets rather than pixel offsets, which you should be able to use for this purpose.

AlexaVillaume commented 6 years ago

Thanks for pointing me towards FITS_tools. Is there an analog to shiftnd that uses the WCS offsets?

keflavich commented 6 years ago

No, for the WCS offsets, you just need to edit the header of the original file. shiftnd only makes sense if you're trying to shift two images that are on the same pixel grid (i.e., same # of pixels in each dimension) such that their pixels line up. I think you're asking for something different, where you want to find the on-sky shift between two images and align them in their original pixel grids. Is that right?

You should be modifying the CRVAL values in the FITS header. If they're already aligned rotationally, this is easy, if not, it's tricky.

AlexaVillaume commented 6 years ago

I think you're asking for something different, where you want to find the on-sky shift between two images and align them in their original pixel grids. Is that right?

Yes, that's right. And, yes, luckily the images are aligned rotationally so editing the CRVAL values did the trick. Thank you for your help!

bala93kumar commented 5 years ago

@AlexaVillaume , I have encountered such issue as you . Am trying to compare two images of different pixels to plot out the difference between the pics using opencv python it is throwing error that the numpy arrays are not same could you help me on this.