Issue
I have a black and white image that I am loading into python. If I use pillow or cv2 I get two different answers for the dimension of the NumPy array that is created. I understand that channel ordering (RGB vs BGR) is different from openCV and pillow but I don't think that's what's going on here
Is my image 2 dimensions as pillow represents it. Does this mean that openCV duplicates the values into a 3D array?
import cv2
from PIL import Image
import numpy as np
path = 'path/to/file.png'
#using pillow
img = Image.open(path)
img.size #(500,500)
img.mode # L
arr = np.array(img)
arr.shape #(500,500)
#using cv2
image = cv2.imread(path)
image.shape #(500,500,3)
If I run file
in a bash terminal
$ file 'path/to/image.png'
path/to/image: PNG image data, 500 x 500, 8-bit grayscale, non-interlaced
Solution
The cv2.imread
function takes two arguments: filename, and flag. The flag is set to cv2.IMREAD_COLOR
(or 1) by default. PIL on the other hand automatically loads your image in grayscale mode, 'L'. If you want your cv2 image to work the same way, do:
image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
You can also convert your grayscale PIL image to 3 channels:
img = Image.open(path).convert('RGB')
Answered By - Mercury
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.