def build_rotations(path,pathdir,files,labels,all_count,size):
    train_labels=labels
    barr=len(labels)

    # train_dates={label:[] for label in train_labels}
    train_dates={}
    for label in train_labels:
        '''每一个label都扩充成4倍,
        原来的原来的是原来的label,
        下一轮是rotate 90的,再下一轮是180的,最后一伦施270的'''
        train_dates[int(label)]=[]
        train_dates[int(label)+barr]=[]
        train_dates[int(label)+2*barr]=[]
        train_dates[int(label)+3*barr]=[]

    for file in files:
        label=file[-11:-7]
        if label in train_labels:
            train_dates[int(label)].append(0.001*(255-np.float32(imresize(imread(file,1),size))))
            train_dates[int(label)+barr].append(0.001*(255-np.float32((imresize(imrotate(imread(file,1),90),size)))))
            train_dates[int(label)+2*barr].append(0.001*(255-np.float32((imresize(imrotate(imread(file,1),180),size)))))
            train_dates[int(label)+3*barr].append(0.001*(255-np.float32((imresize(imrotate(imread(file,1),270),size)))))

    if cnn_only:
        return train_dates
def build_rotations(path,pathdir,files,labels,all_count,size):
    train_labels=labels
    barr=len(labels)
    barr_test=len(labels_eval)

    # train_dates={label:[] for label in train_labels}
    train_dates={}
    test_dates={}
    for label in train_labels:
        '''每一个label都扩充成4倍,
        原来的原来的是原来的label,
        下一轮是rotate 90的,再下一轮是180的,最后一伦施270的'''
        train_dates[int(label)]=[]
        train_dates[int(label)+barr]=[]
        train_dates[int(label)+2*barr]=[]
        train_dates[int(label)+3*barr]=[]

    for label in labels_eval:
        test_dates[int(label)]=[]
        test_dates[int(label)+barr_test]=[]
        test_dates[int(label)+2*barr_test]=[]
        test_dates[int(label)+3*barr_test]=[]

    print 'files_total:',len(files)
    for file in files:
        label=file[-11:-7]
        # print label
        if label in train_labels:
            # print 'train_data:',label
            train_dates[int(label)].append(0.001*(255-np.float32(imresize(imread(file,1),size))))
            train_dates[int(label)+barr].append(0.001*(255-np.float32((imresize(imrotate(imread(file,1),90),size)))))
            train_dates[int(label)+2*barr].append(0.001*(255-np.float32((imresize(imrotate(imread(file,1),180),size)))))
            train_dates[int(label)+3*barr].append(0.001*(255-np.float32((imresize(imrotate(imread(file,1),270),size)))))
        else:
            test_dates[int(label)].append(0.001*(255-np.float32(imresize(imread(file,1),size))))
            test_dates[int(label)+barr_test].append(0.001*(255-np.float32((imresize(imrotate(imread(file,1),90),size)))))
            test_dates[int(label)+2*barr_test].append(0.001*(255-np.float32((imresize(imrotate(imread(file,1),180),size)))))
            test_dates[int(label)+3*barr_test].append(0.001*(255-np.float32((imresize(imrotate(imread(file,1),270),size)))))
            # print 'test_data:',label

    if cnn_only:
        return train_dates

    x_train,y_train=get_sequence_images(train_dates,train_labels,path_length,total_labels_per_seq,size,total_roads)

    # x_train,y_train=get_sequence_images(train_dates,train_labels,path_length,total_labels_per_seq,size,total_roads)
    x_test,y_test=get_sequence_images(test_dates,labels_eval,path_length,total_labels_per_seq,size,total_roads)
    return x_train,y_train,x_test,y_test
Exemplo n.º 3
0
def copy_incorrect(in_folder, out_folder, incorrect_files="snapshotVGG1-5-test.txt"):
    from scipy.misc import imread, imsave, imrotate
    print(incorrect_files)
    if os.path.exists(incorrect_files):
        f = open(incorrect_files, "r")
        print("File found")
    else:
        f = open(os.path.join(in_folder, "stats", incorrect_files), "r")
    page = f.read()

    sources = page.split('\n')
    print(sources)
    print(len(sources))
    count = 0
    for source in sources:
        if source.find("jpg") >= 0:
            fileinfo = source
            if source.find(",") >= 0:
                fileinfo = source.split(", ")[0]
                rotation = source.split(", ")[1]
                image = imread(fileinfo)
                image = imrotate(image, int(rotation))
            else:
                image = imread(fileinfo)
            if count == 0:
                print(fileinfo)
            count += 1
            destination = os.path.split(fileinfo.replace(in_folder, out_folder))[0]
            if not os.path.exists(destination):
                os.makedirs(destination)
            filename = os.path.split(fileinfo)[1]
            # print(os.path.join(destination, filename))
            imsave(os.path.join(destination, filename), image)
    print("Moved " + str(count) + " files")
Exemplo n.º 4
0
def _create_feature_glyph(feature, vbs):
    r"""
    Create glyph of feature pixels.

    Parameters
    ----------
    feature : (N, D) ndarray
        The feature pixels to use.
    vbs: int
        Defines the size of each block with vectors of the glyph image.
    """
    # vbs = Vector block size
    num_bins = feature.shape[2]
    # construct a "glyph" for each orientation
    block_image_temp = np.zeros((vbs, vbs))
    # Create a vertical line of ones, to be the first vector
    block_image_temp[:, round(vbs / 2) - 1:round(vbs / 2) + 1] = 1
    block_im = np.zeros((block_image_temp.shape[0],
                         block_image_temp.shape[1],
                         num_bins))
    # First vector as calculated above
    block_im[:, :, 0] = block_image_temp
    # Number of bins rotations to create an 'asterisk' shape
    for i in range(1, num_bins):
        block_im[:, :, i] = imrotate(block_image_temp, -i * vbs)

    # make pictures of positive feature_data by adding up weighted glyphs
    feature[feature < 0] = 0
    glyph_im = np.sum(block_im[None, None, :, :, :] *
                      feature[:, :, None, None, :], axis=-1)
    glyph_im = np.bmat(glyph_im.tolist())
    return glyph_im
Exemplo n.º 5
0
def rotate_aa (frame):
  step = frame/frames
  fft = np.zeros((size, size), np.complex)
  fft[0][0] = freq_mag #*step #255

  loc = cmath.rect(5, 2*np.pi*step)

  xloc = loc.real
  yloc = loc.imag
  #import pdb;pdb.set_trace()

  s = 0
  for dx in fiter(xloc):
    for dy in fiter(yloc):
      xpart = 1 - abs(dx - xloc)
      ypart = 1 - abs(dy - yloc)
      pct = xpart*ypart
      s += pct
      fft[dy,dx] = cmath.rect(pct*freq_mag/4, 0 if dx%2 == dy%2 else np.pi)
      fft[-dy,-dx] = cmath.rect(pct*freq_mag/4, 0 if dx%2 == dy%2 else np.pi)

  ifft = np.fft.ifft2(fft)

  refft = np.fft.fft2(np.abs(ifft))

  #small_img = imresize(np.abs(ifft), 0.5, interp="nearest", mode='F')
  small_img = (imrotate(np.abs(ifft), step*360)/255)[25:-25,25:-25]
  small_refft = np.fft.fft2(small_img)

  return fft, ifft, refft, small_img, small_refft
Exemplo n.º 6
0
def crawl_directory(directory, augment_with_rotations=False,
                    first_label=0):
  """Crawls data directory and returns stuff."""
  label_idx = first_label
  images = []
  labels = []
  info = []

  # traverse root directory
  for root, _, files in os.walk(directory):
    logging.info('Reading files from %s', root)
    fileflag = 0
    for file_name in files:
      full_file_name = os.path.join(root, file_name)
      img = imread(full_file_name, flatten=True)
      for i, angle in enumerate([0, 90, 180, 270]):
        if not augment_with_rotations and i > 0:
          break

        images.append(imrotate(img, angle))
        labels.append(label_idx + i)
        info.append(full_file_name)

      fileflag = 1

    if fileflag:
      label_idx += 4 if augment_with_rotations else 1

  return images, labels, info
Exemplo n.º 7
0
	def add_shape(self, f):
		#Create shape
		S = Shape(f, self.R, self.SHAPE_R)
	
		#Add to shape list
		S.shape_num = len(self.shape_list)
		self.shape_list.append(S)
		
		row = []
		for k in range(len(self.shape_list)):
			T = self.shape_list[k]
			ift = real(ipfft(pft_mult(pft_rotate(S.pft, 2.*pi/6.), T.pft), 2*self.SHAPE_R+1,2*self.SHAPE_R+1))
			Spad = imrotate(cpad(S.indicator, array([2*self.SHAPE_R+1,2*self.SHAPE_R+1])), 360./6.)
			Tpad = cpad(T.indicator, array([2*self.SHAPE_R+1,2*self.SHAPE_R+1]))
			pind = real(fftconvolve(Spad, Tpad, mode='same'))
			imshow(pind)
			imshow(ift)
			obst = to_ind(pind, 0.001)
			imshow(obst)
			cutoff = best_cutoff(ift, obst, S.radius + T.radius)
			print cutoff
			imshow(to_ind(ift, cutoff))
			row.append(cutoff * self.tarea)
		self.cutoff_matrix.append(row)

		return S
Exemplo n.º 8
0
def getAlignImg(t,label = None):#!!!notice, only take uint8 type for the imrotate function!!!
    f = lambda x:np.asarray([float(a) for a in x]);
    o = f(t.ImageOrientationPatient);
    o1 = o[:3];
    o2 = o[3:];
    oh = np.cross(o1,o2);
    or1 = np.asarray([0.6,0.6,-0.2]);
    o2new = np.cross(oh,or1);
    theta = np.arccos(np.dot(o2,o2new)/np.sqrt(np.sum(o2**2)*np.sum(o2new**2)))*180/3.1416;
    theta = theta * np.sign(np.dot(oh,np.cross(o2,o2new)));
    im_max = np.percentile(t.pixel_array.flatten(),99);
    res = imrotate(np.array(np.clip(np.array(t.pixel_array,dtype=np.float)/im_max*256,0,255),dtype=np.uint8),theta);
    if label is None:
        return res;
    else:
        lab = imrotate(label,theta);
        return res,lab
