如何使用python将二值图像转换为灰度和RGB?

2024-06-07 11:49:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究从皮肤损伤图像中去除毛发。有没有办法把二进制转换回rgb?

原始图像:

enter image description here

遮罩图像:

enter image description here

我只想用原始图像恢复黑色区域。


Tags: 图像区域二进制rgb皮肤黑色办法
2条回答

据我所知,二进制图像以灰度形式存储在opencv值1-->;255中。

要创建“虚拟”RGB图像,可以执行以下操作: rgb_img = cv2.cvtColor(binary_img, cv.CV_GRAY2RGB)

我称之为“虚拟”,因为在这些图像中,红色、绿色和蓝色的值是一样的。

像这样,但是你的遮罩大小不对(200x200像素),所以它与你的图像不匹配(600x450像素):

#!/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')

enter image description here

相关问题 更多 >

    热门问题