• 正文
  • 相关推荐
申请入驻 产业图谱

使用真实噪声和YOLO模糊图像区域

3小时前
287
加入交流群
扫码加入
获取工程师必备礼包
参与热点资讯讨论

介绍

在当今以图像为主导的世界里,保护敏感信息至关重要。对于包含人脸、车牌或其他私人信息的图像尤其如此。虽然存在简单的模糊技术,但它们通常不够精细,可能会留下瑕疵。

这篇博文深入探讨了一种强大的解决方案:使用真实噪声和 YOLO 模糊图像区域。我们将探索如何使用 YOLO(特别是 YOLOv9,尽管 YOLOv8 也有效!)来针对特定区域进行模糊处理,同时结合逼真的噪声以获得自然的效果。最棒的是?您甚至可以使用精确的多边形形状来定义这些区域,从而最大程度地控制模糊的内容。

如何使用 YOLOv9 和真实噪声来模糊图像的部分内容

步骤 1:安装必要的库

pip install opencv-python ultralytics numpy

步骤 2:导入库

from ultralytics import YOLOimport cv2import numpy as np

步骤 3:定义函数来模糊和替换图像的部分内容

def blur_and_replace(image, polygon_vertices):    """    Blurs a polygon within an image and replaces original values with blurred ones.
    Args:        image: The original image as a NumPy array.        polygon_vertices: A list of vertices of the polygon as a 2D NumPy array with dtype=np.int32.        blur_kernel_size: Size of the Gaussian blurring kernel (must be odd).
    Returns:        The image with the blurred polygon replacing the original values.    """
    mask = np.zeros(image.shape, dtype=np.int32)
    channel_count = image.shape[2]
    # Ensure polygon_vertices is a 2D array with int32 dtype    polygon_vertices = np.array(polygon_vertices, dtype=np.int32)
    # Fill the polygon area in the mask    cv2.fillPoly(mask, [polygon_vertices], (255,) * channel_count)
    blurred_image = np.random.uniform(low=0, high=255, size=(mask.shape[0], mask.shape[1], 3)).astype(np.int32)
    out = np.where(mask == np.array([255, 255, 255]), blurred_image, image)
    return out.astype(np.uint8)

步骤 4:读取图像

img = cv2.imread("<YourImagePath>")

步骤 5:选择要用噪声替换的类别

results = model.predict(img, conf=0.2, classes=[0])

这里我选择了 0 类(代表人)进行模糊处理。当然,你也可以选择其他或多个类别。

    步骤 6:用噪声替换类别

for result in results:for mask, box inzip(result.masks.xy, result.boxes):        points = np.int32([mask])        img = blur_and_replace(img, points)

    步骤 7:保存并绘制结果

cv2.imwrite("<YourSavePath>", img)cv2.imshow("Image", img)cv2.waitKey(0)

完整代码:

import cv2import numpy as npfrom ultralytics import YOLO

defblur_and_replace(image, polygon_vertices):"""    Blurs a polygon within an image and replaces original values with blurred ones.
    Args:        image: The original image as a NumPy array.        polygon_vertices: A list of vertices of the polygon as a 2D NumPy array with dtype=np.int32.        blur_kernel_size: Size of the Gaussian blurring kernel (must be odd).
    Returns:        The image with the blurred polygon replacing the original values.    """
    mask = np.zeros(image.shape, dtype=np.int32)
    channel_count = image.shape[2]
# Ensure polygon_vertices is a 2D array with int32 dtype    polygon_vertices = np.array(polygon_vertices, dtype=np.int32)
# Fill the polygon area in the mask    cv2.fillPoly(mask, [polygon_vertices], (255,) * channel_count)
    blurred_image = np.random.uniform(low=0, high=255, size=(mask.shape[0], mask.shape[1], 3)).astype(np.int32)
    out = np.where(mask == np.array([255, 255, 255]), blurred_image, image)
return out.astype(np.uint8)

img = cv2.imread("YourImagePath")raw_img = img.copy()model = YOLO("yolov9e-seg.pt")conf = 0.2# if you just want to blur personsresults = model.predict(img, conf=conf, classes=[0])

for result in results:for mask, box in zip(result.masks.xy, result.boxes):        points = np.int32([mask])        img = blur_and_replace(img, points)
cv2.imwrite("YourSavePath", img)cv2.imshow("Image", result)cv2.waitKey(0)

相关推荐