簡體   English   中英

如何使用 python 將二進制圖像轉換為灰度和 RGB?

[英]How to convert a Binary Image to Grayscale and RGB using python?

我正在研究從皮膚病變圖像中去除毛發。 有沒有辦法將二進制轉換回rgb?

原圖:

在此處輸入圖像描述

蒙版圖片:

在此處輸入圖像描述

我只想用原始圖像恢復黑色區域。

像這樣的東西,但是您的蒙版大小錯誤(200x200 px),因此與您的圖片(600x450 px)不匹配:

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

我稱它們為“虛擬”,因為在這些圖像中,紅色,綠色和藍色值是相同的。

我更新了 Daniel Tremer 的回答

import cv2
opencv_rgb_img = cv2.cvtColor(opencv_image, cv2.COLOR_GRAY2RGB)

由於二進制, opencv_image將是像 [width, height] 這樣的二維矩陣。
由於 RGB, opencv_rgb_img將是像 [width, height, color channel] 的三維矩陣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM