import numpy as np
import cv
from PIL import Image
# open the image
img = Image.open('./pic.png', 'r')
r,g,b, alpha_channel = img.split()
mask = np.array(alpha_channel)
# all elements in alpha_channel that have value 0
# are set to 1 in the mask matrix
mask[alpha_channel==0] = 1
# all elements in alpha_channel that have value 100
# are set to 0 in the mask matrix
mask[alpha_channel==100] = 0
신용 : How to get alpha value of a PNG image with PIL? Converting numpy array having image data to CvMat
cvmat에 NumPy와 배열을 변환하려면 수행
cv_arr = cv.fromarray(mask)
확인 마스크의 DTYPE을. 나는 내 cv_arr가 변환 할 때 그것은 DTYPE ('UINT8') 해야 cvmat (유형 = 42424000 8UC1 행 = 1245 COLS = 2400 단계 = 2400)
내가 OpenCV의 전문가 아니지만, 그것은 보인다 나에 따르면 8UC1은 dtype이 uint8이라는 사실에 기반하여 자동으로 선택된다. (나는 그것에 관한 문서를 찾을 수 없기 때문에 여기서 추측하고있다.)