对分割图像中的图片分割

Posted 扬志九洲

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对分割图像中的图片分割相关的知识,希望对你有一定的参考价值。

import cv2
import os
import numpy as np
import math

def data_split(data_root, save_root, num_w, num_h):
	'''
	data_root:输入图片的路径
	save_root:输出图片的路径
	num_w:横向上的切割量
	num_h:纵向上的切割量
	'''
    for idx,data in enumerate(os.listdir(data_root)):
        data_n = os.path.join(data_root, data)
        img = cv2.imread(data_n)
        img_shape = img.shape
        height = img_shape[0]
        width = img_shape[1]
        img_width = math.floor(width/num_w)
        img_height = math.floor(height/num_h)
        img = np.array(img)
        num = 0
        img_file_list = []
        for w in range(num_w):
            for h in range(num_h):
                img_n = img[h*img_height:(h+1)*img_height, w*img_width:(w+1)*img_width, : ]
                cv2.imwrite(os.path.join(save_root, str(idx)+str(num)+".jpg"),img_n)
                img_file_list.append(os.path.join(save_root, str(idx)+str(num)+".jpg"))
                num += 1
    return img_file_list

def gt_split(data_root, save_root, num_w, num_h):
	'''
	data_root:输入图片的路径
	save_root:输出图片的路径
	num_w:横向上的切割量
	num_h:纵向上的切割量
	'''
    for idx,data in enumerate(os.listdir(data_root)):
        data_n = os.path.join(data_root, data)
        img = cv2.imread(data_n)
        img_shape = img.shape
        height = img_shape[0]
        width = img_shape[1]
        img_width = math.floor(width/num_w)
        img_height = math.floor(height/num_h)
        img = np.array(img)
        num = 0
        gt_file_list = []
        for w in range(num_w):
            for h in range(num_h):
                img_n = img[h*img_height:(h+1)*img_height, w*img_width:(w+1)*img_width, : ]
                cv2.imwrite(os.path.join(save_root, str(idx)+str(num)+".png"),img_n)
                gt_file_list.append(os.path.join(save_root, str(idx)+str(num)+".png"))
                num+=1
    return gt_file_list

def main():
    data_root = "UDD5\\images"
    data_save = "UDD\\images"
    label_root = "UDD5\\labels"
    label_save = "UDD\\labels"
    if not os.path.exists(data_save):
        os.makedirs(data_save)
    if not os.path.exists(label_save):
        os.makedirs(label_save)
    img_file_list = data_split(data_root, data_save, 8, 4)
    gt_file_list = gt_split(label_root, label_save, 8, 4)
    lists = list(zip(img_file_list, gt_file_list))
    print((lists))

if __name__ == '__main__':
    main()

以上是关于对分割图像中的图片分割的主要内容,如果未能解决你的问题,请参考以下文章

对分割图像中的图片分割

python+opencv 实现文字分割(横板-小票文字分割/竖版-古文文字分割)

python+opencv 实现文字分割(横板-小票文字分割/竖版-古文文字分割)

python+opencv 实现文字分割(横板-小票文字分割/竖版-古文文字分割)

python+opencv 实现文字分割(横板-小票文字分割/竖版-古文文字分割)

深度学习-图像分割