按照裁剪后的图像尺寸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)