为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 Opencv 模糊/羽化图像中对象的边缘?

如何使用 Opencv 模糊/羽化图像中对象的边缘?

翻翻过去那场雪 2021-11-30 16:42:09
目标是模糊图像中选定对象的边缘。我已经使用以下代码完成了获取对象轮廓的步骤:image = cv2.imread('path of image')gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY)[1]im, contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)我还可以使用以下方法绘制轮廓:cv2.drawContours(image, contours, -1, (0, 255, 0), 2)现在我想利用存储的点contours来模糊/羽化对象的边缘,也许使用高斯模糊。我怎样才能做到这一点?非常感谢!
查看完整描述

1 回答

?
呼啦一阵风

TA贡献1802条经验 获得超6个赞

与我在这里提到的类似,您可以按照以下步骤进行操作:


加载原始图像并找到轮廓。

模糊原始图像并将其保存在不同的变量中。

创建一个空的蒙版并在其上绘制检测到的轮廓。

使用 np.where() 方法从要模糊值的蒙版(轮廓)中选择像素,然后替换它。

import cv2

import numpy as np


image = cv2.imread('./asdf.jpg')

blurred_img = cv2.GaussianBlur(image, (21, 21), 0)

mask = np.zeros(image.shape, np.uint8)


gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY)[2]

contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)


cv2.drawContours(mask, contours, -1, (255,255,255),5)

output = np.where(mask==np.array([255, 255, 255]), blurred_img, image)

//img1.sycdn.imooc.com//61a5e40500017d6504680465.jpg

//img1.sycdn.imooc.com//61a5e40b0001832604930485.jpg

//img1.sycdn.imooc.com//61a5e414000179a204890466.jpg

查看完整回答
反对 回复 2021-11-30
  • 1 回答
  • 0 关注
  • 1596 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信