[英]How to replace a masked part of an image with another image using openCV python?
The objective is to replace a part of an image with another image.目标是用另一个图像替换图像的一部分。 What I plan to do is to get the segment map of the original photo, and replace the selected part of the original with another image.
我打算做的是获取原始照片的片段图,并将原始照片的选定部分替换为另一张图像。
For example, This is a photo of the original photo's segment map:例如,这是原始照片的分段图的照片:
And I want to replace that pink segment mask with another guy model (different person):我想用另一个人模型(不同的人)替换那个粉红色段面具:
How should I do so about it?我该怎么做? I am thinking of selecting an ROI based on the pink colour (RGB Values of 192,128,128) and
bitwise_and
the two images?我正在考虑根据粉红色(RGB 值为 192,128,128)和
bitwise_and
两个图像来选择 ROI? But I'm not too sure on how to do that, or if that's the best approach.但我不太确定如何做到这一点,或者这是否是最好的方法。
I am aware that the pink mask and the sample guy are not the exact fit, but I just want to be able to fit the model in the pink segment first, and perhaps scale or transform it afterwards.我知道粉红色面具和样品人不是完全合适的,但我只是希望能够首先将模型拟合到粉红色部分,然后再缩放或变换它。
Any suggestions will be great!任何建议都会很棒! Thank you!
谢谢!
Load libs and read images加载库并读取图像
from __future__ import division
import cv2
import numpy as np
guy_img = cv2.imread('path')
target_img = cv2.imread('path')
Get bounding boxes for the guy (every pixel that is non-zero) and for the target mask (the pink region).获取男人(每个非零像素)和目标蒙版(粉红色区域)的边界框。
a = np.where(guy_img > 0)
b = np.where(target_img == 129) # picked one of the channels in your image
bbox_guy = np.min(a[0]), np.max(a[0]), np.min(a[1]), np.max(a[1])
bbox_mask = np.min(b[0]), np.max(b[0]), np.min(b[1]), np.max(b[1])
Note that when I loaded the target image in the values were different from the ones you provided.请注意,当我加载目标图像时,值与您提供的值不同。 There were also some white pixels along the edges of the image.
图像边缘也有一些白色像素。 That was likely just due to the image being uploaded to imgur and downloaded.
这可能只是因为图像被上传到 imgur 并被下载。 If your values are correct and the rest of your image is completely black except for the pink region you can just get all non zero pixels the same way I did for the guy image with
np.where(target_img > 0)
.如果您的值是正确的,并且除了粉红色区域之外,您的图像的其余部分是完全黑色的,您可以像我对带有
np.where(target_img > 0)
的家伙图像所做的相同方式获得所有非零像素。
Now get just the values of the guy and mask regions现在只获取 Guy 和 mask 区域的值
guy = guy_img[bbox_guy[0]:bbox_guy[1], bbox_guy[2]:bbox_guy[3],:]
target = target_img[bbox_mask[0]:bbox_mask[1], bbox_mask[2]:bbox_mask[3],:]
Resize the guy to be the same scale / shape as the mask将人调整为与面具相同的比例/形状
guy_h, guy_w, _ = guy.shape
mask_h, mask_w, _ = target.shape
fy = mask_h / guy_h
fx = mask_w / guy_w
scaled_guy = cv2.resize(guy, (0,0), fx=fx,fy=fy)
Reassign the masked region of the target image with the guy image values使用男人图像值重新分配目标图像的遮罩区域
for i, row in enumerate(range(bbox_mask[0], bbox_mask[1])):
for j, col in enumerate(range(bbox_mask[2], bbox_mask[3])):
target_img[row,col,:] = scaled_guy[i,j,:]
cv2.imshow('', target_img)
cv2.waitKey(0)
First you need to consider few things before you go ahead with the implementation.首先,在继续实施之前,您需要考虑几件事。
I would suggest you to first find the size, color and location of the ROI on the pink segmented image.我建议您首先在粉红色分割图像上找到 ROI 的大小、颜色和位置。 Scale the dimensions of the Object(Boy image) according to the mask image, so it gets easier to mask the object image to the pink part.
根据遮罩图像缩放对象(男孩图像)的尺寸,因此更容易将对象图像遮罩到粉红色部分。
You can then create an image (lets call is step one Image) with dimension similar to mask image.然后,您可以创建一个尺寸类似于蒙版图像的图像(我们称之为第一步图像)。 Add the scaled object image to it at location obtained from the pink blob analysis.
将缩放的对象图像添加到从粉红色斑点分析获得的位置。 This way you will have two images with similar dimension and similar scale.
这样,您将拥有两个具有相似尺寸和相似比例的图像。
With the step one image and Pink ROI should be very easily overlap-able visually.使用第一步图像和粉红色 ROI 应该很容易在视觉上重叠。
if you want exact pink then replace all the pixels on the step one image with the black pixel if the same location pixel is black on the masked image.如果您想要精确的粉红色,则如果蒙版图像上的相同位置像素为黑色,则将第一步图像上的所有像素替换为黑色像素。 Otherwise let it be what it is.
否则就让它成为它的样子。
if(mask[i][j] == black){ step_one[i][j] = black }
which leaves the pink part masked out of object image.这使得粉红色部分被对象图像掩盖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.