From the below image I have created below mask with the following code:
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower_color, upper_color)
res = cv2.bitwise_and(img,img, mask= mask)
Image:
The mask:
I want to overlay res that looks like this:
I use this below code to overlay it.
bgra = cv2.addWeighted(bgra, 1, res, 1, 0)
in a loop that changes the color of the original image
for i in tqdm((range(rang)), ascii=True, desc = 'Loops'):
hmod += speed
alpha = img[:, :, 3]
bgr = img[:, :, 0:3]
hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
hnew = np.mod(h + hmod, 180).astype(np.uint8)
hsv_new = cv2.merge([hnew, s, v])
bgr_new = cv2.cvtColor(hsv_new, cv2.COLOR_HSV2BGR)
bgra = cv2.cvtColor(bgr_new, cv2.COLOR_BGR2BGRA)
bgra[:, :, 3] = alpha
if maskPresent[-1]:
bgra = cv2.addWeighted(bgra, 1, res, 1, 0)
if i == 0:
chromas = bgra
else:
try:
chromas = cv2.vconcat([bgra, chromas])
except MemoryError:
raise MemoryError('Out of memory(Try using an image with a lesser resolution or use a higher speed)')
but this also changes the color of the mask when the image is concatenated even though the res variable stays constant. Does anyone know a workaround?
chromares = cv2.bitwise_and(bgra, bgra, mask= mask)
bgra -= chromares
bgra += originalres #what was res```
Related
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])
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
I am trying to subtract the "hue" part of an image from that image.I have extracted all the h,s,v components .But i do not know what to do next?Is it even possible??
Here is my code
import cv2
def showimage(text,img):
cv2.imshow(text,cv2.resize(img, (700, 700)))
cv2.waitKey(0)
return 0
# Read image in BGR
img_path = "new.jpg"
img = cv2.imread(img_path)
showimage("orig",img)
# Convert BGR to HSV and parse HSV
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
showimage("hsv",hsv_img)
h, s, v = hsv_img[:, :, 0], hsv_img[:, :, 1], hsv_img[:, :, 2]
showimage("h",h)
showimage("s",s)
showimage("v",v)
sub=hsv_img-h
cv2.destroyAllWindows()
You cant subtract hsv_img and h as they are of not same dimensions. However, you can subtract a grayscale version of the image with h. For that, add the lines -
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
sub = gray_img-h
showimage("sub",sub)
However, if you are trying to visualize an image with zero hue component, you can do that with the code below -
Example:
import cv2
import numpy as np
def showimage(text,img):
cv2.imshow(text,cv2.resize(img, (500,500)))
cv2.waitKey(0)
return 0
# Read image in BGR
img_path = "new.jpg"
img = cv2.imread(img_path)
showimage("orig",img)
# Convert BGR to HSV and parse HSV
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
showimage("hsv",hsv_img)
h, s, v = hsv_img[:, :, 0], hsv_img[:, :, 1], hsv_img[:, :, 2]
showimage("h",h)
showimage("s",s)
showimage("v",v)
h = np.zeros_like(h)
img2 = cv2.merge((h,s,v))
img2 = cv2.cvtColor(img2, cv2.COLOR_HSV2BGR)
showimage("With h=0 ", img2)
cv2.destroyAllWindows()
This code is returning pixels coordinates which have red color now i want to extract and paste those pixels on new image. how do i paste pixel coordinates? Please ask if question is not clear.
import cv2
import numpy as np
filename = "oOHc6.png"
img = cv2.imread(filename, 1)
hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hsv_lower=np.uint8([0, 200, 210])
hsv_upper=np.uint8([180, 250, 250])
mask= cv2.inRange(hsv, hsv_lower, hsv_upper)
#display mask
res = cv2.bitwise_and(img,img,mask = mask)
res_gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
ys,xs = np.where(res_gray>0)
pts = [(x,y) for x,y in zip(xs,ys)]
empty = np.zeros_like(img)
mask_c = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
imaskc = mask_c>0
empty[imaskc] = img[imaskc]
#empty.save('C:/Python27/cclabel/images/NewImage'+'.png','png')
cv2.imwrite("new.png", empty)
I do cv2.inRange() in HSV-space to get the mask for the red region:
Then use the mask-operation(such as cv2.bitwise_and()/np.where()/ slices) to "paste" to another image.
To get the coords, you can also use the np.where() like that.
# 使用 cv2.bitwise_and 掩模操作,然后使用 np.where 获取坐标
res = cv2.bitwise_and(img,img,mask = mask)
res_gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
ys,xs = np.where(res_gray>0)
pts = [(x,y) for x,y in zip(xs,ys)]
To "copy-paste" into another same size image:
## 复制-粘贴到其他空白的地方
empty = np.zeros_like(img)
mask_c = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
imaskc = mask_c>0
empty[imaskc] = img[imaskc]
I'm working on stitching multiple images using OpenCV. It's starting to work but I have a problem with one thing. After cv2.warpPerspective image has a "soft" borders, which means that calculated mask is one pixel too big.
My code:
# apply a perspective warp to stitch the images
# together
result = cv2.warpPerspective(imageA, H,
(imageA.shape[1] + imageB.shape[1], imageA.shape[0]))
# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(result,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 0, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
resizedB = np.zeros((result.shape[0],result.shape[1],3), np.uint8)
resizedB[0:imageB.shape[0], 0:imageB.shape[1]] = imageB
difference = cv2.bitwise_or(resizedB,result, mask=mask_inv)
result = cv2.add(result,difference)
cv2.imwrite('result .jpg', result)
I had to use cv2.bitwise_or because adding both images using cv2.add makes it too bright which made an almost black line at the connection.
Do you have any idea how to fix this? Maybe there's a way to modify mask to make it 1 pixel smaller?
I have finally solved this problem by using combination of few logic operations. Solution is presented below:
h1,w1 = imageB.shape[:2]
h2,w2 = imageA.shape[:2]
pts1 = np.float32([[0,0],[0,h1],[w1,h1],[w1,0]]).reshape(-1,1,2)
pts2 = np.float32([[0,0],[0,h2],[w2,h2],[w2,0]]).reshape(-1,1,2)
pts2_ = cv2.perspectiveTransform(pts2, H)
pts = np.concatenate((pts1, pts2_), axis=0)
# print("pts:", pts)
[xmin, ymin] = np.int32(pts.min(axis=0).ravel() - 0.5)
[xmax, ymax] = np.int32(pts.max(axis=0).ravel() + 0.5)
t = [-xmin,-ymin]
Ht = np.array([[1,0,t[0]],[0,1,t[1]],[0,0,1]]) # translate
result = cv2.warpPerspective(imageA, Ht.dot(H), (xmax-xmin, ymax-ymin))
resizedB = np.zeros((result.shape[0], result.shape[1], 3), np.uint8)
resizedB[t[1]:t[1]+h1,t[0]:w1+t[0]] = imageB
# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(result,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 0, 255, cv2.THRESH_BINARY)
kernel = np.ones((5,5),np.uint8)
k1 = (kernel == 1).astype('uint8')
mask = cv2.erode(mask, k1, borderType=cv2.BORDER_CONSTANT)
mask_inv = cv2.bitwise_not(mask)
difference = cv2.bitwise_or(resizedB, resizedB, mask=mask_inv)
result2 = cv2.bitwise_and(result, result, mask=mask)
result = cv2.add(result2, difference)