728x90
반응형
오늘은 특정 좌표에 모자이크나 블러를 생성하는걸 해보자.
import cv2
import numpy as np
def apply_gaussian_blur(image, ksize=(15, 15)):
"""Apply Gaussian blur to the image."""
return cv2.GaussianBlur(image, ksize, 0)
def apply_mosaic(image, x, y, w, h, block_size=10):
"""Apply mosaic effect to a specified region of the image."""
# Extract the region of interest (ROI)
roi = image[y:y+h, x:x+w]
# Resize the ROI to a smaller size using nearest-neighbor interpolation
small_roi = cv2.resize(roi, (w // block_size, h // block_size), interpolation=cv2.INTER_NEAREST)
# Resize the small ROI back to the original size
mosaic_roi = cv2.resize(small_roi, (w, h), interpolation=cv2.INTER_NEAREST)
# Replace the ROI in the original image with the mosaic effect
image[y:y+h, x:x+w] = mosaic_roi
return image
# Read the image
image = cv2.imread('/my_dir/example.png')
# Apply Gaussian blur to the entire image (optional)
blurred_image = apply_gaussian_blur(image, ksize=(15, 15))
# Define the coordinates and size of the mosaic region
x, y, w, h = 50, 50, 100, 100 # Example coordinates and size
# Apply mosaic effect to the specified region
mosaic_image = apply_mosaic(blurred_image, x, y, w, h, block_size=10)
"""block_size가 커지면 모자이크가 강해진다"""
# Save and display the result
cv2.imwrite('/my_dir/result_image.png', mosaic_image)
cv2.imshow('Mosaic Image', mosaic_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
간단하다. 이제 detection 모델 있으면 그걸로 모자이크 저렇게 생성하면 된다.
728x90
반응형
'OpenCV' 카테고리의 다른 글
[OpenCV] 이미지 처리 (비디오) (0) | 2024.08.18 |
---|---|
[OpenCV] 이미지 처리 (자동보정) (0) | 2024.08.17 |
[OpenCV] 이미지 처리 (합성2) (0) | 2024.08.17 |
[OpenCV] 이미지 처리 (합성1) (0) | 2024.08.17 |
[OpenCV] 이미지 처리 (crop) (0) | 2024.08.17 |