0

I want to change the background of an image to white. The following code gives me a black background. the input image has a white background. when i print output, it shows black background. Input image is given above input 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()
HansHirse
  • 18,010
  • 10
  • 38
  • 67
Nemesis
  • 191
  • 1
  • 1
  • 11
  • You could change the value of the background pixels from 0 to 255 (for 8-bit image) – Dr. H. Lecter Dec 18 '19 at 16:25
  • @Dr.H.Lecter in my image there is some dark spot that has to stay. if i change the pixel values, i think those dark spots will go away too – Nemesis Dec 18 '19 at 17:15
  • Please think about what folk might need in order to answer you... you have not provided any input images, nor any indication of what output you expect. You have also removed the `import` statements so we don't know if you are using OpenCV or `imageio` or something else to load your images. – Mark Setchell Dec 18 '19 at 17:48
  • @MarkSetchell the code was too long to post. hope this helps. thanks mate – Nemesis Dec 18 '19 at 18:17
  • @Nemesis Ok there are some dark spots which need to stay. But hopefully you could have the background with pixels values =0, dark spots with pixels values =1, and the rest of the grays values for the object. If you change only the values 0 to 255 than you would keep the dark spots. – Dr. H. Lecter Dec 18 '19 at 20:07
  • Your current input image already has a white background. How can you change the background of the image to white if its already white? – nathancy Dec 18 '19 at 21:41
  • 4
    @nathancy Actually, the background is black, but the alpha channel for the background is (also) `0`, so the image background just appears white on the white background of Stack Overflow. Solution is quite easy: Since the object seems to have `> 0` alpha channel, whereas the background has `0` alpha channel, just set the RGB values of your image to `(255, 255, 255)` where alpha channel is `0`. Overcomes also the problem, that maybe existing black pixels within the object are preserved. Another method would be finding the object's contour, mask the background and set it to white. – HansHirse Dec 19 '19 at 09:13

1 Answers1

2

Actually the background was already black (RGB values of 0), but it appeared white because it was completely transparent (alpha values of 0). With img=imread(path_image+image_file)[:,:,:IMG_CHANNELS] you are removing the alpha channel (assuming IMG_CHANNELS = 3), which contains the transparency of the pixels in the image. Since there is no more transparency, the background now appears black. If you want to keep the image in RGB format, you can make the pixels white wherever the alpha channel is 0 (as @HansHirse suggested in a comment):

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)

Result

You can also take a look at the answers of Convert RGBA PNG to RGB with PIL if you still want the leaf edges to look the same in RGB. Otherwise you should keep your image in RGBA format.

Jonathan Feenstra
  • 2,534
  • 1
  • 15
  • 22