#!/usr/local/bin/python3
from PIL import Image
import numpy as np
# Open the input image as numpy array
npImage=np.array(Image.open("image.jpg"))
# Open the mask image as numpy array
npMask=np.array(Image.open("mask2.jpg").convert("RGB"))
# Make a binary array identifying where the mask is black
cond = npMask<128
# Select image or mask according to condition array
pixels=np.where(cond, npImage, npMask)
# Save resulting image
result=Image.fromarray(pixels)
result.save('result.png')
据我所知,二进制图像以灰度形式存储在opencv值1-->;255中。
要创建“虚拟”RGB图像,可以执行以下操作:
rgb_img = cv2.cvtColor(binary_img, cv.CV_GRAY2RGB)
我称之为“虚拟”,因为在这些图像中,红色、绿色和蓝色的值是一样的。
像这样,但是你的遮罩大小不对(200x200像素),所以它与你的图像不匹配(600x450像素):
相关问题 更多 >
编程相关推荐