Exemplo n.º 9
0
 def correct_rotation(self, img, amin, amax, step = 1):
     results = []
     for a in range(int(amin / step), int(amax / step) + 1):
         rot = misc.imrotate(img, a * step)
         corr = self._corr(rot)
         print a * step, corr
         results.append((corr, rot))
     return max(results, key = lambda x: x[0])[1]
Exemplo n.º 10
0
def rotate_images(images, angle):
    images_list = [None] * images.shape[0]
    for i in range(images.shape[0]):
        images_list[i] = misc.imrotate(images[i,:,:,:], angle)
    images_rot = np.stack(images_list,axis=0)
    sz1 = images_rot.shape[1]/2
    sz2 = FLAGS.image_size/2
    images_crop = images_rot[:,(sz1-sz2):(sz1+sz2),(sz1-sz2):(sz1+sz2),:]
    return images_crop
Exemplo n.º 11
0
def rotate_gen(imgin, num_seq, seq_len, patch_size,
                rgw0=30., rga0=2.):
    """
    Generate rotation patches from imgin(non-flattened).  

    Parameters
    ----------
    imgin: double
        The non-flattened single-channel input image.
    num_seq: int
        Number of sequences to generate. 
    seq_len: int
        The length of the output patch sequence.
    patch_size: int
        The length of side of the output pathes.
    rgw0: double
        Range of initial values of angular velocity.
    rga0: double
        Range of initial values of angular acceleration.

    Returns
    -------
    patch_seq: double (num_seq, seq_len * patch_size**2)
        Output patch sequences.
    """

    img_shape = imgin.shape
    crops = patch_size/2            # crop after rotation
    crope = patch_size/2+patch_size # crop after rotation
    
    thetas = [None] * seq_len  
    seqs = numpy.zeros((num_seq, seq_len*(patch_size**2)))

    for i in range(num_seq):

        # compute the rotation angle for each time step
        theta = 0.
        w = numpy.random.uniform(-rgw0, rgw0)
        a = numpy.random.uniform(-rga0, rga0)
        for t in range(seq_len):
            thetas[t] = theta
            theta += w
            w += a

        # sample a 2 times larger patch for rotation purpose
        row = numpy.random.randint(high=img_shape[0]-2*patch_size, 
                                    low=0)  
        col = numpy.random.randint(high=img_shape[1]-2*patch_size, 
                                    low=0)  
        patch_large = imgin[row:row+2*patch_size, col:col+2*patch_size]

        seqs[i, :] = numpy.concatenate([misc.imrotate(patch_large, theta)\
                    [crops:crope, crops:crope].flatten() 
                    for theta in thetas], axis=1)
    return seqs
Exemplo n.º 12
0
def rotate_dataset(X, Y):
    """
    This produces a dataset 2 times bigger than the original one,
    by rptating the 28x28 images in 10 degrees clockwise and counter clockwise
    """
    angles = [-10, 10]

    rotate = lambda x, w: imrotate(x.reshape((28, 28)), w).ravel()
    X = np.concatenate([X] + [np.apply_along_axis(rotate, 1, X, angle) for angle in angles])
    Y = np.concatenate([Y for _ in range(3)], axis=0)
    return X, Y
Exemplo n.º 13
0
def augment_image(im, aug_code):
    '''
    0: no augmentation, 1-3: rotation, 4-5: flip
    TODO: 6-8: jitter R, G, B channels
    '''
    if aug_code == 0:
        return im
    elif aug_code == 1:
        im_aug = misc.imrotate(im, 90)
    elif aug_code == 2:
        im_aug = misc.imrotate(im, 180)
    elif aug_code == 3:
        im_aug = misc.imrotate(im, 270)
    elif aug_code == 4:
        im_aug = np.fliplr(im)
    elif aug_code == 5:
        im_aug = np.flipud(im)
    else:
        raise Exception("Wrong augmentation code")

    return im_aug
Exemplo n.º 14
0
 def rotate_image(self,image,rotate=None):
     """rotates and overwrites image.
            - rotate is angle in degrees"""
     if rotate == None:
         rotate = self.header['aspect'][0]
     print 'Rotate:  ',rotate
     img_max = np.max(image)
     if rotate != 0.0:
         image = misc.imrotate(image,rotate)
         maxout = float(np.max(image))
         image = img_max*image/maxout
     return image
Exemplo n.º 15
0
    def align(self, img, f5pt):
        ec_mc_y = 48.0
        crop_size = 128
        ec_y = 40
        
        ang_tan = (f5pt[0,1]-f5pt[1,1])/(f5pt[0,0]-f5pt[1,0])
        ang = atan(ang_tan) / pi * 180
        img_rot = imrotate(img, ang, 'bicubic')


        # eye center
        x = (f5pt[0,0]+f5pt[1,0])/2;
        y = (f5pt[0,1]+f5pt[1,1])/2;

        ang = -ang/180*pi;
        xx,yy = transform(x,y,ang,img.shape,img_rot.shape)

        eyec = NP.array([xx, yy]).astype(long)

        x = (f5pt[3,0]+f5pt[4,0])/2;
        y = (f5pt[3,1]+f5pt[4,1])/2;
        [xx, yy] = transform(x, y, ang, img.shape, img_rot.shape)
        mouthc = NP.array([xx, yy]).astype(long)

        resize_scale = ec_mc_y/(mouthc[1]-eyec[1])

        img_resize = imresize(img_rot, resize_scale);


        eyec2 = eyec*resize_scale 
        eyec2 = NP.round(eyec2);
        img_crop = NP.zeros((crop_size, crop_size,3),dtype='uint8')
        # crop_y = eyec2(2) -floor(crop_size/3.0);
        crop_y = eyec2[1] - ec_y
        crop_y_end = crop_y + crop_size
        crop_x = eyec2[0]-int(crop_size/2)
        crop_x_end = crop_x + crop_size
        
        
        box = NP.array([crop_x, crop_x_end, crop_y, crop_y_end])
        box[box<0] = 0
        box[1] = min(box[1], img_resize.shape[1])
        box[3] = min(box[3], img_resize.shape[0])
        try:
            img_crop[(box[2]-crop_y):(box[3]-crop_y), (box[0]-crop_x):(box[1]-crop_x),:] = \
                 img_resize[box[2]:box[3],box[0]:box[1],:]
        except:
            print box, crop_y, crop_x, img_resize.shape
            raise

        return img_crop,img_resize,dlib.rectangle(int(box[0]), int(box[2]),int(box[1]),int(box[3]))          
def build_rotations(path,pathdir,files,labels,all_count,size):
    train_labels=labels
    barr=len(labels)

    # train_dates={label:[] for label in train_labels}
    train_dates={}
    test_dates={}
    for label in train_labels:
        '''每一个label都扩充成4倍,
        原来的原来的是原来的label,
        下一轮是rotate 90的,再下一轮是180的,最后一伦施270的'''
        train_dates[int(label)]=[]
        train_dates[int(label)+barr]=[]
        train_dates[int(label)+2*barr]=[]
        train_dates[int(label)+3*barr]=[]

    for file in files:
        label=file[-11:-7]
        if label in train_labels:
            train_dates[int(label)].append(0.001*(255-np.float32(imresize(imread(file,1),size))))
            train_dates[int(label)+barr].append(0.001*(255-np.float32((imresize(imrotate(imread(file,1),90),size)))))
            train_dates[int(label)+2*barr].append(0.001*(255-np.float32((imresize(imrotate(imread(file,1),180),size)))))
            train_dates[int(label)+3*barr].append(0.001*(255-np.float32((imresize(imrotate(imread(file,1),270),size)))))
        else:
            if label not in test_dates.keys():
                test_dates[label]=[]
            test_dates[label].append(0.001*(255-np.float32(imresize(imread(file,1),size))))
            #TODO:上面这个俩label 加了int就正确率100%,找一下为啥:明白了,老铁!
	    # print 'test_datas:',label

    if cnn_only:
        return train_dates

    x_train,y_train=get_sequence_images(train_dates,train_labels,path_length,total_labels_per_seq,size,total_roads)

    # x_train,y_train=get_sequence_images(train_dates,train_labels,path_length,total_labels_per_seq,size,total_roads)
    x_test,y_test=get_sequence_images(test_dates,labels_eval,path_length,total_labels_per_seq,size,total_roads)
    return x_train,y_train,x_test,y_test
Exemplo n.º 17
0
    def rotate_img(self, X):       
        
        def theta():
            return np.random.random() * 90 - 45 # random rotation degree from [-45, 45]        
        
        for i in range(X.shape[0]):
            deg = theta()
            for j in range(X.shape[1]):
                X[i, j, :, :] = imrotate(X[i, j, :, :], deg)

        return X


        
Exemplo n.º 18
0
def demo_image():
    """
    misc model in scipy contain some method to operate on image, such as 
    open, save, resize, show; and it will operate image as a numpy array.
    """
    img = misc.imread("image_process/tire.bmp")  # img is a numpy array
    """Resize a image to a big one"""
    re_img = misc.imresize(img, (600, 600))
    """Rotate a image to another one"""
    ro_img = misc.imrotate(re_img, 45)
    """Save this image as file"""
    misc.imsave("test.bmp", ro_img)
    """Show a image without matplotlib"""
    misc.imshow(ro_img)
Exemplo n.º 19
0
def run_pretrained(input_state,model,action_states,gameState):
    print '\n\nLoading pretrained weights onto model...'
    model.load_weights(p.PRETRAINED_PATH)
    epsilon=1
    while True:
        print 'Running pretrained model (no exploration) with weights at ', p.PRETRAINED_PATH 
               
        nn_out = model.predict(input_state,batch_size=1,verbose=0)
        nn_action = [[0,1]] if np.argmax(nn_out) else [[1,0]]
        action,rand_flag = select_action(nn_action+action_states,prob=[epsilon,(1-epsilon)/2,(1-epsilon)/2])
        rgbDisplay, reward, tState = gameState.frame_step(action)
        #grayDisplay = (np.dot(imresize(rgbDisplay, (80,80), interp='bilinear')[:,:,:3], [0.299, 0.587, 0.114])).reshape((1,1,80,80))
        grayDisplay = (np.dot(np.fliplr(imrotate(imresize(rgbDisplay, (80,80), interp='bilinear'), -90))[:,:,:3], [0.299, 0.587, 0.114])).reshape((1,1,80,80))
        output_state = np.append(input_state[:,1:,:,:], grayDisplay,axis=1)
