Untitled
user_2381398
plain_text
2 years ago
961 B
8
Indexable
import cv2
import numpy as np
import os
# Step 1: Load images
image_paths = os.listdir('path_to_your_dataset')
for image_path in image_paths:
image = cv2.imread(image_path)
# Step 2: Add noise to image
# Define the noise shape here
noise_shape = (100, 100)
# Create a black image with the same size as the original image
noise = np.zeros_like(image)
# Create a white square at the center of the image
cv2.rectangle(noise,
((image.shape[1] - noise_shape[1]) // 2, (image.shape[0] - noise_shape[0]) // 2),
((image.shape[1] + noise_shape[1]) // 2, (image.shape[0] + noise_shape[0]) // 2),
(255, 255, 255),
-1)
# Add the noise to the original image
noisy_image = cv2.add(image, noise)
# Step 3: Save the noisy image
cv2.imwrite('path_to_save_your_noisy_images/' + os.path.basename(image_path), noisy_image)
Editor is loading...