Commit 7d8de9ef authored by cc215's avatar cc215 💬
Browse files

revise

parent ea081abb
......@@ -5,7 +5,7 @@ import torch as th
import numpy as np
from skimage import transform as sktform
import random
from torchsample.utils import th_affine2d
def resample_by_spacing(im, new_spacing, interpolator=sitk.sitkLinear, keep_z_spacing=False):
'''
......@@ -205,92 +205,6 @@ class MySpecialRandomRotate(object):
return outputs
class MyRotate(object):
def __init__(self,
value,
output_size,
interp='bilinear',
lazy=False,crop=False):
"""
Randomly rotate an image between (-degrees, degrees). If the image
has multiple channels, the same rotation will be applied to each channel.
Arguments
---------
rotation_range : integer or float
image will be rotated between (-degrees, degrees) degrees
interp : string in {'bilinear', 'nearest'} or list of strings
type of interpolation to use. You can provide a different
type of interpolation for each input, e.g. if you have two
inputs then you can say `interp=['bilinear','nearest']
lazy : boolean
if true, only create the affine transform matrix and return that
if false, perform the transform on the tensor and return the tensor
"""
self.value = value
self.interp = interp
self.lazy = lazy
self.crop=crop ## remove black artifacts
self.output_size=output_size
def __call__(self, *inputs):
if not isinstance(self.interp, (tuple,list)):
interp = [self.interp]*len(inputs)
else:
interp = self.interp
theta =math.radians(self.value)
rotation_matrix = th.FloatTensor([[math.cos(theta), -math.sin(theta), 0],
[math.sin(theta), math.cos(theta), 0],
[0, 0, 1]])
self.theta=theta
if self.lazy:
return rotation_matrix
else:
outputs = []
new_w=0
new_h=0
for idx, _input in enumerate(inputs):
# lrr_width, lrr_height = _largest_rotated_rect(output_height, output_width,
# math.radians(rotation_degree))
# resized_image = tf.image.central_crop(image, float(lrr_height) / output_height)
# image = tf.image.resize_images(resized_image, [output_height, output_width],
# method=tf.image.ResizeMethod.BILINEAR, align_corners=False)
image_height=_input.size(1)
image_width=_input.size(2)
if not self.theta ==0.:
input_tf = th_affine2d(_input,
rotation_matrix,
mode=interp[idx],
center=True)
print ('size:',input_tf.size())
if self.crop:
if idx == 0:
##find largest rec to crop## adapted from the origin: https://stackoverflow.com/questions/16702966/rotate-image-and-crop-out-black-borders
new_w, new_h = largest_rotated_rect(
image_height,
image_width,
theta)
edge=min(new_w,new_h)
# out_edge=max(self.output_size[2],self.output_size[1])
cropper = MySpecialCrop(size=(edge, edge, 1), crop_type=0)
print('here')
output = cropper(input_tf) ## 1*H*W
Resizer = MyResize(size=(self.output_size[1], self.output_size[2]), interp=interp[idx])
output = Resizer(output)
else:
input_tf=_input #
padder=MyPad(size=(1,self.output_size[1],self.output_size[2]))
output = padder(input_tf)
# print (output.size())
outputs.append(output)
return outputs if idx >= 1 else outputs[0]
class MyResize(object):
"""
resize a 2D numpy array using skimage , support float type
......@@ -629,4 +543,4 @@ class RandomGamma(object):
def __call__(self, image, origin_space=None):
assert len(image.shape) == 3
\ No newline at end of file
assert len(image.shape) == 3
......@@ -173,7 +173,7 @@ def predict(sequence_name, root_dir, image_format_name,
link_image(origin_path=image_path, root_dir=save_dir, patient_dir=pid)
print('Successfully segmented {:d} subjects '.format(n_count))
print('Cost {:.3f}s per subjects ( exclude data loading time)'.format(np.mean(time_records)))
print('Cost {:.3f}s per subjects (exclude data loading time)'.format(np.mean(time_records)))
if __name__ == '__main__':
......@@ -236,5 +236,5 @@ if __name__ == '__main__':
save_name_format = args.save_name_format
predict(sequence_name=sequence_name, root_dir=root_dir, readable_frames=readable_frames,
image_format_name=image_format_name, model_path=model_path, batch_size=batch_size, if_resample=if_resample,
save_dir=save_dir, save_format_name=save_name_format)
image_format_name=image_format_name, model_path=model_path, batch_size=batch_size,
if_resample=if_resample,save_dir=save_dir, save_format_name=save_name_format)
numpy
matplotlib
torch==1.0.0
git+https://github.com/ncullen93/torchsample.git
pandas
nibabel
tqdm
......
/vol/bitbucket/cc215/Projects/Cardiac_Multi_View_Segmentation/test_data/10DG02038/LVSA/LVSA_img_ED.nii.gz
\ No newline at end of file
/vol/bitbucket/cc215/Projects/Cardiac_Multi_View_Segmentation/test_data/10DH04454/LVSA/LVSA_img_ED.nii.gz
\ No newline at end of file
/vol/bitbucket/cc215/Projects/Cardiac_Multi_View_Segmentation/test_data/10DH04454/LVSA/LVSA_img_ES.nii.gz
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment