python裁剪病理大图(500M)

 按照裁剪后的图像尺寸3000*3000:

可进行多尺度叠加训练

import cv2
import os

# Cutting the input image to h*w blocks
# heightCutNum = 3;
# widthCutNum = 3;


# The folder path of input and output
inPath = r"G:\\python\\nature\\output\\LUAD\\"
outPath = r"G:\\python\\nature\\output\\LUADcaiqie\\3000\\"


for f in os.listdir(inPath):

    path = inPath + f.strip()
    print(path)
    img = cv2.imread(path)    

    # The size of each input image
    height = img.shape[0]
    width = img.shape[1]
    
    # The size of block that you want to cut
    heightBlock = 3000
    widthBlock = 3000
    # heightBlock = int(height / heightCutNum)
    # widthBlock = int(width / widthCutNum)
    for i in range(0,int(height / heightBlock)):
        for j in range(0,int(width / widthBlock)):
            cutImage = img[i*heightBlock:(i+1)*heightBlock, j*widthBlock:(j+1)*widthBlock]
            savePath = outPath + f + str(i) + "_" + str(j) + ".png"
            cv2.imwrite(savePath,cutImage)
        

结果如下: 

按照长宽想裁剪的个数裁剪:


import cv2
import os

# Cutting the input image to h*w blocks
heightCutNum = 3;
widthCutNum = 3;


# The folder path of input and output
inPath = r"G:\\python\\nature\\output\\LUAD\\"
outPath = r"G:\\python\\nature\\output\\LUADcaiqie\\"


for f in os.listdir(inPath):

    path = inPath + f.strip()
    print(path)
    img = cv2.imread(path)    

    # The size of each input image
    height = img.shape[0]
    width = img.shape[1]
    
    # The size of block that you want to cut
    # heightBlock = 3000
    # widthBlock = 3000
    heightBlock = int(height / heightCutNum)
    widthBlock = int(width / widthCutNum)
    for i in range(0,heightCutNum):
        for j in range(0,widthCutNum):
            cutImage = img[i*heightBlock:(i+1)*heightBlock, j*widthBlock:(j+1)*widthBlock]
            savePath = outPath + f + str(i) + "_" + str(j) + ".jpg"
            cv2.imwrite(savePath,cutImage)
        

补充:

如果图片特别大,则cv库无法使用,选用pillow

 
import openslide
import numpy as np
# import cv2
import PIL.Image
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

def readsvs():
    svs_dir = './svs'
    # svs_path_list = []
    svs_name_list = []
    for i in os.listdir(svs_dir):
        path_name = os.path.join(i)
        # path_svs = os.path.join(svs_dir,i)
        svs_name_list.append(path_name)
    return svs_name_list # S1700028Y.svs


def main(svs_name):
    # test = openslide.open_slide('./svs/S1700028Y.svs')
    svs_dir = './svs/'
    test = openslide.open_slide(svs_dir + svs_name)
    img = np.array(test.read_region((0, 0), 0, test.dimensions))
    output_path = r'./outputpng'
    if not os.path.exists(output_path):
        os.mkdir(output_path)
    png_name = svs_name.split('.')[0]
    PIL.Image.fromarray(img).save('./outputpng/'+ png_name + '.png')
if __name__ == '__main__':
    svs_name_list = readsvs()
    for i in range(len(svs_name_list)):
    # for i in range(2):
        abc = main(svs_name_list[i])





裁剪并保存:

import cv2
import os
from PIL import Image
import numpy as np



# The folder path of input and output
inPath = r"./boliyangbian/"
outPath = r"./boliyangbian5000/"
 
 
for f in os.listdir(inPath):
 
    path = inPath + f.strip()
    # print(path)
    # img = cv2.imread(path)    
 
    Image.MAX_IMAGE_PIXELS = None
    mat = Image.open(path)
    img = np.array(mat)
    # print(img.shape)

    # The size of each input image
    height = img.shape[0]
    width = img.shape[1]
    
    # The size of block that you want to cut
    heightBlock = 5000
    widthBlock = 5000
    # heightBlock = int(height / heightCutNum)
    # widthBlock = int(width / widthCutNum)
    f2 = f.split('.')[0]
    # print(f2)
    for i in range(0,int(height / heightBlock)):
        for j in range(0,int(width / widthBlock)):
            cutImage = img[i*heightBlock:(i+1)*heightBlock, j*widthBlock:(j+1)*widthBlock]
            savePath = outPath + f2 + "_" + str(i*int(width / widthBlock)+(j+1)) + ".png"
            cv2.imwrite(savePath,cutImage)

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值