简体   繁体   English

如何使用openCV python用另一个图像替换图像的蒙版部分?

[英]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.首先,在继续实施之前,您需要考虑几件事。

  • Consistent background for mask image?蒙版图像的背景一致吗? consistent color of masked blob?蒙面斑点的颜色一致?
  • Size of the mask and the object to add image.蒙版的大小和要添加图像的对象。

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.

相关问题 使用 OpenCV 和 Python 将图像的分割部分替换为其他图像 - Using OpenCV and Python to replace a segmented part of an image with other images 如何在背景 OpenCV 和 Python 上调整和转换蒙版图像的大小 - How to resize and translate a masked image over a background OpenCV and Python 如何将图像的蒙版部分叠加到新图像上? - How to superimpose masked part of image to a new image? 遮罩图像的BGR值(OpenCV,Python) - BGR values of masked image (OpenCV, Python) 如何使用OpenCV将图像的一部分复制到另一个? - how to copy a part of image to another with OpenCV? 如何使用 OpenCV python 自动检测修剪和裁剪图像的一部分? - How to auto detect trim and crop part of image using OpenCV python? 如何使用opencv和python仅从图像中提取文本部分? - How to extract the text part only from an image using opencv and python? 创建一个圆形蒙版图像,然后将其放置在 OpenCV 中的另一个图像上 - Create a circular masked image then place it on another image in OpenCV 如何使用cv2将图像的一部分叠加/替换? - How to overlay/replace a part of an image with another using cv2? 通过OpenCV将图像的遮罩区域插入另一个(HDR) - Insert a masked region of an image in another (HDR) via OpenCV
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM