Untitled
plain_text
a month ago
1.7 kB
1
Indexable
Never
# %% -- make black border for images import cv2 import numpy as np # Read the image image = cv2.imread('/home/tin/projects/reasoning/scraping/allaboutbirds_data/id_data/Aberts_Towhee/bird_type_data/Adult_1.jpg') # Define patch size patch_size = 24 # Get image dimensions image = cv2.resize(image, (576, 576)) height, width = image.shape[:2] cv2.imwrite('resize_image.png', image) #%% # Calculate the number of patches in rows and columns num_rows = height // patch_size num_cols = width // patch_size # Initialize an empty canvas for the final image final_image = np.zeros((height + 2*24, width+2*24, 3), dtype=np.uint8) # Loop through each patch for row in range(num_rows): for col in range(num_cols): # Calculate patch boundaries y_start = row * patch_size y_end = y_start + patch_size x_start = col * patch_size x_end = x_start + patch_size # Extract the patch patch = image[y_start:y_end, x_start:x_end] # Add a black border to the patch bordered_patch = cv2.copyMakeBorder(patch, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=(0, 0, 0)) # Calculate position for the patch in the final image y_pos = row * (patch_size + 2) x_pos = col * (patch_size + 2) # Place the bordered patch on the final image print(y_pos, y_pos+patch_size+2) print(x_pos, x_pos+patch_size+2) # final_image[y_pos:y_pos+patch_size+2, x_pos:x_pos+patch_size+2] = bordered_patch final_image[y_pos:y_pos+patch_size+2, x_pos:x_pos+patch_size+2] = bordered_patch # Show and save the final image cv2.imwrite('final_image_with_borders.png', final_image)