Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Return rgb when rgba is loaded #734

Merged
merged 15 commits into from
Feb 6, 2019
Merged

Conversation

yuyu2172
Copy link
Member

@yuyu2172 yuyu2172 commented Nov 18, 2018

Sometimes, cv2.imread returns (H, W, 4) array (e.g., the image below).
It seems that this behavior is dependent on the build of cv2.
The bug occurred with the cv2 installed using anaconda, but the same problem did not occur for cv2 installed with apt.

000320

EDIT:
Alpha channel extracted by img = cv2.imread(path)[:, :, 3]
figure_1

RGB extracted by img = cv2.imread(path)[:, :, :3][:. :. ::-1]
figure_1-2

PIL
f = Image.open(path); img = f.convert('RGB');
figure_1-2

@yuyu2172 yuyu2172 added the bug label Nov 19, 2018
@yuyu2172 yuyu2172 added this to the 0.12 milestone Nov 19, 2018
@yuyu2172 yuyu2172 changed the title return rgb when rgba is loaded Return rgb when rgba is loaded Nov 20, 2018
@@ -22,6 +22,11 @@ def _read_image_cv2(path, dtype, color):
# reshape (H, W) -> (1, H, W)
return img[np.newaxis].astype(dtype)
else:
# alpha channel is inclued
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

included?

if img.shape[-1] == 4:
bgr = img[:, :, :3]
alpha = img[:, :, 3:] / 255
img = bgr * alpha + 255 * np.ones_like(bgr) * (1 - alpha)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are three options.

  1. just ignore alpha channel
  2. blend with black image
  3. blend with white image

Why do you choose 3.? Personally, I prefer 1.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because the alpha channel contains meaningful information (for some part, the value is in (0, 1)).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. is not acceptable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 2 better?
I thought 3 was more natural

@yuyu2172
Copy link
Member Author

yuyu2172 commented Feb 3, 2019

@Hakuyume
Please review this again.

@Hakuyume Hakuyume merged commit c722816 into chainer:master Feb 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants