gpt4 book ai didi

python - opencv python中的渐变蒙版混合

转载 作者:太空宇宙 更新时间:2023-11-03 21:17:21 44 4
gpt4 key购买 nike

我有一个图像和圆形区域。我需要模糊所有,除了圆形区域。我还需要使圆的边界平滑。
输入: Input

输出(在带掩码的图像编辑器中生成,但我认为 opencv 仅使用位图掩码):
Output

现在我有 python 代码,它不会模糊圆圈的边界。

def blur_image(cv_image, radius, center, gaussian_core, sigma_x):
blurred = cv.GaussianBlur(cv_image, gaussian_core, sigma_x)
h, w, d = cv_image.shape
# masks
circle_mask = np.ones((h, w), cv_image.dtype)
cv.circle(circle_mask, center, radius, (0, 0, 0), -1)
circle_not_mask = np.zeros((h, w), cv_image.dtype)
cv.circle(circle_not_mask, center, radius, (2, 2, 2), -1)
# Computing
blur_around = cv.bitwise_and(blurred, blurred, mask=circle_mask)
image_in_circle = cv.bitwise_and(cv_image, cv_image, mask=circle_not_mask)
res = cv.bitwise_or(blur_around, image_in_circle)
return res

当前版本:
enter image description here我怎样才能模糊圆圈的边界?在输出示例中,我在程序中使用了渐变蒙版。 opencv中有类似的东西吗?
更新 04.03
所以,我尝试了 this answered topic 中的公式以及我所拥有的:
Another try
代码:

def blend_with_mask_matrix(src1, src2, mask):
res = src2 * (1 - cv.divide(mask, 255.0)) + src1 * cv.divide(mask, 255.0)
return res

此代码的工作方式应该与最近的代码类似,但事实并非如此。圆圈中的图像略有不同。它有一些颜色问题。 问题仍然悬而未决。

最佳答案

我想也许你想要这样的东西。

这是源图像:

enter image description here

源模糊对: enter image description here

ma​​sk-alphablened-pair:

enter image description here


代码注释中有说明的代码。

#!/usr/bin/python3
# 2018.01.16 13:07:05 CST
# 2018.01.16 13:54:39 CST
import cv2
import numpy as np

def alphaBlend(img1, img2, mask):
""" alphaBlend img1 and img 2 (of CV_8UC3) with mask (CV_8UC1 or CV_8UC3)
"""
if mask.ndim==3 and mask.shape[-1] == 3:
alpha = mask/255.0
else:
alpha = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)/255.0
blended = cv2.convertScaleAbs(img1*(1-alpha) + img2*alpha)
return blended

img = cv2.imread("test.png")

H,W = img.shape[:2]
mask = np.zeros((H,W), np.uint8)
cv2.circle(mask, (325, 350), 40, (255,255,255), -1, cv2.LINE_AA)
mask = cv2.GaussianBlur(mask, (21,21),11 )

blured = cv2.GaussianBlur(img, (21,21), 11)

blended1 = alphaBlend(img, blured, mask)
blended2 = alphaBlend(img, blured, 255- mask)

cv2.imshow("blened1", blended1);
cv2.imshow("blened2", blended2);
cv2.waitKey();cv2.destroyAllWindows()

一些有用的链接:

  1. OpenCV C++ 中的 Alpha 混合:Combining 2 images with transparent mask in opencv

  2. OpenCV Python 中的 Alpha 混合: Gradient mask blending in opencv python

关于python - opencv python中的渐变蒙版混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42594993/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com