Get transparent background by using mask, I get white background - python

def combined_display(image, matte):
# calculate display resolution
w, h = image.width, image.height
rw, rh = 800, int(h * 800 / (3 * w))
# obtain predicted foreground
image = np.asarray(image)
if len(image.shape) == 2:
image = image[:, :, None]
if image.shape[2] == 1:
image = np.repeat(image, 3, axis=2)
elif image.shape[2] == 4:
image = image[:, :, 0:3]
matte = np.repeat(np.asarray(matte)[:, :, None], 3, axis=2) / 255
foreground = image * matte + np.full(image.shape, 255) * (1 - matte)
foreground = Image.fromarray(np.uint8(foreground))
foreground.save("imag.png", format="png")
return foreground
I'm trying to get transparent background like removebg, I don't want white background. Please help me with removing background and get transparent background.

Suppose you have a 3-channel image and a 1-channel mask. Then you can stack them together into a 4-channel image where the last channel is responsible for transparency.
import numpy as np
from PIL import Image
image = Image.open('image.png')
mask = Image.open('mask.png').convert('L')
image_with_transparency = np.dstack((image, mask))
Image.fromarray(image_with_transparency).save('image_trs.png')
The result:

Related

Python making Hologram pyramid

I am studying on hologram vision. I want to placed 4 images onto black screen.
I wrote this code:
import numpy as np
import cv2
from screeninfo import get_monitors
if __name__ == '__main__':
screen = get_monitors()[0]
print(screen)
width, height = screen.width, screen.height
image = np.zeros((height, width, 3), dtype=np.float64)
image[:, :] = 0 # black screen
img = cv2.imread("newBen.png")
p = 0.25
w = int(img.shape[1])
h = int(img.shape[0])
new_img = cv2.resize(img, (w, h))
image[:h, :w] = new_img
window_name = 'projector'
cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
cv2.moveWindow(window_name, screen.x - 1, screen.y - 1)
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)
cv2.imshow(window_name, image)
cv2.waitKey()
cv2.destroyAllWindows()
But my image looking like this.
How can ı fix it?
The dtype of a normal RGB image is uint8, not float64.
image = np.zeros((height, width, 3), dtype=np.uint8)
Btw: You don't have to set image[:, :] = 0 # black screen. This is already been done by np.zeros.

How to change the set of pixel colors in contatc with black color

Considering this image:
I would like to change the set of white pixels in contact with black pixels by red, this way:
I tried to use this code in python:
import numpy as np
from PIL import Image
im = Image.open('image.png')
data = np.array(im)
r1, g1, b1 = 255, 255, 255 # Original value
r2, g2, b2 = 0, 0, 255 # Value that we want to replace it with
red, green, blue = data[:,:,0], data[:,:,1], data[:,:,2]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:3][mask] = [r2, g2, b2]
im = Image.fromarray(data)
But I changed all white pixels by red. But could be an UNIX approach suggestion too.
Please, post lossless versions of your input images. Lossy images modify the value of the pixels, creating artifacts that affect processing. I recreated your image and saved it as a lossless PNF file.
I'm using OpenCV to get the result you want. I created a mask with the non-zero elements of your original input. Then, I used Flood-fill to fill the outer shapes with the color you want. The final image can be obtained if you AND both images.
Let's see the code:
# import opencv:
import cv2
# image path
path = "D://opencvImages//"
fileName = "rectsLossless.png"
# Reading an image in default mode:
inputImage = cv2.imread(path + fileName)
# Grayscale image:
grayscaleImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
# Get non-zero mask:
binaryThresh = 1
_, binaryMask = cv2.threshold(grayscaleImage, binaryThresh, 255, cv2.THRESH_BINARY)
This bit creates the non-zero pixels mask:
This will help to zero all the elements that are non-white. That image is the first part of the mask. Now, let's fill the outer shapes with red color. This is achieved in three steps:
# Get image dimensions:
(imageHeight, imageWidth) = inputImage.shape[:2]
# Get image center:
xCenter = int(0.5 * imageWidth)
yCenter = int(0.5 * imageHeight)
# Get flood-fill target color
floodColor = inputImage[yCenter, xCenter]
print("Flood Color: %s" % floodColor)
# numpy array to tuple
floodColor = (int(floodColor[0]), int(floodColor[1]), int(floodColor[2]))
The first step gets the actual filling color. I suppose that the red is located more or less at the center of the image. Then, the second step involves filling all the "foreground" pixels with white. Let's seed at the top left corner:
# Flood fill at top left corner:
leftCorner = (1, 1)
whiteColor = (255, 255, 255)
cv2.floodFill(inputImage, None, leftCorner, whiteColor)
This is the result:
Note how the shapes that are partially outside of the red rectangle are all now connected by the white color. Let's fill again, but this time using the red color I extracted previously:
# Second Flood-fill
cv2.floodFill(inputImage, None, leftCorner, floodColor)
This yields the following image:
Let's create the final image by ANDing this result with the original non-zero mask:
# Create final image:
outImage = cv2.bitwise_and(inputImage, inputImage, mask=binaryMask)
This is the final result:
The question is very close to this question.
My solution is close too...
Assuming the colors are black white and red (the general case may be trickier), we may use the following stages:
Fill the black background with white color (using cv2.floodFill).
The white object on the red boundary are merged with the background.
Fill the white background with black color (using cv2.floodFill).
The white object on the red boundary are going to be black.
Copy the red color channel from the original image to "filled" image.
The red channel of a white pixel is 255, so black and white becomes red.
Code sample:
import cv2
import numpy as np
img = cv2.imread('red_white_black.jpg')
# Copy the original image to img2
img2 = img.copy()
# Fill the black background with white color
cv2.floodFill(img2, None, seedPoint=(0, 0), newVal=(255, 255, 255), loDiff=(50, 50, 50), upDiff=(50, 50, 50))
cv2.imshow('black background', img2) # Show img2 for testing
# Fill the white background with black color
cv2.floodFill(img2, None, seedPoint=(0, 0), newVal=(0, 0, 0), loDiff=(50, 50, 50), upDiff=(50, 50, 50))
cv2.imshow('white background', img2) # Show img2 for testing
# Copy the red color channel from the original image to img2
img2[:, :, 2] = img[:, :, 2]
cv2.imshow('img2', img2) # Show img2 for testing
cv2.waitKey()
cv2.destroyAllWindows()
Results:
Black background:
White background:
img2:
The black margins around the red, are because the original image is JPEG and not PNG (colors are not pure), and the red is not pure red.
We may fix it using the following code (the code in not very elegant)...
red = img[:, :, 2]
r = np.median(img[:, :, 2][red > 50])
g = np.median(img[:, :, 1][red > 50])
b = np.median(img[:, :, 0][red > 50])
mask = np.logical_and(img[:, :, 0] > 100, img2[:, :, 0] <= 100)
img3 = img2.copy()
img3[:, :, 2][mask] = r
img3[:, :, 1][mask] = g
img3[:, :, 0][mask] = b
img3[:, :, 2] = cv2.morphologyEx(img3[:, :, 2], cv2.MORPH_CLOSE, np.ones((3, 3), np.uint8))
img3[:, :, 1] = cv2.morphologyEx(img3[:, :, 1], cv2.MORPH_OPEN, np.ones((3, 3), np.uint8))
img3[:, :, 0] = cv2.morphologyEx(img3[:, :, 0], cv2.MORPH_OPEN, np.ones((3, 3), np.uint8))
cv2.imshow('img3', img3)
cv2.waitKey()
cv2.destroyAllWindows()
Result:

Problems with transparent overlapping image in openCv

I have issue where I have transparent part of image, but I am not sure if it really is transparent because I can not see what is behind transparent part and it is look like there is nothing but under it should be another image, problem looks like this or like this. If I do not use masks for deleting black edges it looks like this
I want to make edges completely transparent or entirely delete them.
Thank you for your help. Have a nice day
this is how I load image
trainImg = cv2.imread(fnames[x], cv2.IMREAD_UNCHANGED)
trainImg_gray = cv2.cvtColor(trainImg, cv2.COLOR_RGB2GRAY)
queryImg = cv2.imread(fnames[y], cv2.IMREAD_UNCHANGED)
queryImg_gray = cv2.cvtColor(queryImg, cv2.COLOR_RGB2GRAY)
this is how I work with pictures
height = trainImg.shape[0] + queryImg.shape[0]
width = trainImg.shape[1] + queryImg.shape[1]
dim = (trainImg.shape[1], trainImg.shape[0])
print(fnames_cropped[x])
trainImg = cv2.imread(fnames_cropped[x])
(b, g, r) = cv2.split(trainImg)
trainImg = cv2.merge([r, g, b])
trainImg = cv2.resize(trainImg, dim)
trainImg = trim(trainImg)
dim = (queryImg.shape[1], queryImg.shape[0])
print(fnames_cropped[y])
queryImg = cv2.imread(fnames_cropped[y])
(b, g, r) = cv2.split(queryImg)
queryImg = cv2.merge([r, g, b])
queryImg = cv2.resize(queryImg, dim)
queryImg = trim(queryImg)
# Make mask of black pixels - mask is True where image is black
RGB = np.array(trainImg)
h, w = RGB.shape[:2]
# Add an alpha channel, fully opaque (255)
RGBA = np.dstack((RGB, np.zeros((h, w), dtype=np.uint8) + 255))
# Make mask of black pixels - mask is True where image is black
mBlack = (RGBA[:, :, 0:3] == [0, 0, 0]).all(2)
# Make all pixels matched by mask into transparent ones
RGBA[mBlack] = (0, 0, 0, 0)
trainImg = RGBA
# Make mask of black pixels - mask is True where image is black
RGB = np.array(queryImg)
h, w = RGB.shape[:2]
# Add an alpha channel, fully opaque (255)
RGBA = np.dstack((RGB, np.zeros((h, w), dtype=np.uint8) + 255))
# Make mask of black pixels - mask is True where image is black
mBlack = (RGBA[:, :, 0:3] == [0, 0, 0]).all(2)
# Make all pixels matched by mask into transparent ones
RGBA[mBlack] = (0, 0, 0, 0)
queryImg = RGBA
result = cv2.warpPerspective(trainImg, H, (width, height), flags=cv2.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT)
im = Image.fromarray(trim(result))
result[0:queryImg.shape[0], 0:queryImg.shape[1]] = queryImg
im = Image.fromarray(trim(result))
im.save(feature_extractor+feature_matching+fnames_cropped[x]+fnames_cropped[y])