Exemplo n.º 20
0
def rotate_manual (frame):
  step = frame/frames
  fft = np.zeros((size, size), np.complex)
  fft[0][0] = freq_mag
  fft[0][5] = freq_mag


  ifft = np.fft.ifft2(fft)

  refft = np.fft.fft2(ifft)

  small_img = (imrotate(np.abs(ifft), step*360)/255)[25:-25,25:-25]
  small_refft = np.fft.fft2(small_img)

  return fft, ifft, refft, small_img, small_refft
Exemplo n.º 21
0
    def perturb(img, scale, angle, skew, mirror):
        img2 = img.copy()
        if scale is not 1:
            img2 = imresize(img2, scale)
        if angle is not 0:
            img2 = imrotate(img2, angle)
        if skew is not 0:
            pass
        if mirror:
            img2 = np.fliplr(img2)

        #plt.imshow(img2)
        #plt.show()

        return img2
Exemplo n.º 22
0
Arquivo: dec.py Projeto: VikingMew/dec
def hog_picture(hog, resolution):
    from scipy.misc import imrotate
    glyph1 = np.zeros((resolution, resolution), dtype=np.uint8)
    glyph1[:, round(resolution / 2)-1:round(resolution / 2) + 1] = 255
    glyph = np.zeros((resolution, resolution, 9), dtype=np.uint8)
    glyph[:, :, 0] = glyph1
    for i in xrange(1, 9):
        glyph[:, :, i] = imrotate(glyph1, -i * 20)

    shape = hog.shape
    clamped_hog = hog.copy()
    clamped_hog[hog < 0] = 0
    image = np.zeros((resolution * shape[0], resolution * shape[1]), dtype=np.float32)
    for i in xrange(shape[0]):
        for j in xrange(shape[1]):
            for k in xrange(9):
                image[i*resolution:(i+1)*resolution, j*resolution:(j+1)*resolution] = np.maximum(image[i*resolution:(i+1)*resolution, j*resolution:(j+1)*resolution], clamped_hog[i, j, k] * glyph[:, :, k])

    return image
Exemplo n.º 23
0
def rotation( X, alpha ):
    """
    This function rotates the image
    :param X    : vector representing the word
    :param alpha: angles by which image is to be rotated
    :return Y   : vector representing rotated word
    :rtype : list
    """
    Y = imrotate(X, alpha)
    len_x1, len_x2 = X.shape
    len_y1, len_y2 = Y.shape

    from_x = np.floor((len_y1 + 1 - len_x1) / 2)
    from_y = np.floor((len_y2 + 1 - len_x2) / 2)
    Y = Y[from_x:from_x+len_x1, from_y:from_y+len_x2]

    idx = np.where(Y == 0)
    Y[idx] = X[idx]

    return Y
    def VariationData(self, img, numb_variations):
        size = img.shape

        variationed_img = []
        for i in range(numb_variations):
            if self.flip:
                if np.random.rand(1) > 0.5:
                    # do vertical flip
                    new_img = cv2.flip(img, flipCode=1)
                else:
                    new_img = np.copy(img)
            if len(self.rotation) != 0:
                rand_rot_index = np.random.randint(len(self.rotation))
                new_img = smisc.imrotate(new_img, self.rotation[rand_rot_index])
            if self.img_size[0] != size[0] and self.img_size[1] != size[1]:
                new_img = smisc.imresize(new_img, self.img_size, interp='nearest')
                cv2.imshow('orig', img)
                cv2.imshow('test', new_img)
                cv2.waitKey(0)
            variationed_img.append(new_img)
        return variationed_img
Exemplo n.º 25
0
def rotate (frame):
  step = frame/frames
  fft = np.zeros((size, size), np.complex)
  fft[0][0] = freq_mag #*step #255

  #xloc = int((size-1)*step)
  loc = cmath.rect(5, 2*np.pi*step)

  xloc = int(loc.real)
  yloc = int(loc.imag)
  #fft[-yloc, -xloc] = cmath.rect(freq_mag/2, 0)
  fft[yloc, xloc] = cmath.rect(freq_mag, 0)
  ifft = np.fft.ifft2(fft)

  refft = np.fft.fft2(ifft)

  #small_img = imresize(np.abs(ifft), 0.5, interp="nearest", mode='F')
  small_img = (imrotate(np.abs(ifft), step*360)/255)[25:-25,25:-25]
  small_refft = np.fft.fft2(small_img)

  return fft, ifft, refft, small_img, small_refft
Exemplo n.º 26
0
def hog_picture(w, bs=20):
    """ Visualize positive HOG weights.
    ported to numpy from https://github.com/CSAILVision/ihog/blob/master/showHOG.m
    """
    if not imrotate_available:
        raise RuntimeError('This function requires scipy')
    bim1 = np.zeros((bs, bs))
    bim1[:,round(bs/2)-1:round(bs/2)] = 1
    bim = np.zeros((9,)+bim1.shape)
    for i in xrange(9):
      bim[i] = imrotate(bim1, -i*20)/255.0
    s = w.shape
    w = w.copy()
    w[w < 0] = 0
    im = np.zeros((bs*s[0], bs*s[1]))
    for i in xrange(s[0]):
      iis = slice( i*bs, (i+1)*bs )
      for j in xrange(s[1]):
        jjs = slice( j*bs, (j+1)*bs )
        for k in xrange(9):
          im[iis,jjs] += bim[k] * w[i,j,k+18]
    return im/np.max(w)
Exemplo n.º 27
0
def merge(inf="Lab/soi/apr14/gan_si_004_02-r1",refine=True,rep=1,msize=1000,rad=300):
	import pyfits
	from numpy import zeros,concatenate,r_,mgrid
	dd=[pyfits.open(inf+a+".fits.gz")[1].data for a in "ab"]
	ina=array([[ 382.07734858,  382.7503476 ],[ 399.76129709,  195.99717147]])
	if rep==0: return dd,ina
	if refine:
		out=[]
		for d in dd:
			z=get_edge(d,0,ndsp=glob_base_disp)
			cz=find_centre(z)[0]
			#newz=concatenate([z[0],find_fasete(z,cz,rep=2)])
			out.append(refit(z,cz)[0])
	if rep==2: return out
	from scipy import misc
	for i in range(2):
		rt=rad/out[i][2]
		nsiz=(r_[dd[i].shape]*r_[out[i][3]*rt,rt]).astype(int)
		dd[i]=misc.imresize(dd[i],nsiz)
		z=get_edge(dd[i],0,ndsp=glob_base_disp)
		cz=find_centre(z)[0]
		ang=find_fasete(z,cz)
		if abs(ang)>0.1:
			dd[i]=misc.imrotate(dd[i],-ang)
			z=get_edge(dd[i],0,ndsp=glob_base_disp)
			cz=find_centre(z)[0]
		out[i]=refit(z,cz)[0]	
		ina[i]=out[i][:2]#out[i][:2]*nsiz/r_[dd[i].shape]
	print(out)
	if rep==3: return dd,ina
	#sel=((mgrid[:dd[0].shape[0],:dd[0].shape[1]]-a[:2,newaxis,newaxis])**2).sum(0)>a[2]**2
	dz=zeros((msize,msize))
	#mid=r_[]
	for i in range(2):
		a=ina[i]
		sel=(((mgrid[:dd[1].shape[0],:dd[1].shape[1]]-a[1::-1,newaxis,newaxis])*r_[a[3],1][:,newaxis,newaxis])**2).sum(0)>a[2]**2
		dd[i][sel]=0
		dz[a[1]:a[1]+512,a[0]:a[0]+768]=dd[i]
	nd=misc.imresize(dd[1],Out[70])
Exemplo n.º 28
0
 def _gabor(self, FT):
     """ Helper function to apply Gabor filter in 
         frequensy domain.
     """
     if self._sf > self._size / 2:
         msg = ('Base frequency for Gabor '
               'noise is  too high (exceeds Nyquist limit).')
         raise Warning(msg)
     localf = self._sf / self._size
     linbw = 2 ** self.noiseBW
     lowf = 2.0 * localf / (linbw + 1.0)
     highf = linbw * lowf
     FWF = highf - lowf
     sigmaF = FWF/(2*numpy.sqrt(2*numpy.log(2)))
     FWO = 2.0*localf*numpy.tan(numpy.pi*self.noiseBWO/360.0)
     sigmaO = FWO/(2*numpy.sqrt(2*numpy.log(2)))
     yy, xx = numpy.mgrid[0:self._size, 0:self._size]
     xx = (0.5 - 1.0 / self._size * xx) 
     yy = (0.5 - 1.0 / self._size * yy) 
     filter=filters.make2DGauss(xx,yy,mean=(localf,0), sd=(sigmaF,sigmaO))
     filter=filter+filters.make2DGauss(xx,yy, mean=(-localf,0), sd=(sigmaF,sigmaO))
     filter=imrotate(filter, self.noiseOri, interp='bicubic')
     return FT*filter
Exemplo n.º 29
0
def find_match(im0, im1, step_size=0.25):
	misc.imsave("/tmp/a.png", im0)
	misc.imsave("/tmp/b.png", im1)
	#im1 = blur_out(im1)
	x=0
	best=0
	mathch=None
	numS = im0.shape[0]/(step_size*im1.shape[0]) * im0.shape[1]/(step_size*im1.shape[1])
	numC = 0
	while x+im1.shape[0]<=im0.shape[0]:
		y=0
		numC += 1
		while y+im1.shape[1]<=im0.shape[1]:
			im2_, scale, angle, (t0, t1), sim = similarity(im1, numpy.asarray(im0[x:x+im1.shape[0], y:y+im1.shape[1]]))
			#print(scale, angle, x, y, t0, t1, sim)
			#print(sim)
			if sim>best:# or sim>0.1:
				print("BEST "+str(100.*numC/numS)+"%")
				print(scale, angle, x, y, t0, t1, sim)
				im2=im2_
				best=sim
				tt0=math.cos(-angle)*t0 - math.sin(-angle)*t1
				tt1=math.sin(-angle)*t0 + math.cos(-angle)*t1
				ox=int(tt0+x)
				oy=int(tt1+y)
				match=(ox,oy,-angle, sim)

			y+=int(step_size*im1.shape[1])
		x+=int(step_size*im1.shape[0])

	im = misc.imrotate(im0, match[2])
	ox = max(match[0],0)
	oy = max(match[1],0)
	im3= numpy.asarray(im[ox:(ox+im1.shape[0]), oy:(oy+im1.shape[1])])
	imshow(im0,im1,im3,im2_)

	return match
