示例#1
0
    def next(self):
        # just grab the next random frame
        frame = self.source[random.choice(self.frame_ids)]
        # frame.show_image_with_labels()
        # get a random crop object
        crop_objs = filter(lambda x: not self.obj_types or x.obj_type in self.obj_types,frame.get_objects())
        # print 'Num crop objs in sample: {0}'.format(len(crop_objs))
        crop = random.choice(crop_objs)
        # print 'crop_box: ' + str(crop.box)

        # frame.show_image_with_labels()

        # 1) Randomly perturb crop box (scale and translation)
        transformed_box = RandomPerturber.perturb_crop_box(crop.box,{})

        # 2) Take crop, todo, change to using center crop to preserve aspect ratio
        # check if the affine is identity within some toleranc, then don't bother applying
        affine = Affine()
        scalex = float(self.crop_size[0])/transformed_box.edges()[0]
        scaley = float(self.crop_size[1])/transformed_box.edges()[1]
        affine.append(Affine.translation(-transformed_box.xy_min()))
        affine.append(Affine.scaling((scalex,scaley)))

        transformed_image = affine.apply_to_image(frame.image,self.crop_size) 
        # transformed_image.visualize(title='transformed image')

        # 3) Randomly perturb cropped image (rotation only)

        chw_image = transformed_image.to_order_and_class(Ordering.CHW,ValueClass.FLOAT01)
        # chw_image.visualize(title='chw_image')
        sample = AutoEncoderSample([torch.Tensor(chw_image.get_data().astype(float))],
                                   [torch.Tensor(chw_image.get_data().astype(float))])
        return sample
示例#2
0
def crop_image_resize(ptimage, crop_box, output_resolution):
    affine = Affine()
    scale = min(
        float(output_resolution[0]) / crop_box.edges()[0],
        float(output_resolution[1]) / crop_box.edges()[1])
    crop_offset = crop_box.xy_min() + 0.5 * (
        np.array(crop_box.edges()) - np.array(output_resolution) / scale)
    affine.append(Affine.translation(-crop_offset))
    affine.append(Affine.scaling([scale, scale]))
    # print affine.transform
    return affine
示例#3
0
def resize_image_center_crop(ptimage, output_resolution):
    affine = Affine()
    # calculate scale and crop offsets
    wh = ptimage.get_wh()
    scale = min(
        float(output_resolution[0]) / wh[0],
        float(output_resolution[1]) / wh[1])
    offset = 0.5 * (scale * np.array(wh) - np.array(output_resolution))
    affine.append(Affine.scaling([scale, scale]))
    affine.append(Affine.translation(-offset))
    return affine
示例#4
0
    def generate_random_affine(center,edges,params):
        affine = Affine()

        # translate to center coordinates
        affine.append(Affine.translation(-center))

        translate = get_deep(params,'translation_range',[-0.1,0.1])
        tx = random.random()*(translate[1]-translate[0])+translate[0]
        ty = random.random()*(translate[1]-translate[0])+translate[0]
        translation_range = np.array([tx,ty])*(-edges)
        affine.append(Affine.translation(translation_range))

        scale = get_deep(params,'scaling_range',[0.9,1.4])
        scale = random.random()*(scale[1]-scale[0])+scale[0]
        scaling_range = np.array([scale,scale])
        affine.append(Affine.scaling(scaling_range))

        # translate back 
        affine.append(Affine.translation(center))
        return affine