python 实现图像分块算法 spilt

python 实现图像分块算法 spilt

注意保存的格式问题, cv2.imwrite()保存的得到BRG转RGB,以及BRG转gray

import numpy as np
import matplotlib.pyplot as plt
import cv2
# https://blog.csdn.net/qq_39237205/article/details/124285223
# https://blog.csdn.net/u014636245/article/details/82930840

#---------------------------------------------
def divide_method3(img, m, n):
    h, w = img.shape[0], img.shape[1]
    # print("w,h",h, w)

    grid_h = int(h * 1.0 / (m - 1) + 0.5)  # 每个网格的高
    grid_w = int(w * 1.0 / (n - 1) + 0.5)  # 每个网格的宽

    # 满足整除关系时的高、宽
    h = grid_h * (m - 1)
    w = grid_w * (n - 1)

    gx, gy = np.meshgrid(np.linspace(0, w, n), np.linspace(0, h, m))
    gx = gx.astype(np.int_)
    gy = gy.astype(np.int_)



    last_val_x = [img.shape[0]] * n
    last_val_y = [img.shape[1]] * m

    gx[:, n - 1] = last_val_x
    gy[m - 1, :] = last_val_y

    print("gx:", gx)
    print("gy:", gy)

    # divide_image = ()
    # index = 0
    # for i in range(m - 1):
    #     for j in range(n - 1):
    #         divide_image[index] = img[gy[i][j]:gy[i + 1][j + 1], gx[i][j]:gx[i + 1][j + 1], :]
    #         index = index + 1

    divide_image = []
    for i in range(m - 1):
        for j in range(n - 1):
            # divide_image[i, j, ...] = img[
            # gy[i][j]:gy[i + 1][j + 1], gx[i][j]:gx[i + 1][j + 1], :]
            print("i------->j:",i,j)
            print("gy:", gy[i][j], gy[i + 1][j + 1])
            print("gx:", gx[i][j], gx[i + 1][j + 1])
            divide_image.append(img[gy[i][j]:gy[i + 1][j + 1], gx[i][j]:gx[i + 1][j + 1], :])

    return divide_image


def display_blocks3(divide_image):
    m = len(divide_image)
    for i in range(m):
        print("img:", divide_image[i].shape)
        # plt.imshow(divide_image[i])
        # plt.axis('off')
        # plotPath = str(title) + "+" + str(i) + str(j) + '.jpg'  # 图片保存路径
        # plotPath = str(i) + str(j) + '.jpg'  # 图片保存路径
        
 # plt.savefig("G:/spyder_workplace/" + plotPath)
        # plt.savefig("F:/rock_data/S5Mars_data/S5Mars_data/change/save/image/" + plotPath, divide_image[i])
        # plt.imsave("F:/rock_data/S5Mars_data/S5Mars_data/change/save/image/" + plotPath, divide_image[i])

# plotPath = str(i) + '.png'  # 图片标签是png ,注意转8位的灰度图
        # cv2.imwrite("F:/rock_data/S5Mars_data/S5Mars_data/change/save/label/" + plotPath,
        #             cv2.cvtColor(divide_image[i], cv2.COLOR_BGRA2GRAY))  # 利用cv2.imwrite()保存图像

        plotPath = str(i) + '.jpg'  # 图片保存路径 转为rgb
        cv2.imwrite("F:/rock_data/S5Mars_data/S5Mars_data/change/save/image/" + plotPath, cv2.cvtColor(divide_image[i],cv2.COLOR_BGR2RGB))  # 利用cv2.imwrite()保存图像


if __name__ == '__main__':
    plt.switch_backend('agg')
    img = cv2.imread('F:/rock_data/S5Mars_data/S5Mars_data/change/image/0025MR0001200090100730E01_DXXX.jpg')  # 图片地址
    # img = cv2.imread('F:/rock_data/S5Mars_data/S5Mars_data/change/label/0025MR0001200090100730E01_DXXX.png')
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    h, w = img.shape[0], img.shape[1]
    m = 5
    n = 5
    divide_image3 = divide_method3(img, m + 1, n + 1)  # 该函数中m+1和n+1表示网格点个数,m和n分别表示分块的块数
    fig3 = plt.figure('分块后的子图像:图像缩放法')
    display_blocks3(divide_image3)

你可能感兴趣的:(RockPro,python,算法,开发语言)