Exemplo n.º 30
0
 def random_rotate_image_func(image):
     #旋转角度范围
     angle = np.random.uniform(low=-angle_tol, high=angle_tol)
     return misc.imrotate(image, angle, 'bicubic')
Exemplo n.º 31
0
f, plots = plt.subplots(2, 3)
plots[0,0].imshow(image, cmap="gray")
plots[1,0].imshow(image, cmap="gray")

##########################################
###      PROGRAMA CON PASO BAJO        ###
##########################################

# tipo de filtro en el espacio de Fourier
filtro = filtroPasoBajo(xSize, ySize)
plots[0,1].imshow(filtro, cmap="gray")
# Ultima lente 
screen = np.fft.fft2(fourierS*filtro)

# Mostrar la imagen rotandola
plots[0,2].imshow(imrotate(abs(screen), 180), cmap="gray")

##########################################
###      PROGRAMA CON PASO ALTO        ###
##########################################

# tipo de filtro en el espacio de Fourier
filtro = filtroPasoAlto(xSize, ySize)
plots[1,1].imshow(filtro, cmap="gray")

# Ultima lente 
screen = np.fft.fft2(fourierS*filtro)

# Mostrar la imagen rotandola
plots[1,2].imshow(imrotate(abs(screen), 180), cmap="gray")
Exemplo n.º 32
0
def rot90(sample):
    print(type(sample[0]))
    print(sample[0].shape)
    image = imrotate(sample[0], 90)
    return image, sample[1]
Exemplo n.º 33
0
def radon_transform(image, steps):
    radon = np.zeros((steps, len(image)), dtype='float64')
    for s in range(steps):
        rotation = misc.imrotate(image, -s * 180 / steps).astype('float64')
        radon[:, s] = sum(rotation)
    return radon
def imagemorphparam_v2(dsm, dem, scale, mid, dtheta, dlg, imp_point):

    try:
        build = dsm - dem
        build[(build < 2.)] = 0.  # building should be higher than 2 meter

        # new part
        buildvec = build[np.where(build > 0)]
        if buildvec.size > 0:
            zH_all = buildvec.mean()
            zHmax_all = buildvec.max()
            zH_sd_all = buildvec.std()
            pai_all = (buildvec.size * 1.0) / (build.size * 1.0)
        else:
            zH_all = 0
            zHmax_all = 0
            zH_sd_all = 0
            pai_all = 0

        fai = np.zeros((int(360. / dtheta), 1))
        zH = np.zeros((int(360. / dtheta), 1))
        zHmax = np.zeros((int(360. / dtheta), 1))
        zH_sd = np.zeros((int(360. / dtheta), 1))
        pai = np.zeros((int(360. / dtheta), 1))
        deg = np.zeros((int(360. / dtheta), 1))

        #%subset and center
        n = dsm.shape[0]
        imid = np.floor((n / 2.))
        if mid == 1:
            dY = np.int16(np.arange(np.dot(
                1, imid)))  # half the length of the grid (y)
        else:
            dY = np.int16(np.arange(np.dot(
                1, n)))  # the whole length of the grid (y)
        if imp_point == 1:
            dlg.progressBar.setRange(0., 360. / dtheta)
        dX = np.int16(np.arange(imid, imid + 1))
        lx = dX.shape[0]
        ly = dY.shape[0]
        filt1 = np.ones((n, 1)) * -1.
        filt2 = np.ones((n, 1))
        filt = np.array(np.hstack((filt1, filt2))).conj().T
        j = int(0)
        for angle in np.arange(0, (360. - dtheta + 0) + dtheta, dtheta):
            if imp_point == 1:
                dlg.progressBar.setValue(angle)

            c = np.zeros((n, n))

            # Rotating building
            d = sc.imrotate(build, angle, 'nearest')
            # d = sc.rotate(build, angle, reshape=False, mode='nearest')
            b = ((build.max() - build.min()) / d.max()) * d + build.min()
            a = b
            if dem.sum() != 0:  # ground heights
                d = sc.imrotate(dsm, angle, 'nearest')
                # d = sc.rotate(dsm, angle, reshape=False, mode='nearest')
                a = ((dsm.max() - dsm.min()) / d.max()) * d + dsm.min()

            #% convolve leading edge filter with domain
            for i in np.arange(1, (n - 1) + 1):
                c[int(i) - 1, :] = np.sum((filt * a[int(i) - 1:i + 1, :]), 0)

            wall = c[dY, dX]  # wall array
            wall = wall[np.where(wall > 2)]  # wall vector
            fai[j] = np.sum(wall) / ((lx * ly) / scale)
            bld = b[dY, dX]  # building array
            bld = bld[np.where(
                bld > 2)]  # building vector: change from 0 to 2  : 20150906
            pai[j] = np.float32(bld.shape[0]) / (lx * ly)
            deg[j] = angle
            if np.float32(bld.shape[0]) == 0:
                zH[j] = 0
                zHmax[j] = 0
                zH_sd[j] = 0
            else:
                zH[j] = bld.sum() / np.float32(bld.shape[0])
                zHmax[j] = bld.max()
                zH_sd[j] = bld.std()

            if angle == 0:
                test = wall

            j += 1

        fai_all = np.mean(fai)

        immorphresult = {
            'fai': fai,
            'pai': pai,
            'zH': zH,
            'deg': deg,
            'zHmax': zHmax,
            'zH_sd': zH_sd,
            'pai_all': pai_all,
            'zH_all': zH_all,
            'zHmax_all': zHmax_all,
            'zH_sd_all': zH_sd_all,
            'fai_all': fai_all,
            'test': test
        }

        return immorphresult

    except Exception:
        exc_type, exc_obj, tb = sys.exc_info()
        f = tb.tb_frame
        lineno = tb.tb_lineno
        filename = f.f_code.co_filename
        linecache.checkcache(filename)
        line = linecache.getline(filename, lineno, f.f_globals)
        errorstring = 'EXCEPTION IN {}, \nLINE {} "{}" \nERROR MESSAGE: {}'.format(
            filename, lineno, line.strip(), exc_obj)
        QgsMessageLog.logMessage(errorstring, level=QgsMessageLog.CRITICAL)
Exemplo n.º 35
0
def random_rotate_image(image):
    random_angle = np.random.uniform(low=-10.0, hight=10.0)
    return misc.imrotate(image, angle=random_angle)
Exemplo n.º 36
0
def random_rotate_image(image):
    angle = np.random.normal(0.0, 8.0)
    return misc.imrotate(image, angle, 'bicubic')
Exemplo n.º 37
0
def random_rotate_image(image):

    logger.info(msg="random_rotate_image called")
    angle = np.random.uniform(low=-10.0, high=10.0)
    return misc.imrotate(image, angle, 'bicubic')
