def crop_images(path_images, path_output, dimensions, centre=True):
"""
Batch crop images from top left hand corner to dimensions specified. Skips
images where dimensions are incompatible.
"""
print 'cropping images...'
for i, filename in enumerate(os.listdir(path_images)):
try:
image = io.imread('{}{}'.format(path_images, filename))
cropped = crop_image(image, dimensions, centre=centre)
io.imsave(
fname='{}{}'.format(path_output, filename),
arr=cropped
)
print '{}: {}'.format(i, filename)
except IndexError:
print '{}: {} failed - dimensions incompatible'.format(i, filename)
print 'all images cropped and saved.'
python类imread()的实例源码
def batch_generator(batch_size, nb_batches):
batch_count = 0
while True:
pos = batch_count * batch_size
batch = dataset[pos:pos+batch_size]
X = np.zeros((batch_size, 1, img_size, img_size), dtype=np.float32)
for k, path in enumerate(batch):
im = io.imread(path)
im = color.rgb2gray(im)
X[k] = im[np.newaxis, ...]
X = torch.from_numpy(X)
X = Variable(X)
yield X, batch
batch_count += 1
if batch_count > nb_batches:
batch_count = 0
create_test_cropped_image.py 文件源码
项目:kaggle-right-whale
作者: felixlaumon
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def get_cropped_test_img(fname, bbox_pred, pad=None, as_grey=False, return_bbox=False):
img = imread(fname, as_grey=as_grey)
h = img.shape[0]
w = img.shape[1]
bbox_pred = bbox_pred * [w, h, w, h]
bbox_pred = np.round(bbox_pred).astype(int)
l = min(max(bbox_pred[0], 0), w)
t = min(max(bbox_pred[1], 0), h)
r = min(max(l + bbox_pred[2], 0), w)
b = min(max(t + bbox_pred[3], 0), h)
if pad is not None:
l, t, r, b = add_padding_to_bbox(
l, t, (r - l), (b - t), pad / 100.0,
img.shape[1], img.shape[0],
format='ltrb'
)
cropped_img = img[t:b, l:r]
if return_bbox:
return cropped_img, bbox_pred
else:
return cropped_img
create_test_head_crop_image.py 文件源码
项目:kaggle-right-whale
作者: felixlaumon
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def get_cropped_test_img(fname, bbox_pred, pad=None, as_grey=False, return_bbox=False):
img = imread(fname, as_grey=as_grey)
h = img.shape[0]
w = img.shape[1]
bbox_pred = bbox_pred * [w, h, w, h]
bbox_pred = np.round(bbox_pred).astype(int)
l = min(max(bbox_pred[0], 0), w)
t = min(max(bbox_pred[1], 0), h)
r = min(max(l + bbox_pred[2], 0), w)
b = min(max(t + bbox_pred[3], 0), h)
if pad is not None:
l, t, r, b = add_padding_to_bbox(
l, t, (r - l), (b - t), pad / 100.0,
img.shape[1], img.shape[0],
format='ltrb'
)
cropped_img = img[t:b, l:r]
if return_bbox:
return cropped_img, bbox_pred
else:
return cropped_img
def load_image(self, path):
self.data = imread(path)
self.path = path
self._load_image_mask()
self._mask_out_common_obstructions()
self._rectify_image()
self.driving_layers = driving.process_strip(channels_first(self.rectified * 255))
self.facade_layers = i12.process_strip(channels_first(self.rectified * 255))
self._create_sky_mask()
self._segment_windows()
self._segment_facade_edges()
facade_cuts = self._split_at_facade_edges()
facade_mask = self._create_facade_mask()
wall_colors = self._mask_out_wall_colors(facade_mask)
self.wall_colors = wall_colors
self.facade_candidates = self._find_facade_candidates(wall_colors, facade_cuts)
def get_data_from_image(image_path):
# from osgeo import gdal
from skimage import io
if image_path.split(".")[1] == "tif":
M = io.imread(image_path)
# dataset = gdal.Open(image_path,gdal.GA_ReadOnly)
# col = dataset.RasterXSize
# row = dataset.RasterYSize
# a = [[[]for y in xrange(col)] for z in xrange(row)]
# for i in xrange(1,dataset.RasterCount + 1):
# band = dataset.GetRasterBand(i).ReadAsArray()
# for m in xrange(0,row):
# for n in xrange(0,col):
# a[m][n].append(band[m][n])
# M = np.array(a,dtype='uint16')
else:
M = np.asarray(Image.open(image_path))
return M
def predict(url):
global model
# Read image
image = io.imread(url)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
image = cv2.resize(image, (500, 500), interpolation=cv2.INTER_CUBIC)
# Use otsu to mask
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
mask = cv2.medianBlur(mask, 5)
features = describe(image, mask)
state = le.inverse_transform(model.predict([features]))[0]
return {'type': state}
def convert2lmdb(path_src, src_imgs, ext, path_dst, class_ids, preprocess_mode, im_sz, data_mode):
if os.path.isdir(path_dst):
print('DB ' + path_dst + ' already exists.\n'
'Skip creating ' + path_dst + '.', file=sys.stderr)
return None
if data_mode == 'label':
lut = create_lut(class_ids)
db = lmdb.open(path_dst, map_size=int(1e12))
with db.begin(write=True) as in_txn:
for idx, img_name in enumerate(src_imgs):
#img = imread(os.path.join(path_src + img_name)+ext)
img = np.array(Image.open(os.path.join(path_src + img_name)+ext))
img = img.astype(np.uint8)
if data_mode == 'label':
img = preprocess_label(img, lut, preprocess_mode, im_sz)
elif data_mode == 'image':
img = preprocess_image(img, preprocess_mode, im_sz)
img_dat = caffe.io.array_to_datum(img)
in_txn.put('{:0>10d}'.format(idx), img_dat.SerializeToString())
def LoadData(FP = '.'):
'''
Loads the OCR dataset. A is matrix of images (NIMG, Height, Width, Channel).
Y is matrix of characters (NIMG, MAX_CHAR)
FP: Path to OCR data folder
return: Data Matrix, Target Matrix, Target Strings
'''
TFP = os.path.join(FP, 'Train.csv')
A, Y, T, FN = [], [], [], []
with open(TFP) as F:
for Li in F:
FNi, Yi = Li.strip().split(',') #filename,string
T.append(Yi)
A.append(imread(os.path.join(FP, 'Out', FNi)))
Y.append(list(Yi) + [' '] * (MAX_CHAR - len(Yi))) #Pad strings with spaces
FN.append(FNi)
return np.stack(A), np.stack(Y), np.stack(T), np.stack(FN)
def pil_to_skimage(img):
"""
Convert PIL image to a Skimage image
:param img: PIL image object
:return: Skimage image object
"""
# Get the absolute path of the working directory
abspath = os.path.dirname(__file__)
# Create a temp file to store the image
temp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False, dir=abspath)
# Save the image into the temp file
img.save(temp.name, 'JPEG')
# Read the image as a SciKit image object
ski_img = io.imread(temp.name, plugin='pil')
# Close the file
temp.close()
# Delete the file
os.remove(temp.name)
return ski_img
def flowList(xFileNames, yFileNames):
'''
(x/y)fileNames: List of the fileNames in order to get the flows from
'''
frameList = []
if (len(xFileNames) != len(yFileNames)):
print 'XFILE!=YFILE ERROR: In', xFileNames[0]
for i in range(0, min(len(xFileNames), len(yFileNames))):
imgX = io.imread(xFileNames[i])
imgY = io.imread(yFileNames[i])
frameList.append(np.dstack((imgX, imgY)))
frameList = np.array(frameList)
return frameList
def plot_masks(cropped_image_path, prediction_map, output_image_path):
fig = plt.figure("segments")
ax = fig.add_subplot(1, 1, 1)
image_draw = io.imread(cropped_image_path)
segparams = SegParams()
feature_mapping = segparams.feature_palette()
classes = segparams.feature_classes()
legend_patches = []
for i in feature_mapping.keys():
if i in prediction_map:
temp_inds = np.where(prediction_map != i)
temp_map = prediction_map.copy()
temp_map[temp_inds] = 0
image_draw = mark_boundaries(
image_draw, temp_map, mode='inner', color=feature_mapping[i]) # outline_color=feature_mapping[i])
legend_patches.append(mpatches.Patch(
color=(feature_mapping[i][0], feature_mapping[i][1], feature_mapping[i][2], 1), label=classes[i]))
ax.imshow(image_draw)
lgd = ax.legend(handles=legend_patches,
loc="upper left", bbox_to_anchor=(1, 1))
plt.axis("off")
plt.savefig(output_image_path.strip('.jpg') + '_segmented.png', bbox_extra_artists=(
lgd,), bbox_inches='tight')
plt.show()
def loadImages(datadir, maxDirectoryCount=10, split=0.9):
for dirPath, dirNames, fileNames in os.walk(datadir):
fileNames = [f for f in fileNames if not f[0] == '.']
dirNames[:] = [d for d in dirNames if not d[0] == '.']
if (maxDirectoryCount != 0):
fullSizeFileNames = [fileName for fileName in fileNames if fileName.endswith("@2x.png") and (fileName.replace("@2x","") in fileNames)]
for fullSizeFileName in fullSizeFileNames:
inputImage = io.imread(dirPath + "/" + fullSizeFileName)
targetImage = io.imread(dirPath + "/" + fullSizeFileName.replace("@2x",""))
# print(dirPath + "/" + fullSizeFileName)
inputSlices, targetSlices = sliceImages(inputImage, targetImage)
# print("got", len(inputSlices), "input splices and",len(targetSlices),"targetSlices")
inputImages.extend(inputSlices)
targetImages.extend(targetSlices)
maxDirectoryCount -= 1
x, y = np.asarray(inputImages), np.asarray(targetImages)
x_train = x[:int(len(x) * split)]
y_train = y[:int(len(y) * split)]
x_test = x[int(len(x) * split):]
y_test = y[int(len(y) * split):]
# Shuffle training data so that repeats aren't in the same batch
# x_train, y_train = shuffle(x_train, y_train, random_state=0)
return (x_train, y_train, x_test, y_test)
def load_img(self, img_filename):
"""
Summary:
Load image from the filename. Default is to load in color if
possible.
Args:
img_name (string): string of the image name, relative to
the image directory.
Returns:
np array of float32: an image as a numpy array of float32
"""
if not img_filename.endswith('.jpg'):
img_filename = os.path.join(self.img_dir, img_filename + '.jpg')
else:
img_filename = os.path.join(self.img_dir, img_filename)
img = skimage.img_as_float(io.imread(
img_filename)).astype(np.float32)
if img.ndim == 2:
img = img[:, :, np.newaxis]
elif img.shape[2] == 4:
img = img[:, :, :3]
return img
def findRemSmObjValue(_biImageFile="E:/workspace/jinyoung/CancerImageAnalyzer/img/ppimg1601101508/thimg1601101511/BF_position020100_time0001hVal0.4thVal0.9.png"):
_remSmObjOutputPath = '/remSmObjImg'+datetime.datetime.today().strftime('%y%m%d%H%M')+'/'
remSmObjImageFileName = os.path.basename(_biImageFile)
biImageFilePath = os.path.dirname(_biImageFile)
reSmObjImageFilePath = biImageFilePath + _remSmObjOutputPath
biImg = imread(_biImageFile)
biImgRsize = biImg.shape[0] * 0.1
biImgCsize = biImg.shape[1] * 0.1
biImg = biImg[biImgRsize:-biImgRsize, biImgCsize:-biImgCsize]
biImg = ndimage.binary_fill_holes(biImg)
for smObjVal in np.arange(0,100000,10000):
filledImg = cia.removeSmallObject(biImg, minSize=smObjVal)
if not os.path.exists(reSmObjImageFilePath):
os.mkdir(reSmObjImageFilePath)
biImageFileName = remSmObjImageFileName[:remSmObjImageFileName.rfind('.')]+'smObjVal'+str(smObjVal)+'.png'
imsave(reSmObjImageFilePath+biImageFileName, filledImg)
#findHvalue()
def load_image(self, idx):
"""
Load input image and preprocess for Caffe:
- resize image
- cast to float
- switch channels RGB -> BGR
- subtract mean
- transpose to channel x height x width order
"""
idx=idx.split()[0]
try:
im = Image.open('{}/{}'.format(self.data_dir, idx))
except:
from skimage import io
im = io.imread('{}/{}'.format(self.data_dir, idx))
im = Image.fromarray(im)
im=im.resize((self.width, self.height), Image.ANTIALIAS) # resize image
im = np.array(im, dtype=np.float32) # cast to float
im = im[:,:,::-1] # RGB -> BGR
im -= self.mean # mean subtraction
# bring colour to the innermost dimension
im = im.transpose((2,0,1))
return im
def load_image(self, idx):
"""
Load input image and preprocess:
- resize
- cast to float
- switch channels RGB -> BGR
- subtract mean
- transpose to channel x height x width order
"""
idx = self.indices[idx]
idx = idx.split()[0]
im = io.imread('{}/{}'.format(self.data_dir, idx))
im = Image.fromarray(im)
im = im.resize((self.width, self.height), Image.ANTIALIAS) # resize image
im = np.array(im, dtype=np.float32) # cast to float
im = im[:,:,::-1] # RGB -> BGR
im -= self.mean_bgr # mean subtraction
im = im.transpose((2,0,1))
return im
def load_label(self, idx):
"""
Load binary mask and preprocess:
- resize
- convert to greyscale
- cast to integer
- binarize
"""
idx = self.indices_label[idx]
idx=idx.split()[0]
im = io.imread('{}/{}'.format(self.data_dir, idx))
im = Image.fromarray(im)
im=im.resize((self.width, self.height), Image.NEAREST) # resize
im=im.convert('L') # convert to greyscale
im=np.array(im, dtype=(np.int32)) # cast to integer
label=im
label[label>0]=1 # convert to binary
label=np.array(label,np.uint8)
label = label[np.newaxis, ...]
return label
def load_depth(self, idx):
"""
Load depth map and preprocess:
- resize
- cast to float
- subtract mean
"""
idx = self.indices_depth[idx]
idx=idx.split()[0]
im = io.imread('{}/depth/{}'.format(self.data_dir, idx))
im = Image.fromarray(im)
im = im.resize((self.width, self.height), Image.ANTIALIAS) # resize
im = np.array(im, dtype=np.float32) # cast to float
d = im
d -= self.mean_depth # mean subtraction
d = d[np.newaxis, ...]
return d
def load_image(self, idx):
"""
Load input image and preprocess for Caffe:
- resize image
- cast to float
- switch channels RGB -> BGR
- subtract mean
- transpose to channel x height x width order
"""
idx=idx.split()[0]
try:
im = Image.open('{}/{}'.format(self.data_dir, idx))
except:
from skimage import io
im = io.imread('{}/{}'.format(self.data_dir, idx))
im = Image.fromarray(im)
im=im.resize((self.width, self.height), Image.ANTIALIAS) # resize image
im = np.array(im, dtype=np.float32) # cast to float
im = im[:,:,::-1] # RGB -> BGR
im -= self.mean # mean subtraction
# bring colour to the innermost dimension
im = im.transpose((2,0,1))
return im
def load_image(self, idx):
"""
Load input image and preprocess:
- resize
- cast to float
- switch channels RGB -> BGR
- subtract mean
- transpose to channel x height x width order
"""
idx = self.indices[idx]
idx = idx.split()[0]
im = io.imread('{}/{}'.format(self.data_dir, idx))
im = Image.fromarray(im)
im = im.resize((self.width, self.height), Image.ANTIALIAS) # resize image
im = np.array(im, dtype=np.float32) # cast to float
im = im[:,:,::-1] # RGB -> BGR
im -= self.mean_bgr # mean subtraction
im = im.transpose((2,0,1))
return im
def load_label(self, idx):
"""
Load binary mask and preprocess:
- resize
- convert to greyscale
- cast to integer
- binarize
"""
idx = self.indices_label[idx]
idx=idx.split()[0]
im = io.imread('{}/{}'.format(self.data_dir, idx))
im = Image.fromarray(im)
im=im.resize((self.width, self.height), Image.NEAREST) # resize
im=im.convert('L') # convert to greyscale
im=np.array(im, dtype=(np.int32)) # cast to integer
label=im
label[label>0]=1 # convert to binary
label=np.array(label,np.uint8)
label = label[np.newaxis, ...]
return label
def load_depth(self, idx):
"""
Load depth map and preprocess:
- resize
- cast to float
- subtract mean
"""
idx = self.indices_depth[idx]
idx=idx.split()[0]
im = io.imread('{}/depth/{}'.format(self.data_dir, idx))
im = Image.fromarray(im)
im = im.resize((self.width, self.height), Image.ANTIALIAS) # resize
im = np.array(im, dtype=np.float32) # cast to float
d = im
d -= self.mean_depth # mean subtraction
d = d[np.newaxis, ...]
return d
def load_image(self, idx):
"""
Load input image and preprocess for Caffe:
- resize image
- cast to float
- switch channels RGB -> BGR
- subtract mean
- transpose to channel x height x width order
"""
idx=idx.split()[0]
try:
im = Image.open('{}/{}'.format(self.data_dir, idx))
except:
from skimage import io
im = io.imread('{}/{}'.format(self.data_dir, idx))
im = Image.fromarray(im)
im=im.resize((self.width, self.height), Image.ANTIALIAS) # resize image
im = np.array(im, dtype=np.float32) # cast to float
im = im[:,:,::-1] # RGB -> BGR
im -= self.mean # mean subtraction
# bring colour to the innermost dimension
im = im.transpose((2,0,1))
return im
def load_image(self, idx):
"""
Load input image and preprocess for Caffe:
- resize image
- cast to float
- switch channels RGB -> BGR
- subtract mean
- transpose to channel x height x width order
"""
idx=idx.split()[0]
try:
im = Image.open('{}/{}'.format(self.data_dir, idx))
except:
from skimage import io
im = io.imread('{}/{}'.format(self.data_dir, idx))
im = Image.fromarray(im)
im=im.resize((self.width, self.height), Image.ANTIALIAS) # resize image
im = np.array(im, dtype=np.float32) # cast to float
im = im[:,:,::-1] # RGB -> BGR
im -= self.mean # mean subtraction
# bring colour to the innermost dimension
im = im.transpose((2,0,1))
return im
def load_image(self, idx):
"""
Load input image and preprocess:
- resize
- cast to float
- switch channels RGB -> BGR
- subtract mean
- transpose to channel x height x width order
"""
idx = self.indices[idx]
idx = idx.split()[0]
im = io.imread('{}/{}'.format(self.data_dir, idx))
im = Image.fromarray(im)
im = im.resize((self.width, self.height), Image.ANTIALIAS) # resize image
im = np.array(im, dtype=np.float32) # cast to float
im = im[:,:,::-1] # RGB -> BGR
im -= self.mean_bgr # mean subtraction
im = im.transpose((2,0,1))
return im
def load_label(self, idx):
"""
Load binary mask and preprocess:
- resize
- convert to greyscale
- cast to integer
- binarize
"""
idx = self.indices_label[idx]
idx=idx.split()[0]
im = io.imread('{}/{}'.format(self.data_dir, idx))
im = Image.fromarray(im)
im=im.resize((self.width, self.height), Image.NEAREST) # resize
im=im.convert('L') # convert to greyscale
im=np.array(im, dtype=(np.int32)) # cast to integer
label=im
label[label>0]=1 # convert to binary
label=np.array(label,np.uint8)
label = label[np.newaxis, ...]
return label
def load_depth(self, idx):
"""
Load depth map and preprocess:
- resize
- cast to float
- subtract mean
"""
idx = self.indices_depth[idx]
idx=idx.split()[0]
im = io.imread('{}/depth/{}'.format(self.data_dir, idx))
im = Image.fromarray(im)
im = im.resize((self.width, self.height), Image.ANTIALIAS) # resize
im = np.array(im, dtype=np.float32) # cast to float
d = im
d -= self.mean_depth # mean subtraction
d = d[np.newaxis, ...]
return d
def SplitSave(self, p = 'TSD/Train/Images', wp = 'TSD/Train/Split'):
'''
#p: #Dir contains images to split
#wp: #Dir to write split images
'''
c = 0
if not os.path.exists(wp):
os.mkdir(wp)
pdl = np.random.choice([fni for fni in os.listdir(p) if fni.startswith('di')], 32, replace = False)
for i, fn in enumerate(pdl):
print('{:4d}/{:4d}:\t{:s}'.format(i + 1, len(pdl), fn))
#A = imread(os.path.join(p, fn))[0:-14, 1:-1]
#A = self.GetScreen()
#S = self.ts.DivideIntoSubimages(A).astype(np.uint8)
A = imread(os.path.join(p, fn))[0:-12, 4:-4, :]
S = self.ts.DivideIntoSubimages(A).astype(np.uint8)
for i, Si in enumerate(S):
imsave(os.path.join(wp, '{:03d}.png'.format(c)), Si)
c += 1
def load_images(self, test_list):
"""
train_list : list of users to use for testing
eg ["user_1", "user_2", "user_3"]
"""
self.image_list = []
for user in test_list:
csv = "%s%s/%s_loc.csv" % (self.data_directory, user, user)
with open(csv) as fh:
data = [line.strip().split(',') for line in fh]
for line in data[1:]:
img_path, x1,y1,x2,y2, = line
pos = tuple(map(int,(x1,y1,x2,y2)))
letter = img_path[-6]
img = io.imread("%s%s" % (self.data_directory, img_path))
self.image_list.append((img, pos, letter))