I have this code
But I'm having difficulty in labeling the patches of each image in a certain pattern for instance i have a image with name 000_train now its patches should be named as 000_train_00................000_train_01 and so on or for 671_train its patch should be 671_train_00..............671_train_01 and so on
so I have 671 images, So each image patches should have name that uniquely identifies its original image but as well as automatically store the each image cropped images in separate folders respectively with name 0,1..........
import numpy as np
import cv2
import glob
from os import listdir
from os.path import isfile, join
from PIL import Image
import os
#look_up_table={"Ab":"0","An":"1","Di":"2","He":"3","Is":"4","Ky":"5","Kyr":"6","Me":"7","Pi":"8","Vi":"9"}
path = r"C:/Users/55/.spyder-py3/IAM/Train/"
save_path = "C:/Users/55/.spyder-py3/IAM/train_patches/"
# path = r"C:/Users/55/.spyder-py3/IAM/Test/"
# save_path = "C:/Users/55/.spyder-py3/IAM/test_patches/"
image_files = [f for f in os.listdir(path) if f.endswith('.png')]
def load_labels(path):
labels=[]
fileList = glob.glob(os.path.join(path,"*.png"))
for fname in fileList:
fileName=os.path.basename(fname)
curLabel=fileName
labels.append(curLabel)
return np.asarray(labels)
def load_data(path):
fileList=glob.glob(path)
x=np.array([np.array(Image.open(fname)).flatten() for fname in fileList])
x=x/255 #img size grey scale
return x
def imcrop(img, bbox):
x1,y1,x2,y2 = bbox
if x1 < 0 or y1 < 0 or x2 > img.shape[1] or y2 > img.shape[0]:
img, x1, x2, y1, y2 = pad_img_to_fit_bbox(img, x1, x2, y1, y2)
return img[y1:y2, x1:x2, :]
def pad_img_to_fit_bbox(img, x1, x2, y1, y2):
img = np.pad(img, ((np.abs(np.minimum(0, y1)), np.maximum(y2 - img.shape[0], 0)),
(np.abs(np.minimum(0, x1)), np.maximum(x2 - img.shape[1], 0)), (0,0)), mode="constant")
y1 += np.abs(np.minimum(0, y1))
y2 += np.abs(np.minimum(0, y1))
x1 += np.abs(np.minimum(0, x1))
x2 += np.abs(np.minimum(0, x1))
return img, x1, x2, y1, y2
for file in image_files:
w=0
sample_image = cv2.imread(path+str(file))
# get dimensions of image
dimensions = sample_image.shape
# height, width, number of channels in image
height = sample_image.shape[0]
width = sample_image.shape[1]
channels = sample_image.shape[2]
print('Image Dimension : ',dimensions)
print('Image Height : ',height)
print('Image Width : ',width)
print('Number of Channels : ',channels)
window_width=500
window_height=500
writer = file.split("fileName")[0]
#writer = load_labels(path)
for i in range(0,height,window_height):
for j in range(0,width,window_width):
# for temp in writer:
# if crop_image==window_width && window_height
crop_image = sample_image[i:i+window_height,j:j+window_width]
cv2.imwrite(save_path+str(writer)+"_"+str(w)+".png",crop_image)
w=w+1
#else
#