Image Utilities

nuskybgd.image.crop_image(image)[source]

Crop an image that has a padding of 0 value so that there is no empty margin. Return the cropped image, and the x, y offsets of the new image coordinates relative to the uncropped image. If there is no non-zero value in the image, returns False.

Parameters

image (numpy.ndarray) – The input image.

Returns

  • (numpy.ndarray, [int, int]) – The cropped image and the [x, y] offsets relative to the uncropped image.

  • False – If the image has no non-zero pixels.

nuskybgd.image.get_aspect_hist_image(asphistimgfile)[source]

Return an image of the aspect histogram and x and y offsets if any (from the X_OFF and Y_OFF keywords in header).

nuskybgd.image.get_aspect_hist_peak(asphistimg, xoff=0, yoff=0)[source]

Return the (x, y) array indices of the maximum in the aspect histogram image.

TODO: what to do if there are two such pixels

nuskybgd.image.get_caldb_instrmap(evthdr)[source]

Get instrument map image from CALDB, shifted by (-1, -1).

Parameters

evthdr (astropy.io.fits.Header) – The FITS header of the event file to get the CALDB instrument map for.

Returns

The CALDB instrument map and the header of the instrument map extension.

Return type

(numpy.ndarray, astropy.io.fits.Header)

nuskybgd.image.get_det_mask(instmap_mask_file, detnum)[source]

Create image masks of individual detectors from the instrument map mask image.

TODO: add input validation.

Parameters
  • instmap_mask_file (str) – Path to the instrument mask file.

  • detnum (int or list of int) – Detector number(s).

Returns

3D numpy array, containing images with the same shape as the instrument mask, with the active area of the detector set to 1, and elsewhere set to 0.

Return type

np.ndarray[det, y, x]

nuskybgd.image.get_evtfile_pa(evtfile)[source]

Get position angle from events file header.

Parameters

evtfile (str) – Path to the events file.

Returns

The position angle.

Return type

float

nuskybgd.image.transform_image(image, shiftx, shifty, angle, order=1)[source]

Apply shift and rotation to the image.

The translation is applied first, then the rotation. If no rotation is requested (angle=0), then scipy.ndimage.shift() is called to perform a translation. Otherwise, scipy.ndimage.affine_transform() is called. In both cases the settings mode='wrap', prefilter=False are used. Prefilter must be turned off because it applies lossy image sharpening leading to artifacts.

Parameters
  • image (numpy.ndarray) – 2D image input.

  • shiftx (float) – Shift in the x-axis in pixels.

  • shifty (float) – Shift in the y-axis in pixels.

  • angle (float) – Rotation angle in radians (positive is clockwise).

  • order (int) – (Optional, default: 1) Spline interpolation order. 1 for bilinear, 3 for bicubic (bilinear is the original behavior).

Returns

Transformed image.

Return type

numpy.ndarray

Notes

The transformation is implemented as sequence of affine transformations. The scipy module takes a matrix of the form (ndim + 1, ndim + 1), where it assumes that the transformation is specified using homogeneous coordinates. This matrix has the 2x2 rotation matrix in the top left corner, and the linear shifts in the top right. They are applied in this order:

1  0  shiftx
0  1  shifty
0  0  1
(translation by shift amounts)

1  0  -(X-1)/2
0  1  -(Y-1)/2
0  0  1
(translation to center rotation on the IDL rot center)

cos  sin 0
-sin cos 0
0    0   1
(clockwise rotation)

1  0  +(X-1)/2
0  1  +(Y-1)/2
0  0  1
(undo translation for center of rotation)