0

Currently I'm replacing ImageMagick library with OpenCV in my project and encountered strange problem when working with transparent images (png format). When I load image which contains alpha channel - the image appears "broken", sometimes (when only background is transparent) it just has black background, but when contents of the image is itself transparent then it looks like on the screenshot. As far as I tried - the problem is reproducible in C++ and Python.

I tried converting from BGRA to RGBA - the issue is still there but with slightly different color in the resulting image. I also tried it with several images I found on the Internet so I suppose it's not just one broken image, but something with OpenCV or the way I try to use it. When I was using ImageMagick (v7) in C++ - images were loaded properly without such issues (but I still need to replace it due to other reasons, so sticking to the old library isn't an option).

The main question:

Is there something I should know to be able to read such images properly or this is just the way OpenCV works?

I'm using OpenCV versions 4.4.0 in Python and 4.3.0 in C++.

This is the code I'm using in Python:

import cv2

img = cv2.imread('/home/monkeber/Pictures/trans2.png', cv2.IMREAD_UNCHANGED)

cv2.imshow("image", img)
cv2.waitKey(0) 

resized = cv2.resize(img, (0, 0), fx=0.5, fy=0.5, interpolation = cv2.INTER_LINEAR)

cv2.imshow("Resized image", resized)
cv2.waitKey(0) 

UPD. It appears the same problem is present in Imgur service which is used on StackOverflow, when I tried to make images look smaller in the question - I had the exact same effect on the original image.

Screenshot with the issue and original image to reproduce: Screenshot Original image

monkeber
  • 73
  • 2
  • 5
  • 1
    I loaded your 2nd image into GIMP on my side and "removed" the Alpha channel. Then I get the exact same output like in your right view of the 1st image (snapshot). I've got the impression that the OpenCV view just doesn't show the alpha channel but only the RGB values. The strange background you see is already there in your original image - just "suppressed" by the alpha values. – Scheff's Cat Aug 25 '20 at 14:43
  • 1
    @Scheff indeed that was the problem, thank you! – monkeber Aug 26 '20 at 08:19

0 Answers0