Ask Your Question

OpenCV + Astropy + FITS Unrecognized or Unsupported Array Type

asked 2018-12-27 11:57:26 -0500

noel gravatar image

updated 2018-12-27 12:13:32 -0500

I'm trying to read a FITS image file and display it using imshow. I can read in an image and I've checked that it isn't empty and has the expected dimensions. I also checked the image type which is '>i2' AKA big endian signed 16-bit integer. I've tried converting it into a 32-bit float image, an 8-bit unsigned int image, and 16-bit unsigned int image. Every time though I get the same error:

cv2.error: OpenCV(3.4.3) /io/opencv/modules/core/src/array.cpp:2492: error: (-206:Bad flag (parameter or structure field)) Unrecognized or unsupported array type in function 'cvGetMat'

when I call the imshow function. I've also tried to use pyplot to display the image as shown here. Then I got a new error:

TypeError: Invalid dimensions for image data

Below is the code I'm using.

import cv2
from astropy import fits

with as hdul:
      print(f'length: {len(hdul)}')
      img = hdul[0].data
      cv2.imshow('Viewer', img)

      if cv2.waitKey(1) & 0xFF == ord('q'):

I've upload an example image to my Google Drive in case anyone wants to try the code.

How can I display this image using imshow?

edit retag flag offensive close merge delete


you need to make a numpy array from it

berak gravatar imageberak ( 2018-12-27 12:05:01 -0500 )edit

is (1 hour ago) just for me ? anyone reported it recently :)

sturkmen gravatar imagesturkmen ( 2018-12-27 12:09:52 -0500 )edit

@sturkmen, the clock is off by 1 for anyone here ;) (and it also was always like this)

berak gravatar imageberak ( 2018-12-27 12:13:16 -0500 )edit

@berak the img variable is a numpy array

noel gravatar imagenoel ( 2018-12-27 12:26:27 -0500 )edit

try cv2.imshow('Viewer', img[0,:,:])

LBerger gravatar imageLBerger ( 2018-12-27 15:59:57 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-12-27 16:40:57 -0500

noel gravatar image

updated 2018-12-27 17:19:45 -0500

I found the solution. My image array had the number of color channels as the first element like this (1, 480, 640) in other words it was (color, width, height), but OpenCV didn't expect 640 color channels. So, I had to swap the axes around like so:

 img = np.swapaxes(img, 0, 2)
 img = np.swapaxes(img, 0, 1)
edit flag offensive delete link more


@noel i converted your comment as an answer. try to edit it.

sturkmen gravatar imagesturkmen ( 2018-12-27 17:07:58 -0500 )edit

Question Tools

1 follower


Asked: 2018-12-27 11:57:26 -0500

Seen: 556 times

Last updated: Dec 27 '18