OpenCV overlay 2 image based on image mask

I need overlay 2 images based on third image mask
Example
1.-I have this background
2.-I have this object image and also i have de segmentation image
Object image
I'm try to merge Backgound and Object image based on third image (mask image)
(mask image)
The final result is Background image + Object image(only based on mask)
Any idea..
I tried
import cv2
added_image = cv2.addWeighted(back_img,0.4,aug_demoimage,0.1,0)
But not working as expected.. any sugestion? thanks!
Solved
def get_only_object(img, mask, back_img):
fg = cv2.bitwise_or(img, img, mask=mask)
#imshow(fg)
# invert mask
mask_inv = cv2.bitwise_not(mask)
#fg_back = cv2.bitwise_or(back_img, back_img, mask=mask)
fg_back_inv = cv2.bitwise_or(back_img, back_img, mask=mask_inv)
#imshow(fg_back_inv)
final = cv2.bitwise_or(fg, fg_back_inv)
#imshow(final)
return final
You need to convert the object image into an RGBA image where the alpha channel is the mask image you have created. Once you do this, you can paste it to the background image.
def convert_to_png(img, a):
#alpha and img must have the same dimenstons
fin_img = cv2.cvtColor(img, cv2.COLOR_RGB2RGBA)
b, g, r, alpha = cv2.split(fin_img)
alpha = a
# plt.imshow(alpha);plt.title('alpha image');plt.show()
# plt.imshow(img);plt.title('original image');plt.show()
# plt.imshow(alpha);plt.title('fin alpha image');plt.show()
fin_img[:,:, 0] = img[:,:,0]
fin_img[:,:, 1] = img[:,:,1]
fin_img[:,:, 2] = img[:,:,2]
fin_img[:,:, 3] = alpha[:,:]
# plt.imshow(fin_img);plt.title('fin image');plt.show()
return fin_img
This function will combine the two images into an RGBA image.
y1, y2 = new_loc[1], new_loc[1] + img.shape[0]
x1, x2 = new_loc[0], new_loc[0] + img.shape[1]
alpha_s = img[:, :, 3] / 255.0
alpha_l = 1.0 - alpha_s
for c in range(0, 3):
fin_img[y1:y2, x1:x2, c] = (alpha_s * img[:, :, c] +
alpha_l * img[y1:y2, x1:x2, c])
And this will copy the Object image to the background image

python: want to display red channel only in opencv

I am beginner in image processing. I am showing image in many color space the below code show the image in the 3 channels R G B however the image displayed in the gray layout. i need to display three images one with red channel as red image, another as blue, and the last one as green. thanks in advance.
# cspace.py
import cv2
import numpy as np
image = cv2.imread('download.jpg')
# Convert BGR to HSV
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hsl = cv2.cvtColor(image, cv2.COLOR_BGR2HLS) # equal to HSL
luv = cv2.cvtColor(image, cv2.COLOR_BGR2LUV)
#RGB - Blue
cv2.imshow('B-RGB.jpg',image[:, :, 0])
cv2.imwrite('B-RGB.jpg',image[:, :, 0])
# RGB - Green
cv2.imshow('G-RGB',image[:, :, 1])
cv2.imwrite('G-RGB.jpg',image[:, :, 1])
# RGB Red
cv2.imshow('R-RGB',image[:, :, 2])
cv2.imwrite('R-RGB.jpg',image[:, :, 2])
cv2.waitKey(0)
Blue image as displayed currently
i need to display blue channel like this image
You can just make a copy of the original image and set some channels to 0.
import cv2
image = cv2.imread('download.jpg')
b = image.copy()
# set green and red channels to 0
b[:, :, 1] = 0
b[:, :, 2] = 0
g = image.copy()
# set blue and red channels to 0
g[:, :, 0] = 0
g[:, :, 2] = 0
r = image.copy()
# set blue and green channels to 0
r[:, :, 0] = 0
r[:, :, 1] = 0
# RGB - Blue
cv2.imshow('B-RGB', b)
# RGB - Green
cv2.imshow('G-RGB', g)
# RGB - Red
cv2.imshow('R-RGB', r)
cv2.waitKey(0)
import cv2
import numpy as np
channel_initials = list('BGR')
image = cv2.imread('download.jpg')
for channel_index in range(3):
channel = np.zeros(shape=image.shape, dtype=np.uint8)
channel[:,:,channel_index] = image[:,:,channel_index]
cv2.imshow(f'{channel_initials[channel_index]}-RGB', channel)
cv2.waitKey(0)

Categories