本文介绍了模糊图像的特定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张图片.像这样:
I have an image. Like this:
我检测到一个主题(在这种情况下为人)&它会像这样屏蔽图像:
I detect a subject(which is a person in this case) & it masks the image like this:
我希望主体的背景模糊.像这样:
I want the background of the subject to be blurrred. Like this:
下面是我尝试过的代码.以下代码只会模糊
Below is the code I have tried. the following code only blurs
import cv2
import numpy as np
from matplotlib import pyplot as plt
import os
path = 'selfies\\'
selfImgs = os.listdir(path)
for image in selfImgs:
img = cv2.imread(path+image)
img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
blur = cv2.blur(img,(10,10))
#canny = cv2.Canny(blur, 10, 30)
#plt.imshow(canny)
plt.imshow(blur)
j=cv2.cvtColor(blur, cv2.COLOR_BGR2RGB)
print(image)
cv2.imwrite('blurred\\'+image+".jpg",j)
有什么方法可以仅模糊图像的特定部分.
Is there any way by which I can blur only specific part/parts of the image.
该项目基于 https://github.com/matterport/Mask_RCNN
如果需要,我可以提供更多信息.
I can provide more information if required.
我在numpy中有一种方法:-
I have an approach in numpy :-
final_image = original * mask + blurred * (1-mask)
推荐答案
您可以使用np.where()
方法选择想要模糊值的像素,然后将其替换为:
You may use np.where()
method to select the pixels where you want blurred values and then replace them as:
import cv2
import numpy as np
img = cv2.imread("/home/user/Downloads/lena.png")
blurred_img = cv2.GaussianBlur(img, (21, 21), 0)
mask = np.zeros((512, 512, 3), dtype=np.uint8)
mask = cv2.circle(mask, (258, 258), 100, np.array([255, 255, 255]), -1)
out = np.where(mask==np.array([255, 255, 255]), img, blurred_img)
cv2.imwrite("./out.png", out)
这篇关于模糊图像的特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!