1

In my Qt application I have image data as a numpy.ndarray. Usually that comes from cv2.imread(), which I then convert to a QImage as follows:

height, width, channel = cvImg.shape
bytesPerLine = 3 * width
qImg = QImage(cvImg.data, width, height, bytesPerLine, QImage.Format_RGB888)

This works fine, the QImage can be converted to a pixmap and painted to a label. Now in some cases I don't get the image data from a file via imread(), but instead directly from a camera. This data is also a numpy.ndarray, I can save it via cv2.imwrite() (and then open it in an image viewer). However, using the code above I cannot convert that image data directly to a QImage, the result is a red-ish image without any details, just some vertical lines.

Now since I can save that camera image data it seems to be valid, I just need to find the correct image format when calling the QImage constructor (I guess). I tried several of them, but none worked. So how can I determine in which format this image data is?

Matthias
  • 9,817
  • 14
  • 66
  • 125
  • You should be able to tell the image format from your camera settings. If that isn't possible for some reason, try looking at the `shape` and the value range of the array. That might give you a hint to whether the information is stored for example in rgb, counts, or something else or if there is an alpha channel. – mapf May 12 '20 at 16:12
  • @mapf It's a network camera from *The Imaging Source*, which shoud give me a 32 bit image. I can tell it has 4 channels by `cvImg.shape`. But how can i determine the **exact** format - e.g. how does openCV does it when saving the data to a file? – Matthias May 12 '20 at 16:24
  • @Matthias The shape does not determine the format, several formats can generate the same shape. A trivial example would be an RGB and BGR image (also HSV, etc.) try with this code: https://stackoverflow.com/a/51608230/6622587 – eyllanesc May 12 '20 at 16:39
  • @eyllanesc Thank you very much for the link, using the `qimage2ndarray` package that is mentioned there it works fine now. I learned however that my knowledge of all these image formats and conversions fromt/to `numpy` is less than sufficient. Do you know a good tutorial on the theory of that topic? – Matthias May 12 '20 at 16:57
  • No, numpy has nothing to do with the format but the library that gets the information such as opencv, PIL, etc. Numpy is a library that provides a container, and the other libraries fill that container with raw data in a certain format – eyllanesc May 12 '20 at 17:02
  • @eyllanesc To be fair, I didn't say the `shape` *determines* the format, I only said it might give a *hint*. Sorry if this was missleading though. Your suggestion is much better of course. – mapf May 12 '20 at 17:53

0 Answers0