Exemplo n.º 38
0
def FourierTrafoF(I, typeflag):
"""
    # Input:     - I: A 2D image
    #            - typeflag: Struct of logicals to permit extracting features
    #              based on desired characteristics:
    #                   + typeflag.global: all features
    #                   + typeflag.transform: all features
    #                   + typeflag.moments: only features based on moments
    #                   + typeflag.corr: only features based on correlation
    #              default: all features are being extracted
    #              For more information see README.txt
    #
    #
    # Output:    - Out: A (1x300) vector containing 300 metrics calculated from
    #                   the Fourier transform of an image
"""
    # ************************************************************************
    # Implemented for MRI feature extraction by the Department of Diagnostic
    # and Interventional Radiology, University Hospital of Tuebingen, Germany
    # and the Institute of Signal Processing and System Theory University of
    # Stuttgart, Germany. Last modified: November 2016
    #
    # This implementation is part of ImFEATbox, a toolbox for image feature
    # extraction and analysis. Available online at:
    # https://github.com/annikaliebgott/ImFEATbox
    #
    # Contact: [email protected]
    # ************************************************************************

    if 'typeflag' not in globals():
        typeflag = dict()
        typeflag['global'] = True
        typeflag['transform'] = True
        typeflag['moments'] = True
        typeflag['corr'] = True


    # change image to a square matrix by means of zero padding
    length_x, length_y = np.shape(I)

    if (length_x < length_y):
        I = padarray(I, [( length_y - length_x ),0],'post')
    elif (length_x > length_y):
        I = padarray(I, [0, ( length_x - length_y )],'post')
    elif (length_x == length_y):
        I = double( I )

    #reservation for variables
    feat_corr = np.zeros(90)
    zero_sum = np.zeros(3)
    zero_sum1 = np.zeros(3)
    zero_sum2 = np.zeros(3)
    zero_sum3 = np.zeros(3)
    zero_sum4 = np.zeros(3)
    power_total = np.zeros(3)
    power_total1 = np.zeros(3)
    power_total2 = np.zeros(3)
    power_total3 = np.zeros(3)
    power_total4 = np.zeros(3)
    m_2_M = np.zeros(3)
    m_4_M = np.zeros(3)
    m_2_M1 = np.zeros(3)
    m_4_M1 = np.zeros(3)
    m_2_M2 = np.zeros(3)
    m_4_M2 = np.zeros(3)
    m_2_M3 = np.zeros(3)
    m_4_M3 = np.zeros(3)
    m_2_M4 = np.zeros(3)
    m_4_M4 = np.zeros(3)
    sd_M = np.zeros(3)
    sd_M1 = np.zeros(3)
    sd_M2 = np.zeros(3)
    sd_M3 = np.zeros(3)
    sd_M4 = np.zeros(3)
    sd_F = np.zeros(3)
    sd_F1 = np.zeros(3)
    sd_F2 = np.zeros(3)
    sd_F3 = np.zeros(3)
    sd_F4 = np.zeros(3)
    r_M = np.zeros(3)
    r_M1 = np.zeros(3)
    r_M2 = np.zeros(3)
    r_M3 = np.zeros(3)
    r_M4 = np.zeros(3)
    r_F = np.zeros(3)
    r_F1 = np.zeros(3)
    r_F2 = np.zeros(3)
    r_F3 = np.zeros(3)
    r_F4 = np.zeros(3)
    tr_M = np.zeros(3)
    tr_M1 = np.zeros(3)
    tr_M2 = np.zeros(3)
    tr_M3 = np.zeros(3)
    tr_M4 = np.zeros(3)
    tr_F = np.zeros(3)
    tr_F1 = np.zeros(3)
    tr_F2 = np.zeros(3)
    tr_F3 = np.zeros(3)
    tr_F4 = np.zeros(3)
    max_eig_M = np.zeros(3)
    max_eig_M1 = np.zeros(3)
    max_eig_M2 = np.zeros(3)
    max_eig_M3 = np.zeros(3)
    max_eig_M4 = np.zeros(3)
    max_eig_F = np.zeros(3)
    max_eig_F1 = np.zeros(3)
    max_eig_F2 = np.zeros(3)
    max_eig_F3 = np.zeros(3)
    max_eig_F4 = np.zeros(3)
    mean_eig_M = np.zeros(3)
    mean_eig_M1 = np.zeros(3)
    mean_eig_M2 = np.zeros(3)
    mean_eig_M3 = np.zeros(3)
    mean_eig_M4 = np.zeros(3)
    mean_eig_F = np.zeros(3)
    mean_eig_F1 = np.zeros(3)
    mean_eig_F2 = np.zeros(3)
    mean_eig_F3 = np.zeros(3)
    mean_eig_F4 = np.zeros(3)

    ## calculate transformation for different orientations

    for z in range(0, 3):

        if z == 1:
            # rotate image by 90 degree
            I = imrotate(I,90, interp='bilinear')   #'bicubic'
        elif z == 2:
            # rotate image by -90 degree
            I = imrotate(I,-180, interp='bilinear')

        # deterimie the Fourier - Transform
        F = np.fft.fft2(I)

        # create translation invariance
        M = np.abs(np.power(F,2))

        # break the image into sectors

        # get the center of the image
        half = np.max(np.shape(I))/2

        I1 = I[:half, :half)
        I2 = I[:half,half+1:np.max(np.shape(I)))
        I3 = I[half+1:np.max(np.shape(I)), :half)
        I4 = I[half+1:np.max(np.shape(I)), half+1:np.max(np.shape(I)))

        # Fourier Transform
        F1 = np.fft.fft2(I1)
        F2 = np.fft.fft2(I2)
        F3 = np.fft.fft2(I3)
        F4 = np.fft.fft2(I4)

        # create translation invariance
        M1 = np.abs(np.power(F1,2))
        M2 = np.abs(np.power(F2,2))
        M3 = np.abs(np.power(F3,2))
        M4 = np.abs(np.power(F4,2))

        ## extract features

        # determine the moments
        if (typeflag.moments || typeflag.global || typeflag.transform)
            m_2_M[z] = np.mean(moment(M,2))
            m_4_M[z] = np.mean(moment(M,4))
            m_2_M1[z] = np.mean(moment(M1,2))
            m_4_M1[z] = np.mean(moment(M1,4))
            m_2_M2[z] = np.mean(moment(M2,2))
            m_4_M2[z] = np.mean(moment(M2,4))
            m_2_M3[z] = np.mean(moment(M3,2))
            m_4_M3[z] = np.mean(moment(M3,4))
            m_2_M4[z] = np.mean(moment(M4,2))
            m_4_M4[z] = np.mean(moment(M4,4))
        end

        if (typeflag['global'] || typeflag['transform']):
            # standard derivation
            sd_M[z] = np.std(M, ddof=1)
            sd_M1[z] = np.std(M1, ddof=1)
            sd_M2[z] = np.std(M2, ddof=1)
            sd_M3[z] = np.std(M3, ddof=1)
            sd_M4[z] = np.std(M4, ddof=1)

            sd_F[z] = np.std(F, ddof=1)
            sd_F1[z] = np.std(F1, ddof=1)
            sd_F2[z] = np.std(F2, ddof=1)
            sd_F3[z] = np.std(F3, ddof=1)
            sd_F4[z] = np.std(F4, ddof=1)

            # rank
            r_M[z] = np.linalg.matrix_rank(M)
            r_M1[z] = np.linalg.matrix_rank(M1)
            r_M2[z] = np.linalg.matrix_rank(M2)
            r_M3[z] = np.linalg.matrix_rank(M3)
            r_M4[z] = np.linalg.matrix_rank(M4)

            r_F[z] = np.linalg.matrix_rank(F)
            r_F1[z] = np.linalg.matrix_rank(F1)
            r_F2[z] = np.linalg.matrix_rank(F2)
            r_F3[z] = np.linalg.matrix_rank(F3)
            r_F4[z] = np.linalg.matrix_rank(F4)

            # trace
            tr_M[z] = np.trace(M)
            tr_M1[z] = np.trace(M1)
            tr_M2[z] = np.trace(M2)
            tr_M3[z] = np.trace(M3)
            tr_M4[z] = np.trace(M4)

            tr_F[z] = np.trace(F)
            tr_F1[z] = np.trace(F1)
            tr_F2[z] = np.trace(F2)
            tr_F3[z] = np.trace(F3)
            tr_F4[z] = np.trace(F4)

            # largest/smallest eingenvalue
            eig_M = np.linalg.eig(M)[0]
            eig_M1 = np.linalg.eig(M1)[0]
            eig_M2 = np.linalg.eig(M2)[0]
            eig_M3 = np.linalg.eig(M3)[0]
            eig_M4 = np.linalg.eig(M4)[0]

            eig_F = np.linalg.eig(F)[0]
            eig_F1 = np.linalg.eig(F1)[0]
            eig_F2 = np.linalg.eig(F2)[0]
            eig_F3 = np.linalg.eig(F3)[0]
            eig_F4 = np.linalg.eig(F4)[0]

            max_eig_M[z] = np.max(eig_M)
            max_eig_M1[z] = np.max(eig_M1)
            max_eig_M2[z] = np.max(eig_M2)
            max_eig_M3[z] = np.max(eig_M3)
            max_eig_M4[z] = np.max(eig_M4)

            max_eig_F[z] = np.max(eig_F)
            max_eig_F1[z] = np.max(eig_F1)
            max_eig_F2[z] = np.max(eig_F2)
            max_eig_F3[z] = np.max(eig_F3)
            max_eig_F4[z] = np.max(eig_F4)

            # np.mean
            mean_eig_M[z] = np.mean(eig_M)
            mean_eig_M1[z] = np.mean(eig_M1)
            mean_eig_M2[z] = np.mean(eig_M2)
            mean_eig_M3[z] = np.mean(eig_M3)
            mean_eig_M4[z] = np.mean(eig_M4)

            mean_eig_F[z] = np.mean(eig_F)
            mean_eig_F1[z] = np.mean(eig_F1)
            mean_eig_F2[z] = np.mean(eig_F2)
            mean_eig_F3[z] = np.mean(eig_F3)
            mean_eig_F4[z] = np.mean(eig_F4)

        # correlation
        if (typeflag['corr'] or typeflag['transform'] or typeflag['global']):
            corr12 = np.corrcoef(M1,M2)
            corr13 = np.corrcoef(M1,M3)
            corr14 = np.corrcoef(M1,M4)
            corr32 = np.corrcoef(M3,M2)
            corr42 = np.corrcoef(M4,M2)
            corr34 = np.corrcoef(M4,M3)
            feat_corr((z-1)*30+1:(z-1)*30+30) = [np.std(corr12, ddof=1), np.mean(corr12),
                np.max(corr12(:)), np.min(corr12(:)), np.count_nonzero(corr12),
                np.std(corr13, ddof=1), np.mean(corr13), np.max(corr13(:)), np.min(corr13(:)), np.count_nonzero(corr13),
                np.std(corr14, ddof=1), np.mean(corr14), np.max(corr14(:)), np.min(corr14(:)), np.count_nonzero(corr14),
                np.std(corr32, ddof=1), np.mean(corr32), np.max(corr32(:)), np.min(corr32(:)), np.count_nonzero(corr32),
                np.std(corr42, ddof=1), np.mean(corr42), np.max(corr42(:)), np.min(corr42(:)), np.count_nonzero(corr42),
                np.std(corr34, ddof=1), np.mean(corr34), np.max(corr34(:)), np.min(corr34(:)), np.count_nonzero(corr34)]

        if (typeflag['global'] or typeflag['transform']):
            # count zero points
            zero_sum[z] = np.count_nonzero(F)
            zero_sum1[z] = np.count_nonzero(F1)
            zero_sum2[z] = np.count_nonzero(F2)
            zero_sum3[z] = np.count_nonzero(F3)
            zero_sum4[z] = np.count_nonzero(F4)


            # compute the DFT power

            # Transform length
            n = (2**ceil(log2(np.max(np.shape(I)))))**2

            # Power of the DFT
            power = np.power(F,conj(F))
            power1 = np.power(F1,conj(F1))
            power2 = np.power(F2,conj(F2))
            power3 = np.power(F3,conj(F3))
            power4 = np.power(F4,conj(F4))
            power_total[z] = np.sum( np.sum(power) ) /  (n*n)
            power_total1[z] = np.sum( np.sum(power1) ) / ( n*n )
            power_total2[z] = np.sum( np.sum(power2) ) / ( n*n )
            power_total3[z] = np.sum( np.sum(power3) ) / ( n*n )
            power_total4[z] = np.sum( np.sum(power4) ) / ( n*n )
Exemplo n.º 39
0
def Rotate(image, angle):
    img = scm.imrotate(image, angle)
    # cv.imshow('image', img)
    # cv.waitKey(0)
    # cv.destroyAllWindows()
    return img
