Python opencv 將一張圖片任意 N 等分並儲存_opencv 等分影像

钢之炼丹术师發表於2024-05-19

CSDN搬家失敗,手動匯出markdown後再匯入部落格園

import cv2
src = cv2.imread('100_0124_0013_pre_disaster.JPG', -1)
cnt = 1
num = 1
sub_images = []
sub_image_num = 4
src_height, src_width = src.shape[0], src.shape[1]
sub_height = src_height // sub_image_num
sub_width = src_width // sub_image_num
for j in range(sub_image_num):
    for i in range(sub_image_num):
        if j < sub_image_num - 1 and i < sub_image_num - 1:
            image_roi = src[j * sub_height: (j + 1) * sub_height, i * sub_width: (i + 1) * sub_width, :]
        elif j < sub_image_num - 1:
            image_roi = src[j * sub_height: (j + 1) * sub_height, i * sub_width:, :]
        elif i < sub_image_num - 1:
            image_roi = src[j * sub_height:, i * sub_width: (i + 1) * sub_width, :]
        else:
            image_roi = src[j * sub_height:, i * sub_width:, :]
        sub_images.append(image_roi)
for i, img in enumerate(sub_images):
    cv2.imwrite('sub_img_' + str(i) + '.png', img)

相關文章