使用 YOLO11 分割和高斯模糊創(chuàng)建人像效果
分割和高斯模糊后的圖像
本文通過(guò)結(jié)合最新的YOLO11實(shí)例分割模型和高斯模糊,為你的圖片應(yīng)用人像效果。我們將使用YOLO11將人物從背景中分割出來(lái),并對(duì)除了主體之外的所有內(nèi)容應(yīng)用模糊效果。
1. 安裝Ultralytics庫(kù)
首先創(chuàng)建并激活一個(gè)Python虛擬環(huán)境來(lái)管理依賴項(xiàng)。如果你不熟悉虛擬環(huán)境,請(qǐng)查看這個(gè)教程:
激活虛擬環(huán)境后,我們需要安裝ultralytics庫(kù),這將允許我們使用YOLO11實(shí)例分割模型。運(yùn)行以下命令在你的環(huán)境里安裝庫(kù):
pip install ultralytics
2. 下載測(cè)試圖片
接下來(lái),讓我們從Unsplash下載一張測(cè)試圖片進(jìn)行測(cè)試,你可以使用任何你選擇的圖片。我為我們的測(cè)試目的選擇了以下圖片:
在.py文件中,添加以下代碼來(lái)下載和加載圖片:
import urllib.request
import cv2
# Download the image
url, filename = ("https://images.unsplash.com/photo-1634646493821-9fca74f85f59?w=600&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mzk0fHx1bmJsdXJyZWQlMjBwb3J0YWl0fGVufDB8fDB8fHww", "scene.jpg")
urllib.request.urlretrieve(url, filename)
# Load the input image using OpenCV
image = cv2.imread(filename)
3. 生成分割掩碼
圖片加載后,下一步是創(chuàng)建一個(gè)分割掩碼,以識(shí)別圖片中的人物。有關(guān)使用YOLO11實(shí)例分割模型識(shí)別人物的更詳細(xì)教程,請(qǐng)查看這個(gè)教程:《YOLO11 實(shí)例分割模型做行人分割》
模型將檢測(cè)人物,我們將創(chuàng)建一個(gè)掩碼以將主體與背景隔離。我們將使用yolo11n-seg.pt模型,但你可以使用Ultralytics YOLO11文檔中的任何你喜歡的模型。以下是加載模型并生成掩碼的代碼:
import urllib.request
import cv2
from ultralytics import YOLO
import numpy as np
def segment_image(image, model):
# Predict with the model
results = model(filename) # predict on an image
# Create an empty mask for segmentation
segmentation_mask = np.zeros_like(image, dtype=np.uint8)
# Iterate over the results
for i, r in enumerate(results):
# Iterate through the detected masks
for j, mask in enumerate(r.masks.xy):
# Convert the class tensor to an integer
class_id = int(r.boxes.cls[j].item()) # Extract the class ID as an integer
# Check if the detected class corresponds to 'person' (class ID 0)
if class_id == 0:
# Convert mask coordinates to an integer format for drawing
mask = np.array(mask, dtype=np.int32)
# Fill the segmentation mask with color (e.g., white for people)
cv2.fillPoly(segmentation_mask, [mask], (255, 255, 255))
return segmentation_mask
# Download the image
url, filename = ("https://images.unsplash.com/photo-1634646493821-9fca74f85f59?w=600&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mzk0fHx1bmJsdXJyZWQlMjBwb3J0YWl0fGVufDB8fDB8fHww", "scene.jpg")
urllib.request.urlretrieve(url, filename)
# Load the input image using OpenCV
image = cv2.imread(filename)
# Load the model
model = YOLO("yolo11n-seg.pt") # load an official YOLO model
# Generate the segmentation mask
segmentation_mask = segment_image(image, model)
# Visualize the segmentation mask before combining it with the original image
cv2.imwrite("mask.jpg", segmentation_mask)
這一步將生成一個(gè)二進(jìn)制掩碼,其中人物被突出顯示,如下例所示:
圖像二進(jìn)制分割掩碼
4. 使用掩碼對(duì)圖像應(yīng)用高斯模糊
現(xiàn)在我們有了分割掩碼,我們可以在保持人物清晰的同時(shí)對(duì)背景應(yīng)用高斯模糊。我們將模糊整個(gè)圖像,然后使用掩碼將清晰的人物區(qū)域與模糊的背景結(jié)合起來(lái)。以下是分割和應(yīng)用模糊的所有代碼:
import urllib.request
import cv2
from ultralytics import YOLO
import numpy as np
def segment_image(image, model):
# Predict with the model
results = model(filename) # predict on an image
# Create an empty mask for segmentation
segmentation_mask = np.zeros_like(image, dtype=np.uint8)
# Iterate over the results
for i, r in enumerate(results):
# Iterate through the detected masks
for j, mask in enumerate(r.masks.xy):
# Convert the class tensor to an integer
class_id = int(r.boxes.cls[j].item()) # Extract the class ID as an integer
# Check if the detected class corresponds to 'person' (class ID 0)
if class_id == 0:
# Convert mask coordinates to an integer format for drawing
mask = np.array(mask, dtype=np.int32)
# Fill the segmentation mask with color (e.g., white for people)
cv2.fillPoly(segmentation_mask, [mask], (255, 255, 255))
return segmentation_mask
def apply_blur_using_mask(image, mask, blur_strength=(25, 25)):
# Apply Gaussian blur to the entire image
blurred_image = cv2.GaussianBlur(image, blur_strength, 0)
# Create an inverted mask where the background is white and the person is black
inverted_mask = cv2.bitwise_not(mask)
# Use the mask to keep the person sharp and blur the background
background_blur = cv2.bitwise_and(blurred_image, blurred_image, mask=inverted_mask[:, :, 0])
person_region = cv2.bitwise_and(image, image, mask=mask[:, :, 0])
# Combine the sharp person region with the blurred background
final_image = cv2.add(person_region, background_blur)
return final_image
# Download the image
url, filename = ("https://images.unsplash.com/photo-1634646493821-9fca74f85f59?w=600&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mzk0fHx1bmJsdXJyZWQlMjBwb3J0YWl0fGVufDB8fDB8fHww", "scene.jpg")
urllib.request.urlretrieve(url, filename)
# Load the input image using OpenCV
image = cv2.imread(filename)
# Load the model
model = YOLO("yolo11n-seg.pt") # load an official YOLO model
# Generate the segmentation mask
segmentation_mask = segment_image(image, model)
# Call the function to apply the blur and save the result
final_image = apply_blur_using_mask(image, segmentation_mask)
# Visualize the segmentation mask before combining it with the original image
cv2.imwrite("mask.jpg", segmentation_mask)
# Save the result
cv2.imwrite("blurred_image.jpg", final_image)
# Optionally display the image (make sure you're running in a GUI environment)
cv2.imshow("Blurred Image Result", final_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
最終結(jié)果
這段代碼將清晰的人物與模糊的背景結(jié)合起來(lái),為你的圖像提供專業(yè)的人像效果。分割掩碼確保人物保持聚焦,而背景則通過(guò)高斯模糊變?nèi)岷汀?/p>
示例結(jié)果
完整代碼:https://github.com/Brianhulela/background_blur