Exemplo n.º 40
0
    # A[1,1] = 23
    inc = 0
    for row in range(A.shape[0]):
        # print(inc, '\n')
        for col in range(detectorNum):
            # print('\t', inc+row+col)
            A[row,inc+col] = 1
        inc += detectorNum

    # print(A)

    A_Origin = A
    for proj in range(36):
        # A_Rotated = im.imrotate(A, int(t_Argv[1]))
        A_Rotated = im.imrotate(A_Origin, (proj+1)*-10)
        for row in range(A_Rotated.shape[0]):
            for col in range(A_Rotated.shape[1]):
                if A_Rotated[row, col] != 0:
                    A_Rotated[row, col] = 1
        A = np.vstack((A, A_Rotated))

    # print(A.shape)
    # print(A_Rotated.shape)
    # A_Joint = np.vstack((A, A_Rotated))

    # fig, (ax1, ax2) = plt.subplots(2, 1)
    # ax1.imshow(A)
    # for i in range(A_Rotated.shape[0]):
    #     for j in range(A_Rotated.shape[1]):
    #         text = ax1.text(j, i, A[i, j], ha="center", va="center", color="w")
Exemplo n.º 41
0
def add_mustache(frame):
    # Create greyscale image from the video feed

    image = imutils.resize(frame, width=500)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    ih, iw, _ = image.shape

    # detect faces in the grayscale image
    rects = detector(gray, 1)
    #roi_color = frame #frame[y:y+h, x:x+w]
    print(len(rects), "RECTS")
    # loop over the face detections
    for (i, rect) in enumerate(rects):
        # determine the facial landmarks for the face region, then
        # convert the facial landmark (x, y)-coordinates to a NumPy
        # array
        shape = predictor(gray, rect)
        shape = face_utils.shape_to_np(shape)

        # convert dlib's rectangle to a OpenCV-style bounding box
        # [i.e., (x, y, w, h)], then draw the face bounding box
        (x, y, w, h) = face_utils.rect_to_bb(rect)

        #roi_gray = gray[y:y+h, x:x+w]
        #roi_color = frame[y:y+h, x:x+w]
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

        # show the face number
        #cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y - 10),
        #    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

        # loop over the (x, y)-coordinates for the facial landmarks
        # and draw them on the image
        for (x, y) in shape:
            cv2.circle(image, (x, y), 1, (0, 0, 255), -1)

        #name = 'nose'
        #i,j = face_utils.FACIAL_LANDMARKS_IDXS[name]
        #for (i, name) in enumerate(FACIAL_LANDMARKS_IDXS.keys()):
        i = 0
        name = 'mouth'
        if 1:
            # grab the (x, y)-coordinates associated with the
            # face landmark
            (j, k) = FACIAL_LANDMARKS_IDXS[name]
            pts = shape[j:k]

            # check if are supposed to draw the jawline
            if name == "jaw":
                # since the jawline is a non-enclosed facial region,
                # just draw lines between the (x, y)-coordinates
                for l in range(1, len(pts)):
                    ptA = tuple(pts[l - 1])
                    ptB = tuple(pts[l])
                    #cv2.line(overlay, ptA, ptB, colors[i], 2)
                    cv2.line(image, ptA, ptB, colors[i], 2)

            # otherwise, compute the convex hull of the facial
            # landmark coordinates points and display it
            else:
                hull = cv2.convexHull(pts)
                #cv2.drawContours(overlay, [hull], -1, colors[i], -1)
                #cv2.drawContours(image, [hull], -1, colors[i], -1)
                if name == 'mouth':
                    nx = np.min(pts[:, 0])
                    ny = np.min(pts[:, 1])
                    nw = np.max(pts[:, 0]) - np.min(pts[:, 0]) + 1
                    nh = np.max(pts[:, 1]) - np.min(pts[:, 1]) + 1

                    #cv2.rectangle(image,(nx,ny),(nx+nw,ny+nh),(255,0,0),2)
                    cv2.rectangle(image, (nx, ny), (nx + nw, ny + nh),
                                  colors[i])

                    mustacheWidth = 15 * nw
                    mustacheHeight = mustacheWidth * origMustacheHeight / origMustacheWidth

                    # Center the mustache on the bottom of the nose
                    x1 = nx - int(mustacheWidth / 4.)
                    x2 = nx + nw + int(mustacheWidth / 4.)
                    y1 = ny + nh - int(mustacheHeight / 4.)
                    y2 = ny + nh + int((mustacheHeight / 2.))

                    rx2, ry2 = pts[np.argmax(pts[:, 0]), :]
                    rx1, ry1 = pts[np.argmin(pts[:, 0]), :]

                    opp = ry2 - ry1
                    adj = rx2 - rx1
                angle = -np.rad2deg(np.arctan(opp / float(adj)))

                # Check for clipping
                if x1 < 0:
                    x1 = 0
                if y1 < 0:
                    y1 = 0
                if x2 > iw:
                    x2 = iw
                if y2 > ih:
                    y2 = ih

                # Re-calculate the width and height of the mustache image
                mustacheWidth = x2 - x1
                mustacheHeight = y2 - y1

                # Re-size the original image and the masks to the mustache sizes
                # calcualted above
                mustache = cv2.resize(imgMustache,
                                      (mustacheWidth, mustacheHeight),
                                      interpolation=cv2.INTER_AREA)
                mustache_rot = imrotate(mustache, angle)
                orig_mask_rot = imrotate(orig_mask, angle)
                orig_mask_inv_rot = cv2.bitwise_not(orig_mask_rot)
                mask = cv2.resize(orig_mask_rot,
                                  (mustacheWidth, mustacheHeight),
                                  interpolation=cv2.INTER_AREA)
                mask_inv = cv2.resize(orig_mask_inv_rot,
                                      (mustacheWidth, mustacheHeight),
                                      interpolation=cv2.INTER_AREA)

                # take ROI for mustache from background equal to size of mustache image
                roi = image[y1:y2, x1:x2]

                ### roi_bg contains the original image only where the mustache is not
                ## in the region that is the size of the mustache.

                roi_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
                ## roi_fg contains the image of the mustache only where the mustache is
                roi_fg = cv2.bitwise_and(mustache, mustache, mask=mask)

                ## join the roi_bg and roi_fg
                dst = cv2.add(roi_bg, roi_fg)

                ## place the joined image, saved to dst back over the original image
                image[y1:y2, x1:x2] = dst
    return image
def crop_image(img):
    """ Crops the raw raspberry pi image to the office lab area """
    img = imrotate(img, -4.5)
    img = img[592:1243 + 592, 736:2354 + 736]
    return img
Exemplo n.º 43
0
from scipy import misc

misc.imshow(misc.imrotate(misc.face(), 45))
Exemplo n.º 44
0
points_anms = anms(dst, top=800)
points_anms = np.delete(points_anms, [2, 3], 1)
filtered_coords = points_anms
plt.figure(figsize=(25, 20))
plt.gray()
plt.imshow(I1, cmap='gray')
plt.plot([p[1] for p in filtered_coords], [p[0] for p in filtered_coords],
         '*',
         color='r',
         markersize=4)
plt.axis('off')
plt.show()

# Chess rotation:
I2 = imrotate(
    np.pad(io.imread(path1), ((0, 0), (200, 200), (0, 0)), 'constant'), 30,
    'bilinear')
dst2 = harrisCV(I2)
plotDetections(I2, dst2)

nms2 = nms(dst2, 8)

