Reputation: 3
I am not sure of why this is happening but I am not able to load image using imread(). I am able to open that image in paint and after saving that image, the image is being loaded and displayed. I am using Jupyter notebook.
import os
import cv2
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
def displayImage(image):
plt.imshow(image)
plt.show()
image = cv2.imread('path/to/image')
displayImage(image)
Output
Expected Output:
Upvotes: 0
Views: 3074
Reputation: 1006
It happens because your image is in RGBA
mode (your background is transparent).
so you need to read your image in RGBA
mode as:
image = cv2.imread('path/to/image.png',-1)
or:
from scipy.ndimage import imread
rgba = imread('path/to/image.png', mode='RGBA')
the result:
Upvotes: 4
Reputation: 207853
The issue is that your image doesn't contain any non-zero Red, Green or Blue pixels, it is entirely black. The only reason it looks how you show it with "@ @ 6 L"
is because it has a an alpha/transparency channel that masks the black out and reveals the white PNG background colour.
If you look at it with ImageMagick's identify
you will see:
identify -verbose a.png | more
Image: a.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 203x50+0+0
Resolution: 37.79x37.79
Print size: 5.37179x1.3231
Units: PixelsPerCentimeter
Colorspace: sRGB
Type: Bilevel
Base type: Undefined
Endianess: Undefined
Depth: 8-bit
Channel depth:
Red: 1-bit
Green: 1-bit
Blue: 1-bit
Alpha: 8-bit
Channel statistics:
Pixels: 10150
Red:
min: 0 (0)
max: 0 (0) <--- Brightest Red is zero
mean: 0 (0)
standard deviation: 0 (0)
kurtosis: -3
skewness: 0
entropy: 0
Green:
min: 0 (0)
max: 0 (0) <--- Brightest Green is zero
mean: 0 (0)
standard deviation: 0 (0)
kurtosis: -3
skewness: 0
entropy: 0
Blue:
min: 0 (0)
max: 0 (0) <--- Brightest Blue is zero
mean: 0 (0)
standard deviation: 0 (0)
kurtosis: -3
skewness: 0
entropy: 0
Alpha:
min: 0 (0)
max: 255 (1) <--- Alpha channel is only one with info
mean: 16.477 (0.0646159)
standard deviation: 58.73 (0.230314)
kurtosis: 10.7342
skewness: 3.50997
entropy: 0.128008
...
...
Background color: white <--- Background is white
...
...
The answer is to read ALL FOUR channels with cv2.IMREAD_UNCHANGED
, and just use the 4th/alpha channel:
def read_transparent_png(filename):
image_4channel = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
alpha_channel = image_4channel[:,:,3]
rgb_channels = image_4channel[:,:,:3]
Code extracted from here.
Upvotes: 1
Reputation: 131
After loading the image use that:
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
Upvotes: 0
Reputation: 96167
andCheck what data is actually being loaded. Check the size with image.shape(), either look at the max/min/mean values or if you use spyder (highly recommended), look at the data in the variables viewer.
ps. for a single display item there is no need for the plt.show()
command
Upvotes: 0