I can subtract 2 images by the following codes:
import cv2
original = cv2.imread('image1.png', cv2.IMREAD_UNCHANGED)
tiled = cv2.imread('image2.png', cv2.IMREAD_UNCHANGED)
subtract = cv2.subtract(tiled, original)
cv2.imwrite('subtract.png', subtract)
However, how can I obtain the area (maybe in form of array of pixels, or shapes) that results in black (i.e. after subtraction, the pixel is black color)?
I can only think of looping through each pixel of an image to check whether the pixel value equals to an array of zeros.
Ultimately, I want to change those pixels in black color after subtraction to be transparent.
Related
The aim is to take a coloured image, and change any pixels within a certain luminosity range to black. For example, if luminosity is the average of a pixel's RGB values, any pixel with a value under 50 is changed to black.
I’ve attempted to begin using PIL and converting to grayscale, but having trouble trying to find a solution that can identify luminosity value and use that info to manipulate a pixel map.
There are many ways to do this, but the simplest and probably fastest is with Numpy, which you should get accustomed to using with image processing in Python:
from PIL import Image
import numpy as np
# Load image and ensure RGB, not palette image
im = Image.open('start.png').convert('RGB')
# Make into Numpy array
na = np.array(im)
# Make all pixels of "na" where the mean of the R,G,B channels is less than 50 into black (0)
na[np.mean(na, axis=-1)<50] = 0
# Convert back to PIL Image to save or display
result = Image.fromarray(na)
result.show()
That turns this:
Into this:
Another slightly different way would be to convert the image to a more conventional greyscale, rather than averaging for the luminosity:
# Load image and ensure RGB
im = Image.open('start.png').convert('RGB')
# Calculate greyscale version
grey = im.convert('L')
# Point process over pixels to make mask of darker ones
mask = grey.point(lambda p: 255 if p<50 else 0)
# Paste black (i.e. 0) into image where mask indicates it is dark
im.paste(0, mask=mask)
Notice that the blue channel is given considerably less significance in the ITU-R 601-2 luma transform that PIL uses (see the lower 114 weighting for Blue versus 299 for Red and 587 for Green) in the formula:
L = R * 299/1000 + G * 587/1000 + B * 114/1000
so the blue shades are considered darker and become black.
Another way would be to make a greyscale and a mask as above. but then choose the darker pixel at each location when comparing the original and the mask:
from PIL import Image, ImageChops
im = Image.open('start.png').convert('RGB')
grey = im.convert('L')
mask = grey.point(lambda p: 0 if p<50 else 255)
res = ImageChops.darker(im, mask.convert('RGB'))
That gives the same result as above.
Another way, pure PIL and probably closest to what you actually asked, would be to derive a luminosity value by averaging the channels:
# Load image and ensure RGB
im = Image.open('start.png').convert('RGB')
# Calculate greyscale version by averaging R,G and B
grey = im.convert('L', matrix=(0.333, 0.333, 0.333, 0))
# Point process over pixels to make mask of darker ones
mask = grey.point(lambda p: 255 if p<50 else 0)
# Paste black (i.e. 0) into image where mask indicates it is dark
im.paste(0, mask=mask)
Another approach could be to split the image into its constituent RGB channels, evaluate a mathematical function over the channels and mask with the result:
from PIL import Image, ImageMath
# Load image and ensure RGB
im = Image.open('start.png').convert('RGB')
# Split into RGB channels
(R, G, B) = im.split()
# Evaluate mathematical function over channels
dark = ImageMath.eval('(((R+G+B)/3) <= 50) * 255', R=R, G=G, B=B)
# Paste black (i.e. 0) into image where mask indicates it is dark
im.paste(0, mask=dark)
I created a function that returns a list with True if the pixel has a luminosity of less than a parameter, and False if it doesn't. It includes an RGB or RGBA option (True or False)
def get_avg_lum(pic,avg=50,RGBA=False):
num=3
numd=4
if RGBA==False:
num=2
numd=3
li=[[[0]for y in range(0,pic.size[1])] for x in range(0,pic.size[0])]
for x in range(0,pic.size[0]):
for y in range(0,pic.size[1]):
if sum(pic.getpixel((x,y))[:num])/numd<avg:
li[x][y]=True
else:
li[x][y]=False
return(li)
a=get_avg_lum(im)
The pixels match in the list, so (0,10) on the image is [0][10] in the list.
Hopefully this helps. My module is for standard PIL objects.
I have 2 images - one of the images is binarized i.e. each pixel is either black or white, and the other image is a standard RGB image. Both images are of the same size. For all the white pixels in the first image, I would like to take the corresponding pixels in the RGB image and attach them in place of the white pixels. How can I do this in Python?
Use the binarized image as a binary index for the pixel coordinates:
import numpy as np
my_source_image = np.random.randint(0, 255, (480,640,3), np.uint8) # defined somewhere, assumed to have shape [H,W,3]
my_binarized_image = np.random.randint(0, 2, (480,640), np.uint8) # defined somewhere, assumed to have shape [H,W]
pixel_idx = my_binarized_image.astype(bool)
my_dest_image = np.zeros_like(my_source_image)
my_dest_image[pixel_idx,:] = my_source_image[pixel_idx]
Note that I define the destination image as all-zeroes (if you want a different background color, simply initialize to the color value instead) and then fill in the relevant pixels instead of reusing my_binarized_image as you see to require in your question, because the destination image needs three channels, while the binarized image only has one.
Alternatively, as long as the "background" color is black, you can avoid preallocating a second image and simply zero-out the pixels in the first image instead:
my_dest_image = my_source_image * pixel_idx[...,None]
(The [...,None] adds an extra dimension at the end of pixel_idx to make the pixel-wise multiplication possible)
From face image i get a skin-mask of image with cv2 as i find here
The result is an array of arrays (image) made up of pixels (RGB)
The problem is that in the result picture there are so many black pixels that do not belong to the skin.
I want to get 2d array with non-black pixels as [[218,195,182]. ... [229,0, 133]] -with only the pixels of facial skin color
I try to eject the black pixels by finding all the pixels whose all RGB is equal to 0 like [0,0,0] only:
Note that I do not want to extract zeros from pixels like: [255,0,125] [0,0,255] and so on.
def eject_black_color(skin):
list=[]
#loop over pixels of skin-image
for i in range(skin.shape[0]):
for j in range(skin.shape[1]):
if(not (skin[i][j][0]==0 and skin[i][j][1]==0 and skin[i][j][2]==0)):
#add only non-black pixels to list
list.append(skin[i][j])
return list
How to write it in a more efficient and fast way?
Thanks
if the values are zeros, they are essentially not contributing to your image in matrix multiplications. But if you want to show your image without black pixels, you can add an alpha mask and make those pixels transparent.
def make_transparent(img):
tmp = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_,alpha = cv2.threshold(tmp,0,255,cv2.THRESH_BINARY)
b, g, r = cv2.split(img)
rgba = [b,g,r, alpha]
dst = cv2.merge(rgba,4)
return dst
EDIT: if you want to find non-black pixels, you can use numpy's nonzero method.
desired_pixels = img[img.nonzero()]
I asked this question here in a different way and got a great answer.
Good luck!
I am using drawContours to make a mask for extracting ROI.
I have already defined four points and a zero mask for drawing the contour.
The output mask of the drawContours function is sort of a trapezoid shape which is what I want.
However, when I use this mask to do bitwise_and with the image,
the result isn't really the same shape with the mask.
The edge of the shape is obviously jagged.
Here is my python code snippet:
hull2 = cv2.convexHull(crop1)
mask10 = np.zeros(image.shape[:2], dtype = "uint8")
print(len(hull2))
cv2.drawContours(mask10, [hull2], -1,255, -1,cv2.LINE_AA)
cv2.imshow("mask10",mask10)
cv2.waitKey(0)
crop = cv2.bitwise_and(image, image, mask=mask10)
cv2.imshow("crop",crop)
cv2.waitKey(0)
cv2.drawContours(image, [hull2], -1, (0, 255, 0), -1,cv2.LINE_AA)
cv2.imshow("mask+img",image)
cv2.waitKey(0)
And here is a picture showing the result: "crop" is the ROI result image
Thanks for anyone trying to help.
The reason you are getting jagged edges while your mask looks like it has smooth edges is because you are using the anti-aliasing flag on drawContours( - ,cv2.LINE_AA) which fills in the surrounding of the jagged edges with darker pixels creating a gradient that fools your eye into thinking its a smooth edge.
Why does this matter? when you use bitwise_and with a mask, any value in the mask greater than 0 is evaluated as "True" and the corresponding pixel in the image will be selected.
So those extra AA pixels despite being a smaller gray value than 255, are expanding the edge of the mask, creating your jagged edge in crop. To emulate this, do mask10[mask10 > 0] = 255; cv2.imshow('mask10', mask10) and it should have the same shape as crop.
Now as a possible solution to your problem, you could use alpha blending to use the gradient (darkened intensity) of those extra AA pixels to darken the crop image edge pixels.
mask_float = cv2.cvtColor(mask10, cv2.COLOR_GRAY2BGR).astype('float32') / 255
image_float = image.astype('float32')
crop = cv2.multiply(image_float, mask_float).astype('uint8')
First we convert mask10 to a 3 channel array so that we can apply the alpha blending to all 3 BGR channels of the image.
Then we normalize the mask to a [0-1] range as we will need to multiply the values in the next step and dtype uint8 doesnt allow greater than 255. So first converting to float32 then dividing by 255. (we could potentially use cv2.normalize() but numpy should be alot faster)
we then convert the image to float32 to allow for multiplication with the mask.
then we multiply the image with the mask to get an alpha blended image of foreground to a black background and convert it back to uint8 for opencv.
Now since the BGR values are converted from float32 to uint8, it will discard the decimal values which will cause a negligible change in color. Also, I'm not 100% sure but there might be a small change in color due to multiplying each channel individually by the same value (eg: 20%) or it could be fine and im just overthinking it? But that only applies to those darkened AA pixels, the effect should also be negligible and we are already modifying it from the original anyways so it should be fine!
As an alternative, you could also convert the image to HLS and multiply the mask to the L-channel only. I believe that should be more true to the image's colors on those edges if that is very important, and the slower speed is permissible
I have two grayscale images of the same size, one of them is this one:
I'm trying to add a background to this image, which is to just change the white pixels to the respective pixels in the other picture. The best result I've managed to do is just a bitwise and of all the pixels of both pictures but the resultant picture is distorted inside James Bond. I also tried a weighted add between the two pictures but when I increase the weight of the James Bond image, it's white pixels are visible in the resultant image.
To combine with a second image, ensure that both images have the same dimensions (which yours do). They can then be combined
import cv2
img_jb = cv2.imread('james_bond.png')
img_007 = cv2.imread('007_logo.png')
height, width, channels = img_jb.shape
img_007_resized = cv2.resize(img_007, (width, height), interpolation=cv2.INTER_CUBIC)
threshold = img_jb > 240
img_jb[threshold] = img_007_resized[threshold]
cv2.imwrite('james_bond_logo.png', img_jb)
Giving you:
numpy allows you to work on the indexes of an array that match a given criteria. This has the effect of copying pixels from the background image into the foreground image where the foreground image has a value above 240.