plotDetections(I2, nms2)
anms2 = anms(dst2, top=800)
anms2 = np.delete(anms2, [2, 3], 1)
filtered_coords2 = anms2
plt.figure(figsize=(25, 20))
plt.gray()
plt.imshow(I2, cmap='gray')
plt.plot([p[1] for p in filtered_coords2], [p[0] for p in filtered_coords2],
         '*',
Exemplo n.º 45
0
def rot90(sample):
    image = imrotate(sample[0], 90)
    return image, sample[1]
Exemplo n.º 46
0
def align_images(img1, img2):
    #
    # Aligns im1 and im2 (translation, scale, rotation) after getting two pairs
    # of points from the user.  In the output of im1 and im2, the two pairs of
    # points will have approximately the same coordinates.
    #

    # get image sizes
    h1, w1, d = img1.shape
    h2, w2, d = img2.shape

    # gets two points from the user
    print(
        'Select two points from each image define rotation, scale, translation'
    )
    plt.imshow(img1, cmap='gray')
    x1, y1 = tuple(zip(*plt.ginput(2)))
    plt.close()
    cx1, cy1 = np.mean(x1), np.mean(y1)
    plt.imshow(img2, cmap='gray')
    x2, y2 = tuple(zip(*plt.ginput(2)))
    plt.close()
    cx2, cy2 = np.mean(x2), np.mean(y2)

    # translate first so that center of ref points is center of image
    tx = int(np.round((w1 / 2 - cx1) * 2))
    img1 = translate_image(img1, tx, axis=1)
    ty = int(np.round((h1 / 2 - cy1) * 2))
    img1 = translate_image(img1, ty, axis=0)
    tx = int(np.round((w2 / 2 - cx2) * 2))
    img2 = translate_image(img2, tx, axis=1)
    ty = int(np.round((h2 / 2 - cy2) * 2))
    img2 = translate_image(img2, ty, axis=0)

    # downscale larger image so that lengths between ref points are the same
    len1 = np.sqrt((y1[1] - y1[0])**2 + (x1[1] - x1[0])**2)
    len2 = np.sqrt((y2[1] - y2[0])**2 + (x2[1] - x2[0])**2)

    dscale = len2 / len1

    if dscale < 1:
        img1 = misc.imresize(img1, dscale, 'bilinear')
    else:
        img2 = misc.imresize(img2, 1 / dscale, 'bilinear')

    # rotate im1 so that angle between points is the same
    theta1 = np.arctan2(-(y1[1] - y1[0]), x1[1] - x1[0])
    theta2 = np.arctan2(-(y2[1] - y2[0]), x2[1] - x2[0])
    dtheta = theta2 - theta1
    img1 = misc.imrotate(img1, dtheta * 180 / np.pi,
                         'bilinear')  # imrotate uses degree units
    img1 = norm_image(
        img1)  # imrotate seems to put the picture back to the interval [0-255]

    # Crop images (on both sides of border) to be same height and width
    h1, w1, d = img1.shape
    h2, w2, d = img2.shape

    minw = min(w1, w2)
    brd = (max(w1, w2) - minw) / 2

    if minw == w1:
        img2 = img2[:, int(np.ceil(brd)):-int(np.floor(brd))]
    else:
        img1 = img1[:, int(np.ceil(brd)):-int(np.floor(brd))]

    minh = min(h1, h2)
    brd = (max(h1, h2) - minh) / 2

    if minh == h1:
        img2 = img2[int(np.ceil(brd)):-int(np.floor(brd)), :]
    else:
        img1 = img1[int(np.ceil(brd)):-int(np.floor(brd)), :]

    return img1, img2
Exemplo n.º 47
0
    def set_image(self,
                  image,
                  filter='',
                  pgc=0,
                  index=-2,
                  only_image=False,
                  garbage=False,
                  im_folder=None,
                  flag=None,
                  inc=None):

        self.filter = filter

        if im_folder != None:
            self.im_folder = im_folder
        if flag != None:
            self.flag = flag

        if inc != None:
            self.inc = inc

        if self.filter == 'gri':
            inv = not self.invert
        else:
            inv = self.invert

        image = scimisc.imrotate(image, self.angle, interp='bilinear')

        if inv:
            self.image = 255 - image
        else:
            self.image = image

        self.ax.imshow(self.image)

        if not only_image:
            self.pgc = pgc
            self.index = index + 1
            self.garbage = garbage

        if self.filter in ['g', 'r', 'i', '']:
            self.filter_txt.set_text(self.filter)

        if self.filter == 'gri':
            self.filter_txt.set_text('gri')

        if self.pgc > 0:
            self.pgc_txt.set_text('pgc: ' + str(self.pgc))
        else:
            self.pgc_txt.set_text('')

        if self.index > -1 and self.flag == -1:
            self.index_txt.set_text(str(self.index) + ' ***')
        elif self.index > -1:
            self.index_txt.set_text(str(self.index))
        else:
            self.index_txt.set_text('')

        if self.index == 5:
            if not self.garbage:
                self.index_txt.set_text('??')
            else:
                self.index_txt.set_text('!? x')

        if self.inc >= 0 and self.flag == -1:
            self.inc_txt.set_text(str(self.inc) + r'$^o$')
        elif self.inc >= 0 and self.flag >= 0:
            self.inc_txt.set_text('(' + str(self.inc) + r'$^o )$')
        else:
            self.inc_txt.set_text('')

        if self.garbage:
            self.pgc_txt.set_color('red')
            self.index_txt.set_color('red')
            self.index_txt.set_text(str(self.index) + ' X')
            self.filter_txt.set_color('red')
            self.inc_txt.set_color('red')
            if self.index == 5:
                self.index_txt.set_text('!? x')
        else:
            self.pgc_txt.set_color('green')
            self.index_txt.set_color('green')
            if self.index > -1 and self.flag == -1:
                self.index_txt.set_text(str(self.index) + ' ***')
            elif self.index > -1:
                self.index_txt.set_text(str(self.index))
            self.filter_txt.set_color('green')
            self.inc_txt.set_color('green')
            if self.index == 5:
                self.index_txt.set_text('??')
Exemplo n.º 48
0
f = plt.figure()
ax = f.add_subplot(2, 2, 1)
ax.set_title("theta = %d + %s erosion" % (text_thresh, structure.shape))
ax.imshow(img)

ft = np.fft.fft2(img)
amp, phase = (np.abs(ft), np.angle(ft))

amp_thresh = 8
amp[np.log(amp) < amp_thresh] = 0

ax = f.add_subplot(2, 2, 2)
ax.set_title("log((shift(amp) >= %d) + 1)" % (amp_thresh))
ax.imshow(np.log(np.fft.fftshift(amp + 1)))

from skimage.transform import hough_line, hough_line_peaks
hough, angles, dists = hough_line(amp)
peak_vals, peak_angles, peak_dists = hough_line_peaks(hough, angles, dists)
peak_angles_deg = np.rad2deg(peak_angles)

ax = f.add_subplot(2, 2, 3)
ax.imshow(hough)
ax.set_title("Hough transform of amplitude")

ax = f.add_subplot(2, 2, 4)
ax.set_title("img rotated by %f°" % peak_angles_deg[0])
ax.imshow(misc.imrotate(img, peak_angles_deg[0]))

plt.show()
Exemplo n.º 49
0
def rotateImg(img, angle, mask_in=None):
    if angle == 0:
        return img, mask_in

    # grab the dimensions of the image
    (h, w) = img.shape[:2]

    max_dim = int(max(h, w) * 2.0)

    # Get a blank array the max paste size
    if len(img.shape) > 2:
        buffer_roi = np.zeros([max_dim, max_dim, img.shape[2]], dtype=np.uint8)
    else:
        buffer_roi = np.zeros([max_dim, max_dim], dtype=np.uint8)

    if mask_in is not None:
        buffer_roi_mask = np.zeros([max_dim, max_dim], dtype=np.uint8)

    center_rotate_roi = int(max_dim / 2.0)
    paste_left = int(img.shape[1] / 2.0)
    paste_right = img.shape[1] - paste_left
    paste_top = int(img.shape[0] / 2.0)
    paste_bottom = img.shape[0] - paste_top

    # Copy the image into the center of this
    buffer_roi[(center_rotate_roi - paste_top):(center_rotate_roi +
                                                paste_bottom),
               (center_rotate_roi - paste_left):(center_rotate_roi +
                                                 paste_right)] = img
    if mask_in is not None:
        buffer_roi_mask[(center_rotate_roi - paste_top):(center_rotate_roi +
                                                         paste_bottom),
                        (center_rotate_roi -
                         paste_left):(center_rotate_roi +
                                      paste_right)] = mask_in

    # showAndWait('buffer_roi', buffer_roi)

    rotated = misc.imrotate(buffer_roi, angle)
    if mask_in is not None:
        rotated_mask = misc.imrotate(buffer_roi_mask, angle)

    if len(img.shape) > 2:
        paste_grey = cv2.cvtColor(rotated, cv2.COLOR_BGR2GRAY)
    else:
        paste_grey = rotated

    # showAndWait('paste_grey', paste_grey)
    # cv2.imwrite('/media/dcofer/Ubuntu_Data/drone_images/paste_grey.png', paste_grey)

    ret, rotated_mask_img = cv2.threshold(paste_grey, 5, 255,
                                          cv2.THRESH_BINARY)

    # showAndWait('mask', rotated_mask)
    # cv2.imwrite('/media/dcofer/Ubuntu_Data/drone_images/rotated_mask.png', rotated_mask)

    where = np.array(np.where(rotated_mask_img))
    # np.savetxt('/media/dcofer/Ubuntu_Data/drone_images/fuckhead.csv', np.transpose(where))

    x1, y1 = np.amin(where, axis=1)
    x2, y2 = np.amax(where, axis=1)

    out_image = rotated[x1:x2, y1:y2]
    if mask_in is not None:
        out_mask = rotated_mask[x1:x2, y1:y2]
        ret, out_mask = cv2.threshold(out_mask, 3, 255, cv2.THRESH_BINARY)
    else:
        out_mask = None

    # showAndWait('out_image', out_image)
    # cv2.imwrite('/media/dcofer/Ubuntu_Data/drone_images/out_image.png', out_image)

    # return the rotated image
    return out_image, out_mask
Exemplo n.º 50
0
def random_rotate_image(image):
    angle = np.random.uniform(low=-10.0, high=10.0)
    return misc.imrotate(image, angle, 'bicubic')
Exemplo n.º 51
0
def rotate(img, angle, interpolation='cubic'):

    imgnew = imrotate(img, angle, interp=interpolation)

    return imgnew
Exemplo n.º 52
0
def random_rotate_image(image):
    """rotate +-10 degrees randomly"""
    angle = np.random.uniform(low=-10.0, high=10.0)
    return misc.imrotate(image, angle, 'bicubic')
Exemplo n.º 53
0
def get_data(img_width=30,
             img_height=30,
             file_path='Data_Train',
             class_names=None,
             validation_ratio=0.2,
             nb_samples=[1000, 1000, 1000],
             data_augmentation=False):

    if class_names is None:  # testing, no answer
        img_path = [str(a) + '.bmp' for a in range(1, 601)]
        print(img_path)
        df = pd.DataFrame(index=range(len(img_path)),
                          columns=range(img_height * img_width))
        for file, i in zip(img_path, range(len(img_path))):
            img = misc.imread(file_path + '/' + file).flatten()
            df.iloc[i] = img
        return df

    train_df = pd.DataFrame(columns=range(img_height * img_width))
    train_df['class'] = 0
    augmentation_df = pd.DataFrame(columns=range(img_height * img_width))
    augmentation_df['class'] = 0
    for i in range(len(class_names)):
        path = file_path + '/' + class_names[i] + '/'
        print(path)
        class_img_files = os.listdir(path)[:nb_samples[i]]

        df = pd.DataFrame(index=range(len(class_img_files)),
                          columns=range(img_height * img_width))
        aug_df = pd.DataFrame(columns=range(img_height * img_width))
        for file, j in zip(class_img_files, range(len(class_img_files))):
            img_path = path + file
            img = misc.imread(img_path).flatten()
            df.iloc[j] = img
            if data_augmentation:
                img = img.reshape((img_height, img_width))
                # rotate
                angle = [random.randint(10, 20),
                         random.randint(-20, -10)][random.randint(0, 1)]
                img1 = misc.imrotate(img, angle)
                img1 = img1.flatten()
                aug_df = aug_df.append(pd.Series(img1), ignore_index=True)
                # flip
                img2 = np.fliplr(img)
                img2 = img2.flatten()
                aug_df = aug_df.append(pd.Series(img2), ignore_index=True)
        df['class'] = class_names[i]
        aug_df['class'] = class_names[i]
        train_df = train_df.append(df)
        augmentation_df = augmentation_df.append(aug_df)

    np.random.seed(0)
    train_df = train_df.sample(frac=1)
    train_df, val_df = train_df.iloc[:int(
        len(train_df) * (1 - validation_ratio)
    )], train_df.iloc[int(len(train_df) * (1 - validation_ratio)):]
    train_df = train_df.append(augmentation_df)

    train_class = train_df['class']
    train_class.index = range(len(train_class))
    train_df = train_df.drop('class', axis=1)
    train_df.index = range(len(train_df))
    val_class = val_df['class']
    val_class.index = range(len(val_class))
    val_df = val_df.drop('class', axis=1)
    val_df.index = range(len(val_df))

    return train_df, train_class, val_df, val_class
Exemplo n.º 54
0
def load_train_data(img_dir, tag_path, prepro_dir, vocab_path, shuffle_time=1):
    vocab = collections.defaultdict(int)
    used_vocab = collections.defaultdict(int)
    raw_tags = []
    attrib_tags = []
    img_feat = []
    with open(tag_path, 'r') as f:
        for ridx, row in enumerate(csv.reader(f)):
            tags = row[1].split('\t')
            for t in tags:
                tag = t.split(':')[0].strip()
                for w in tag.split():
                    vocab[w] += 1

    with open(tag_path, 'r') as f:
        for ridx, row in enumerate(csv.reader(f)):
            tags = row[1].split('\t')
            c_tags = []
            k_tags = {}
            length = 0
            has_attrib = False
            attrib = {'eyes': '<UNK>', 'hair': '<UNK>'}
            for t in tags:
                if t != '':
                    tag = t.split(':')[0].strip()
                    s_tag = tag.split()
                    if len(s_tag) > 2:
                        continue
                    score = int(t.split(':')[1].strip())
                    C_flag = False
                    B_flag = False
                    for w in s_tag:
                        if w == 'hair' and (s_tag[0] == 'long'
                                            or s_tag[0] == 'short'):
                            C_flag = True
                            break
                        if w == 'eyes' or w == 'hair':
                            if attrib[w] != '<UNK>':
                                B_flag = True
                                break
                            attrib[w] = s_tag[0]
                            has_attrib = True
                            score = float('Inf')
                        if vocab[w] < vocab_limit:
                            C_flag = True
                            break
                    if C_flag:
                        continue
                    if B_flag:
                        break
                    length += len(s_tag)
                    k_tags[tag] = score

            if len(k_tags) == 0 or not has_attrib or B_flag:
                continue
            #Tag文件中所有与头发与眼睛颜色有关的数据都会读取,然后找到与之匹配的图片,将图片进行选择、对折等操作,图片数据都存入img_feat,对应描述数据都存入raw_tags,而描述出现了多少次存入了used_vocab
            a_tags = ' '.join([attrib['eyes'], attrib['hair']])

            for idx, (k, v) in enumerate(
                    sorted(k_tags.items(), key=lambda x: x[1], reverse=True)):
                if idx < topk:
                    c_tags.append(k)
                    for w in k.split():
                        used_vocab[w] += 1

            c_tags = [attrib['eyes'] + ' eyes', attrib['hair'] + ' hair']

            img_path = os.path.join(img_dir, '{}.jpg'.format(ridx))
            feat = misc.imread(img_path)  #读取图片获得与该图片对应的narray
            feat = misc.imresize(feat, [64, 64, 3])  #调整图片大小
            random.shuffle(c_tags)  #打乱c_tags
            raw_tags.append(' '.join(c_tags))
            attrib_tags.append(a_tags)
            img_feat.append(feat)

            m_feat = np.fliplr(feat)
            random.shuffle(c_tags)
            raw_tags.append(' '.join(c_tags))
            attrib_tags.append(a_tags)
            img_feat.append(m_feat)

            feat_p5 = misc.imrotate(feat, 5)
            random.shuffle(c_tags)
            raw_tags.append(' '.join(c_tags))
            attrib_tags.append(a_tags)
            img_feat.append(feat_p5)

            feat_m5 = misc.imrotate(feat, -5)
            random.shuffle(c_tags)
            raw_tags.append(' '.join(c_tags))
            attrib_tags.append(a_tags)
            img_feat.append(feat_m5)

    img_feat = np.array(img_feat)

    vocabulary = []
    for k, v in sorted(used_vocab.items(), key=lambda x: x[1], reverse=True):
        vocabulary.append(k)

    avg_length = sum([len(tags.split()) for tags in raw_tags]) / len(raw_tags)
    max_length = max([len(tags.split()) for tags in raw_tags])
    vocab_processor = VocabularyProcessor(max_document_length=max_length,
                                          vocabulary=vocabulary)
    tags_idx = vocab_processor.transform(raw_tags)
    a_tags_idx = vocab_processor.transform(attrib_tags, 2)

    print("max sentence length: {}".format(max_length))
    print("avg sentence length: {}".format(avg_length))

    cPickle.dump(img_feat, open(os.path.join(prepro_dir, "img_feat.dat"),
                                'wb'))
    cPickle.dump(tags_idx, open(os.path.join(prepro_dir, "tag_ids.dat"), 'wb'))
    cPickle.dump(a_tags_idx,
                 open(os.path.join(prepro_dir, "a_tag_ids.dat"), 'wb'))

    vocab_processor.save(vocab_path)
    return img_feat, tags_idx, a_tags_idx, vocab_processor
Exemplo n.º 55
0
                plt.figure(1)
                plt.imshow(
                    np.concatenate((image[0, ...], target[0, ...]), axis=1))
                plt.show()

                flipped_X = image[:, ::-1, :]
                X.append(flipped_X)
                labels.append(i)

                flipped_y = target[:, ::-1, :]
                y.append(flipped_y)

                for angle in [90, 180, 270]:
                    rotated_X = imrotate(image[0, ...],
                                         angle,
                                         interp='nearest')
                    rotated_X = rotated_X.astype(np.float32)
                    rotated_X /= np.max(rotated_X)
                    rotated_y = np.clip(
                        imrotate(target[0, ...], angle, interp='nearest'), 0,
                        1)

                    X.append(rotated_X[np.newaxis])
                    labels.append(i)
                    y.append(rotated_y[np.newaxis])

                for angle in [90, 180, 270]:
                    rotated_X = imrotate(flipped_X[0, ...],
                                         angle,
                                         interp='nearest')
Exemplo n.º 56
0
         

        if fl == 0 and i == 0:
    
            rtImRec[indIm,:,:,:] = im3C
            yi = np.zeros((1,10));
            yi[0,int(labelFl[0,i])]=1
            yRec = yi
            yiVect = labelFl[0,i]
            yVectRec = yiVect
            
            indIm=indIm+1
            
        if (fl > 0 or i > 0) and (maxMovRec[i] > thresholdMovement) and labelFl[0,i] != 7:
            
            imRandRotated = misc.imrotate(im3C,np.random.randint(360))
            
            rtImRec[indIm,:,:,:] = imRandRotated
            
            yi = np.zeros((1,10));
            yi[0,int(labelFl[0,i])]=1            
            yRec = np.vstack((yRec,yi))
            yiVect = labelFl[0,i]
            yVectRec = np.vstack((yVectRec,yiVect))
            
            indIm=indIm+1
            
            
        if maxMovRec[i] < thresholdMovement:
            print(maxMovRec[i]);print('No movement detected')
Exemplo n.º 57
0
for f in files:
    try:
        img = io.imread(f)
        #img = img_as_uint(img)
        #img.astype('float32', copy=False)
        new_img = imresize(img, (size_image, size_image, 3))
        if random.choice([True, False]):
            if 'English' not in f:  # letters/numbers shouldn't be flipped
                #print (f)
                new_img = np.fliplr(new_img)
        allX[count] = np.array(new_img)
        ally[count] = 0
        count += 1

        angle = 1
        upside_downimage = imrotate(new_img, angle * 90)
        allX[count] = np.array(upside_downimage)
        ally[count] = angle
        count += 1

        angle = 2
        upside_downimage = imrotate(new_img, angle * 90)
        allX[count] = np.array(upside_downimage)
        ally[count] = angle
        count += 1

        angle = 3
        upside_downimage = imrotate(new_img, angle * 90)
        allX[count] = np.array(upside_downimage)
        ally[count] = angle
        count += 1
Exemplo n.º 58
0
def preprocessing(preproc_dir, img_dir, tag_path, eye_map, hair_map):

    attrib_tags = []
    img_feat = []
    img_size = 96
    resieze = int(96 * 1.15)

    with open(tag_path, 'r') as f:
        for idx, row in enumerate(csv.reader(f)):

            tags = row[1].split('\t')
            hair = 'unk'
            eyes = 'unk'
            has_hair = False
            has_eye = False
            skip_hair = False
            skip_eye = False
            skip = False

            for t in tags:
                if t != '':
                    tag = t.split(':')[0].strip()

                    if tag == 'bicolored eyes':
                        print(tag)
                        skip = True
                        break

                    if tag in eye_map:

                        if has_eye:
                            skip_hair = True

                        eyes = tag
                        has_eye = True

                    elif tag in hair_map:
                        if has_hair:
                            skip_eye = True

                        hair = tag
                        has_hair = True

            if skip_hair:
                hair = 'unk'

            if skip_eye:
                eyes = 'unk'

            if eyes == 'unk' or hair == 'unk':
                skip = True

            if skip:
                continue

            hair_idx = hair_map[hair]
            eyes_idx = eye_map[eyes]

            img_path = os.path.join(img_dir, '{}.jpg'.format(idx))
            feat = misc.imread(img_path)
            feat = misc.imresize(feat, [img_size, img_size, 3])
            attrib_tags.append([hair_idx, eyes_idx])
            img_feat.append(feat)

            m_feat = np.fliplr(feat)
            attrib_tags.append([hair_idx, eyes_idx])
            img_feat.append(m_feat)

            feat_p5 = misc.imrotate(feat, 5)
            feat_p5 = misc.imresize(feat_p5, [resieze, resieze, 3])
            feat_p5 = crop_center(feat_p5, img_size, img_size)

            attrib_tags.append([hair_idx, eyes_idx])
            img_feat.append(feat_p5)

            feat_m5 = misc.imrotate(feat, -5)
            feat_m5 = misc.imresize(feat_m5, [resieze, resieze, 3])
            feat_m5 = crop_center(feat_m5, img_size, img_size)

            attrib_tags.append([hair_idx, eyes_idx])
            img_feat.append(feat_m5)

    img_feat = np.array(img_feat)

    pickle.dump(img_feat,
                open(os.path.join(preproc_dir, "img_feat_96.dat"), 'wb'))
    pickle.dump(attrib_tags, open(os.path.join(preproc_dir, "tags.dat"), 'wb'))

    return img_feat, attrib_tags
Exemplo n.º 59
0
def random_rotate_image(image):
    angle = np.random.uniform(low=-10.0, high=10.0)
    return misc.imrotate(image, angle, 'bicubic')
Exemplo n.º 60
0
 def random_rotate_image_func(image, angle):
     return misc.imrotate(image, angle, 'bicubic')