I have a picture like this and i want to remove the background by adding a layer on top the image just like how photoshop layout works.
Original Picture
Mask/Layer
Final desired output
I am trying to do this iwth opencv's addweighed function but i am not able to get the desired output
im_overlay = cv2.imread('%s/%s.png'%(_src,camera_name.split(".")[0]))
img = cv2.addWeighted(im, 1, im_overlay, 0.0, 0)
showImage(img)
The format of the mask is strange for OpenCv to read on my system (using opencv 3.4.2). I was able to read it with cv2.IMREAD_UNCHANGED but it show me that your mask has 4 channels (I was expecting only one channel). The code below produce opposite or what you would expect:
img = cv2.imread(r"C:\Users\...\Desktop\\W1kle.jpg")
mask = cv2.imread(r"C:\Users\...\Desktop\LZdyB.png",cv2.IMREAD_UNCHANGED)
mask = mask[:,:,3]
res = cv2.bitwise_and(img,img,mask=mask)
cv2.imshow("image",res)
cv2.waitKey(0)
Hope this helps you :)
Related
Python 3.10,
opencv-python==4.5.4.60
I'm having a hard time understanding the color format of screenshots taken by MSS.
Here is the portion of my screen that I am screenshotting (with correct/expected colors, taken manually):
Here is the screenshot code I'm working with:
with mss.mss() as sct:
monitor = rect
res = np.array(sct.grab(monitor))[:, :, :3] # remove alpha
When I use cv2.imsave() or cv2.imshow(), the screenshot looks as expected. If I attempt to create masks on this image based on [R, G, B] colors, it produces opposite results, implying that the default color space of MSS Screenshots is BGRA.
If I do the following:
res = cv2.cvtColor(res, cv2.COLOR_BGRA2RGB)
cv2.imshow("res", res)
cv2.waitKey(0)
It produces this:
However, then I am able to perform RGB masking properly.
I may have just answered my own question, but is it correct to say that MSS Screenshots are in BGRA format, and OpenCV expects BGR format - which would explain why RGB images have reversed colors when using cv2.imshow() and cv2.imsave()?
I am new to computer version, I am trying to remove the background of the image given and make it white background. I have tried most of the codes shared here but non is working on my image.
code_1
code_2
code_3
code_4
input:
desired output:
Chanda Steven wrote:
I am new to computer version, I am trying to remove the background of
the image given and make it white background.
If that is all you want to do, then the following is a simple way to do that in Python/OpenCV/Numpy.
(But your desired result looks like an inverted result. So I am not sure what you want.)
If making the background white is all you want. Then convert the input to gray. Copy the input and use Numpy to change the background to white where gray is close to black.
Input:
import cv2
import numpy as np
# read image
img = cv2.imread("a_blob.jpg")
# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# change black to white
result = img.copy()
result[gray<=2] = (255,255,255)
# write results to disk
cv2.imwrite("a_blob_white.jpg", result)
# show results
cv2.imshow("RESULT", result)
cv2.waitKey(0)
Result:
If you remove background you not have your desider output.
The object in the image have a different processing, similar negative, but isn't negative.
If it was negative you got this result :
From output image it's very difficult understand what operations have been carried out.
Does anyone know how I can get these results better?
Total Kills: 15,230,550
Kill Details: (recorded after 2019/10,/Z3]
993,151 331,129
1,330,450 33,265,533
5,031,168
This is what it returns however it is meant to be the same as the image posted below, I am new to python so are there any parameters that I can add to make it read the image better?
img = cv2.imread("kills.jpeg")
text = pytesseract.image_to_string(img)
print(text)
This is my code to read the image, Is there anything I can add to make it read better? Also, the black boxes are to cover images that were interfering with the reading. I would like to also say that I have added the 2 black boxes to see if the images behind them were causing the issue, but I still get the same issue.
The missing knowledge is page-segmentation-mode (psm). You need to use them, when you can't get the desired result.
If we look at your image, the only artifacts are the black columns. Other than that, the image looks like a binary image. Suitable for tesseract to recognize the characters and the digits.
Lets try reading the image by setting the psm to 6.
6 Assume a single uniform block of text.
print(pytesseract.image_to_string(img, config="--psm 6")
The result will be:
Total Kills: 75,230,550
Kill Details: (recorded after 2019/10/23)
993,161 331,129
1,380,450 33,265,533
5,031,168
Update
The second way to solve the problem is getting binary mask and applying OCR to the mask features.
Binary-mask
Features of the binary-mask
As we can see the result is slightly different from the input image. Now when we apply OCR result will be:
Total Kills: 75,230,550
Kill Details: (recorded after 2019/10/23)
993,161 331,129
1,380,450 33,265,533
5,031,168
Code:
import cv2
import numpy as np
import pytesseract
# Load the image
img = cv2.imread("LuKz3.jpg")
# Convert to hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Get the binary mask
msk = cv2.inRange(hsv, np.array([0, 0, 0]), np.array([179, 255, 154]))
# Extract
krn = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 3))
dlt = cv2.dilate(msk, krn, iterations=5)
res = 255 - cv2.bitwise_and(dlt, msk)
# OCR
txt = pytesseract.image_to_string(res, config="--psm 6")
print(txt)
# Display
cv2.imshow("res", res)
cv2.waitKey(0)
I have a python program that detects a rectangle from the the captured Video. Now I want to project another image into the detected square (just like in this video).
I have Tried using the warpPerspective and that does not seem to be working or maybe I'm using it in the wrong way.
my present output looks like this. I want my output to look like this
I tried to overlay images after using warpPerspective:
img = cv2.imread('cola.jpg')
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
background = cv2.imread('stadium.jpg')
background = cv2.cvtColor(background,cv2.COLOR_BGR2RGB)
rows,cols,ch = background.shape
pts1 = np.float32([[0,0],[974,0],[0,974],[974,974]]) # cola coords
pts2 = np.float32([[560,383],[940, 516],[5,527],[298,733]]) # stadium tile coords
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(img,M,(cols,rows))
overlay = cv2.add(background, dst)
[Output image
I used OpenCV documentation
I have an image which looks like this:
I want to convert the background to white and all the other pixels to black so that my image look likes this:
Let's say the original image is img and the above result is mask. When I try this to get the mask from the original image, things don't work as expected. I did this:
mask = np.ones_like(img)*255
mask[img > 0] = 0
Ideally I should get the expected result but this is what I am getting instead.
Also, I have another image which looks like this:
I want to paste the expected mask on this final sunset image. How can I do that using numpy/scipy/PIL/skimage?
Since we are looking to get anything that's not black in img to be set as zero in mask, just look for ANY along the three channels (last axis) and use that boolean array for masking into mask -
mask[(img>0).any(-1)] = 0
Output for given sample #1 -
To mix it with the sunset image img2 -
from scipy.misc import imresize
mask_resized = imresize(mask, size=img2.shape)
out = (mask_resized==255)*img2
Output -