去除图像中的背景
Background removing in an image
我想将图像的背景更改为白色。下面的代码给我一个黑色的背景。输入图像具有白色背景。当我打印输出时,它显示黑色背景。输入图片如上
import os
import sys
import random
import warnings
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from skimage.io import imread, imshow, imread_collection,concatenate_images
from skimage.transform import resize
from skimage.morphology import label
import tensorflow as tf
X_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)
Y_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.uint8)
print('Getting and resizing train images and masks ... ')
sys.stdout.flush()
for n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):
path = TRAIN_PATH +'\'+ id_
path_image = path + '\images\'
path_mask = path + '\masks\'
for image_file, mask_file in zip(os.listdir(path_image), os.listdir(path_mask)):
img=imread(path_image+image_file)[:,:,:IMG_CHANNELS]
img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
X_train[n] = img
print(path_mask)
print(mask_file)
img2=imread(path_mask+mask_file)[:,:,:IMG_CHANNELS]
img1 = resize(img2, (IMG_HEIGHT, IMG_WIDTH,1), preserve_range=True)
Y_train[n] = img1
#print(img2[0][0])
plt.imshow(img2)
plt.show()
实际上背景已经是黑色的(RGB 值为 0),但它看起来是白色的,因为它是完全透明的(alpha 值为 0)。和
img=imread(path_image+image_file)[:,:,:IMG_CHANNELS]
您正在删除 alpha 通道(假设 IMG_CHANNELS = 3
),它包含图像中像素的透明度。由于不再有透明度,背景现在显示为黑色。如果要将图像保留为 RGB 格式,则可以在 alpha 通道为 0 的任何地方将像素设为白色(如@HansHirse 在评论中建议的那样):
from skimage.io import imread
img_rgba = imread('https://i.stack.imgur.com/BmIUd.png')
# split RGB channels and alpha channel
img_rgb, img_a = img_rgba[..., :3], img_rgba[..., 3]
# make all fully transparent pixels white
img_rgb[img_a == 0] = (255, 255, 255)
如果您仍然希望叶子边缘在 RGB 中看起来相同,您也可以查看 Convert RGBA PNG to RGB with PIL 的答案。
否则你应该保持你的图像为 RGBA 格式。
我想将图像的背景更改为白色。下面的代码给我一个黑色的背景。输入图像具有白色背景。当我打印输出时,它显示黑色背景。输入图片如上
import os
import sys
import random
import warnings
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from skimage.io import imread, imshow, imread_collection,concatenate_images
from skimage.transform import resize
from skimage.morphology import label
import tensorflow as tf
X_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)
Y_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.uint8)
print('Getting and resizing train images and masks ... ')
sys.stdout.flush()
for n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):
path = TRAIN_PATH +'\'+ id_
path_image = path + '\images\'
path_mask = path + '\masks\'
for image_file, mask_file in zip(os.listdir(path_image), os.listdir(path_mask)):
img=imread(path_image+image_file)[:,:,:IMG_CHANNELS]
img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
X_train[n] = img
print(path_mask)
print(mask_file)
img2=imread(path_mask+mask_file)[:,:,:IMG_CHANNELS]
img1 = resize(img2, (IMG_HEIGHT, IMG_WIDTH,1), preserve_range=True)
Y_train[n] = img1
#print(img2[0][0])
plt.imshow(img2)
plt.show()
实际上背景已经是黑色的(RGB 值为 0),但它看起来是白色的,因为它是完全透明的(alpha 值为 0)。和
img=imread(path_image+image_file)[:,:,:IMG_CHANNELS]
您正在删除 alpha 通道(假设 IMG_CHANNELS = 3
),它包含图像中像素的透明度。由于不再有透明度,背景现在显示为黑色。如果要将图像保留为 RGB 格式,则可以在 alpha 通道为 0 的任何地方将像素设为白色(如@HansHirse 在评论中建议的那样):
from skimage.io import imread
img_rgba = imread('https://i.stack.imgur.com/BmIUd.png')
# split RGB channels and alpha channel
img_rgb, img_a = img_rgba[..., :3], img_rgba[..., 3]
# make all fully transparent pixels white
img_rgb[img_a == 0] = (255, 255, 255)
如果您仍然希望叶子边缘在 RGB 中看起来相同,您也可以查看 Convert RGBA PNG to RGB with PIL 的答案。 否则你应该保持你的图像为 RGBA 格式。