def loadData(self): self.rawData = io.imread(self.fileName, plugin='tifffile') self.rawData = cv2.merge((self.rawData[:, :, 0].T, self.rawData[:, :, 1].T, self.rawData[:, :, 2].T)) self.cData = self.rawData.copy() self.grayData = self.rawData.copy() self.grayData = color.rgb2gray(self.rawData) self.hsvData = color.rgb2hsv(self.rawData) # self.grayData = self.grayData.convert('LA') # self.grayData = self.grayData.transpose(method=PIL.Image.TRANSPOSE) self.grayData = transform.rotate(self.grayData, angle=0) self.cData = transform.rotate(self.cData, angle=0) self.hsvData = transform.rotate(self.hsvData, angle=0) self.b = self.cData[:, :, 0] self.g = self.cData[:, :, 1] self.r = self.cData[:, :, 2] self.v = self.hsvData[:, :, 0] self.s = self.hsvData[:, :, 1] self.h = self.hsvData[:, :, 2] self.colorDict = {'RGB': self.cData, 'GRAY': self.grayData, 'B': self.b, 'G': self.g, 'R': self.r, 'HSV': self.hsvData, 'H': self.h, 'S': self.s, 'V': self.v}
def augmentation(image, imageB, org_width=160,org_height=224, width=190, height=262): max_angle=20 image=cv2.resize(image,(height,width)) imageB=cv2.resize(imageB,(height,width)) angle=np.random.randint(max_angle) if np.random.randint(2): angle=-angle image=rotate(image,angle,resize=True) imageB=rotate(imageB,angle,resize=True) xstart=np.random.randint(width-org_width) ystart=np.random.randint(height-org_height) image=image[xstart:xstart+org_width,ystart:ystart+org_height] imageB=imageB[xstart:xstart+org_width,ystart:ystart+org_height] if np.random.randint(2): image=cv2.flip(image,1) imageB=cv2.flip(imageB,1) if np.random.randint(2): image=cv2.flip(image,0) imageB=cv2.flip(imageB,0) image=cv2.resize(image,(org_height,org_width)) imageB=cv2.resize(imageB,(org_height,org_width)) return image,imageB
def rotate(self, img, label, angle): assert img.shape==label.shape img = t.rotate(img, angle, resize=True) label = t.rotate(label, angle, resize=True) assert img.shape==label.shape lower_x, lower_y = 0 , 0 while (img[lower_x,:]==0).all(): lower_x +=1 while (img[:, lower_y]==0).all(): lower_y +=1 upper_x, upper_y = img.shape upper_x -=1 upper_y -=1 while (img[upper_x,:]==0).all(): upper_x -=1 while (img[:, upper_y]==0).all(): upper_y -=1 img = img[lower_x:upper_x, lower_y:upper_y] label = label[lower_x:upper_x, lower_y:upper_y] return img, label
def _augment(self,img, hm, max_rotation = 30): """ # TODO : IMPLEMENT DATA AUGMENTATION """ if random.choice([0,1]): r_angle = np.random.randint(-1*max_rotation, max_rotation) img = transform.rotate(img, r_angle, preserve_range = True) hm = transform.rotate(hm, r_angle) return img, hm
def iterate_train(self,batchsize,data_augment=False): num_batch=40000 for i in range(num_batch/batchsize): start=i*batchsize end=(i+1)*batchsize if (data_augment==False): x=self.train_set_x.get_value(borrow=True)[start:end] x=(x-self.mean)/256.0 x=np.asarray(x,dtype=theano.config.floatX) yield x, self.train_set_y.eval()[start:end] else: imgs=self.train_set_x.get_value(borrow=True)[start:end] for j in range(imgs.shape[0]): #horizontally flip if randint(0,1)==0: target=np.copy(imgs[j]) for i in range(imgs[j].shape[2]): target[:,:,i]=imgs[j][:,:,imgs[j].shape[2]-1-i] imgs[j]=target #color transform target=np.zeros([3,32,32]) mix=range(3) np.random.shuffle(mix) for x in range(3): target[x]=imgs[j][mix[x]] imgs[j]=target r=randint(0,7) if r==0: tmp=np.transpose(imgs[j],(1,2,0)); tmp=transform.resize(tmp[0:28,0:28,:],[32,32,3]) imgs[j]=np.transpose(tmp,(2,0,1)) elif r==1: tmp=np.transpose(imgs[j],(1,2,0)) tmp=transform.resize(tmp[0:28,4:32,:],[32,32,3]) imgs[j]=np.transpose(tmp,(2,0,1)) elif r==2: tmp=np.transpose(imgs[j],(1,2,0)) tmp=transform.resize(tmp[4:32,0:28,:],[32,32,3]) imgs[j]=np.transpose(tmp,(2,0,1)) elif r==3: tmp=np.transpose(imgs[j],(1,2,0)) tmp=transform.resize(tmp[4:32,4:32,:],[32,32,3]) imgs[j]=np.transpose(tmp,(2,0,1)) elif r==4: tmp=np.asarray(imgs[j],dtype='int32') tmp=transform.rotate(image=tmp,angle=5) imgs[j]=np.asarray(imgs[j],dtype=theano.config.floatX) elif r==5: tmp=np.asarray(imgs[j],dtype='int32') tmp=transform.rotate(image=tmp,angle=-5) imgs[j]=np.asarray(imgs[j],dtype=theano.config.floatX) imgs=(imgs-self.mean)/256.0 imgs=np.asarray(imgs,dtype=theano.config.floatX) yield imgs,self.train_set_y.eval()[start:end]
def transform_rot(image): # Need black background (0 boundary condition) # for rotational transform # Thats why Sobel or inverse transformation here #image_ref = filters.sobel(image) image_ref = 1.0 - image # Center of mass to be used as rotation center m = moments(image_ref,order=1) cx = m[1, 0]/m[0, 0] cy = m[0, 1]/m[0, 0] com = cx, cy # This next step is perfect in the math but the rotation angle # it generates varies drastically with changes in the watch image # thus its not robust enough for universal alignment. # Therefore we add an extra rotation step after it. # Ascertaining rotation angle from FFT transform ind1 = np.arange(image.shape[0],dtype=float) ind2 = np.arange(image.shape[1],dtype=float)[:,None] angle = \ np.angle(ind1-com[0]+1j*(ind2-com[1])) exp_theta = np.exp(1j*angle) angle_rot = np.angle(np.sum(np.sum(image_ref*exp_theta,axis=1)),deg=True) # Creating temporary rotated version of input image image_rot_aux = \ transform.rotate(image,angle_rot,resize=False,center=com,mode='nearest') # Second rotation step based on Inertia tensor # Again need 0 boundary condition away from object and # thus Sobel or inverse transform #image_ref = filters.sobel(image_rot_aux) image_ref = 1.0 - image_rot_aux m = moments(image_ref,order=2) Ixx = m[2, 0]/m[0, 0] - np.power(cx,2) Iyy = m[0, 2]/m[0, 0] - np.power(cy,2) Ixy = m[1, 1]/m[0, 0] - cx*cy inertia = [[Ixx, Ixy],[Ixy, Iyy]] w, v = np.linalg.eig(inertia) idx = w.argsort()[::-1] w = w[idx] v = v[:,idx] cross = np.cross(v[:,0],v[:,1]) # Ensuring eigenvectors satisfy right-hand rule if (cross < 0): v[:,1] *= -1 # Ascertaining rotation angle from inertia tensor eigenvectors angle_rad = np.arctan2(v[1,0],v[0,0]) + np.pi/2 angle_rot = np.degrees(angle_rad) # Creating final rotated version of input image image_rot = \ transform.rotate(image_rot_aux,angle_rot,resize=False,center=com,mode='nearest') return image_rot
def rotate_3d_ski(im, gt): im = np.transpose(im, (1, 2, 0)) gt = np.transpose(gt, (1, 2, 0)) ang = np.random.uniform(0, 360) r_im = rotate(im , ang, order=3) r_gt = rotate(gt, ang, order=3) return np.transpose(r_im, (2, 0, 1)), np.transpose(r_gt, (2, 0, 1))
def test_rotate_resize(): x = np.zeros((10, 10), dtype=np.double) x45 = rotate(x, 45, resize=False) assert x45.shape == (10, 10) x45 = rotate(x, 45, resize=True) # new dimension should be d = sqrt(2 * (10/2)^2) assert x45.shape == (14, 14)
def test_rotate_center(): x = np.zeros((10, 10), dtype=np.double) x[4, 4] = 1 refx = np.zeros((10, 10), dtype=np.double) refx[2, 5] = 1 x20 = rotate(x, 20, order=0, center=(0, 0)) assert_almost_equal(x20, refx) x0 = rotate(x20, -20, order=0, center=(0, 0)) assert_almost_equal(x0, x)
def img_rotate(img, rotate, corner_deg_chance): rot_chance = np.random.random() if rot_chance < corner_deg_chance: return tf.rotate(img, 90) if corner_deg_chance <= rot_chance < (corner_deg_chance * 2): return tf.rotate(img, 180) if (corner_deg_chance * 2) <= rot_chance < (corner_deg_chance * 3): return tf.rotate(img, 270) return tf.rotate(img, rotate)
def rotate_patch(patch, angle): """ :param patch: patch of size (4, 33, 33) :param angle: says how much rotation must be applied :return: rotate_patch """ return np.array([rotate(patch[0], angle, resize=False), rotate(patch[1], angle, resize=False), rotate(patch[2], angle, resize=False), rotate(patch[3], angle, resize=False)])
def shape_symmetry(image, center, major_axis, attrs={}, debug=False): # pad to make image center coincide with symmetry center lesion_mask, _ = pad_for_rotation(image[..., 3], center) rotated = rotate(lesion_mask, 90-major_axis.angle) flipped = rotated[:,::-1] diff = np.logical_xor(rotated, flipped) pixels_diff = diff.sum() / 2. major_ratio = pixels_diff / rotated.sum() if debug: print """\ ==== Shape Symmetry ==== --- Major Axis --- num of pixels : %d shape sym ratio : %.3f """ % (pixels_diff, major_ratio) plt.subplot(231) plt.imshow(rotated) plt.subplot(232) plt.imshow(flipped) plt.subplot(233) plt.imshow(diff) rotated = rotate(lesion_mask, 180-major_axis.angle) flipped = rotated[:,::-1] diff = np.logical_xor(rotated, flipped) pixels_diff = diff.sum() / 2. minor_ratio = pixels_diff / rotated.sum() if debug: print """\ --- Minor Axis --- num of pixels : %d shape sym ratio : %.3f """ % (pixels_diff, minor_ratio) plt.subplot(234) plt.imshow(rotated) plt.subplot(235) plt.imshow(flipped) plt.subplot(236) plt.imshow(diff) plt.show() attrs.update([ ('Shape Asymmetry Major Ratio', major_ratio), ('Shape Asymmetry Minor Ratio', minor_ratio), ('--Shape Asymmetry Score', (major_ratio > 0.13)*1 + (minor_ratio > 0.15)*1), ])
def _argmin_tilt(tilts, img0, flipped_img180, differ): nrows, ncols = img0.shape borderY, borderX = nrows//20, ncols//20 from skimage.transform import rotate diffs = [] for tilt in tilts: a = rotate(img0/np.max(img0), -tilt)[borderY:-borderY, borderX:-borderX] b = rotate(flipped_img180/np.max(flipped_img180), tilt)[borderY:-borderY, borderX:-borderX] diff = differ(a,b) print("* tilt=%s, diff=%s" % (tilt, diff)) diffs.append(diff) continue return tilts[np.argmin(diffs)]
def augment_data(self, image, target): images = [image.ravel(), ] targets = [target, ] image_modifiers = ( lambda x: rotate(x, 90), lambda x: rotate(x, 180), lambda x: rotate(x, 270), lambda x: rotate(x, 45), lambda x: swirl(x) ) for i in xrange(self.augmentation): img = image_modifiers[i](image) images.append(img.ravel()) targets.append(target) return images, targets
def get_rotated_sample(X, y, n): subset = np.random.random_integers(0, X.shape[0]-1, n) X_sub = X[subset] for index, img in enumerate(X_sub): if index%500==0: print "Processed Rotated {}".format(index) img1 = tf.rotate(img, np.random.uniform(5,15)) img1 = img1.reshape(-1, 1, 28, 28) img2 = tf.rotate(img, -np.random.uniform(5,15)) img2 = img2.reshape(-1, 1, 28, 28) X = np.vstack( (X, img1) ) X = np.vstack( (X, img2) ) y = np.append(y,y[subset[index]]) y = np.append(y,y[subset[index]]) return X, y
def allrotations(image, N): angles = np.linspace(0, 350, N) R = np.zeros((N, 784)) for i in xrange(N): img = rotate(image, angles[i]) R[i] = img.flatten() return R
def random_trans_single_output(pic_array): # randomly transform the pic_array, which is a numpy nd array # flipping do_hori_flip = np.random.binomial(1, 0.5) if do_hori_flip: pic_array = np.fliplr(pic_array) do_vert_flip = np.random.binomial(1, 0.5) if do_vert_flip: pic_array = np.flipud(pic_array) # rotation pic_array = rotate(pic_array, np.random.random_integers(0, 360), mode='constant', cval=1) # scaling scale_ratio = log(np.random.uniform(2.5, 4.5)) afine_tf = tf.AffineTransform(scale=(scale_ratio, scale_ratio)) pic_array = tf.warp(pic_array, afine_tf, mode='constant', cval=1) # translation trans_length = np.random.random_integers(-6, 6, 2) trans_length = (trans_length[0], trans_length[1]) afine_tf = tf.AffineTransform(translation=trans_length) pic_array = tf.warp(pic_array, afine_tf, mode='constant', cval=1) return pic_array
def asymmetry_measures(image): """Returns the distance between several rotations and flips of the image""" binary = image binary[binary > 0] = 1 lbs = label(binary) properties = regionprops(lbs) if len(properties) > 0: original = properties[0].image * 1. # flips left_right = np.fliplr(original) up_dow = np.flipud(original) # rotations angles = [45, 90, 135, 180, 225, 270, 315] rotations = [tf.rotate(original, angle) for angle in angles] # distances lr_dist = np.linalg.norm(original - left_right, ord=-np.inf) ud_dist = np.linalg.norm(original - up_dow, ord=-np.inf) rotation_dist = [] for rotation in rotations: rotation_dist.append(np.linalg.norm(original - rotation, ord=-np.inf)) return [lr_dist, ud_dist] + rotation_dist else: return [0, 0, 0, 0, 0, 0, 0, 0, 0]
def main(): try: if len(sys.argv) <= 1: print 'Error: Filename Required' if len(sys.argv) == 2: print 'Error: Background Filename Required' if len(sys.argv) >= 3: # Constants Window_Size = 5 image_name = sys.argv[1] ref_name = sys.argv[2] image = rgb2gray(io.imread(sys.argv[1])) ref = rgb2gray(io.imread(sys.argv[2])) part_image, region, angle = pre.interest_region(image, plot_image = 0) ref_rotate = rotate(ref,angle) part_ref = ref_rotate[region[0]:region[1], region[2]:region[3]] pre_image = pre.noise_reduction(part_image, part_ref, Window_Size, mode = 0) io.imsave('pre_image.jpg',pre_image) except KeyboardInterrupt: print "Shutdown requested... exiting" except Exception: traceback.print_exc(file=sys.stdout) sys.exit(0)
def addArtificialData(): print "here" baseName = os.path.basename(leftEyePath) print baseName data_dir = os.path.join(projectPath,baseName) print data_dir files = os.listdir(data_dir) files = [f for f in files if f.split('.')[-1]=='txt'] print files data = [] for f in files: label = f.split('.')[0] filePath = os.path.join(data_dir,f) with open(filePath,'r') as r: for image in r: data.append(image.strip()) #print data for f in data: parentDir = os.path.dirname(f) image_name = f.split('/')[-1].split('.')[0] scale_image_name = os.path.join(parentDir,image_name+'_s.jpg') roate_image_name = os.path.join(parentDir,image_name+'_r.jpg') print image_name img = io.imread(f,as_grey=True) scale_image = rescale(img,0.9) rotated_image = rotate(img,5,resize=False) print img.shape print scale_image.shape print rotated_image.shape io.imsave(scale_image_name,scale_image) io.imsave(roate_image_name,rotated_image) raw_input()
def image_generate(img, ZCA_array) : timg = np.copy(img) timg = timg.transpose(1, 2, 0) # transpose to use skimage augnum = random.randrange(0, 5) if augnum==0 or augnum==1 : # change nothing pass elif augnum==2 : # horizontal flip for j in range(3) : timg[:,:,j] = np.fliplr(timg[:,:,j]) elif augnum==3 : # random rotation of -15~15 degrees angle = random.random()*30-15 timg = transform.rotate(timg/256.0, angle) elif augnum==4 : # gamma correction (luminance adjust) - random gamma 0.7~1.3 gamma = random.random()*0.6+0.7 timg = exposure.adjust_gamma(timg/256.0, gamma) timg = timg.transpose(2, 0, 1) # GCN, ZCA for i in range(3) : timg[i,:,:] -= np.mean(timg[i,:,:]) timg[i,:,:] /= np.std(timg[i,:,:]) timg[i,:,:] = np.dot(ZCA_array, timg[i,:,:].reshape(1024, 1)).reshape(32, 32) return timg
def rotate_im1(im1, im2, pts): p1, p2, p3, p4 = pts theta1 = math.atan2(-(p2[1] - p1[1]), (p2[0] - p1[0])) theta2 = math.atan2(-(p4[1] - p3[1]), (p4[0] - p3[0])) dtheta = theta2 - theta1 im1 = sktr.rotate(im1, dtheta*180/np.pi) return im1, dtheta
def augmentImage(img, reSize, prefix): resizeImg = sktr.resize(img, reSize) skio.imsave(prefix+'resize.jpg', resizeImg) rotateImg = sktr.rotate(resizeImg, 180) skio.imsave(prefix+'rotate.jpg', rotateImg) mirrorImg = mirrorImage(resizeImg) skio.imsave(prefix+'mirror.jpg', mirrorImg)
def saveimage_16bit(image, fname='Test.tif', folder=None, rescale=True, dtype=np.uint16, imager=None): ''' Saves an images as a 16 bit tiff ''' # rotate the reverse direction image = tf.rotate(image, -1 * _imager_rot[imager]) # if scaled to 0,1 then rescale back to 16 bit if rescale: # print 'rescaled' image = rescale_intensity( image, in_range=(0, 1), out_range=(0, 2**16)) # Ensureing all the values are integers image = image.astype(dtype) folder = folder or '' image = io.imsave( os.path.join(folder, fname), image)
def f(image): # initialization rotation_angle = rng.uniform(low=-180, high=180) x_shift = rng.uniform(low=-5, high=5) y_shift = rng.uniform(low=-5, high=5) scale = 1/1.3 tr_scale_trans = tf.SimilarityTransform( scale=scale, translation=(x_shift, y_shift) ) image = image[0].reshape(shape).transpose(1, 2, 0) # color transformation trans = np.multiply( pca_val, np.array([ rng.normal(loc=0.0, scale=0.02), rng.normal(loc=0.0, scale=0.02), rng.normal(loc=0.0, scale=0.02) ]) ) trans = np.dot(pca_vec, trans) image = image + trans # geometric transformation transformed = tf.rotate(image, angle=rotation_angle) transformed = tf.warp(transformed, tr_scale_trans) transformed = transformed.transpose(2, 0, 1).flatten() return np.expand_dims(transformed, axis=0)
def modify(img): """Randomly modify an image This is a preprocessing step for training an OCR classifier. It takes in an image and casts it to greyscale, reshapes it, and adds some (1) rotations, (2) translations and (3) noise. If more efficiency is needed, we could factor out some of the initial nonrandom transforms. """ block_size = np.random.uniform(20, 40) rotation = 5*np.random.randn() #print 'BLOCK SIZE', block_size #print 'ROTATION ', rotation img = color.rgb2grey(img) img = transform.resize(img, output_shape=(50,30)) img = filter.threshold_adaptive(img, block_size=block_size) # rotate the image img = np.logical_not(transform.rotate(np.logical_not(img), rotation)) # translate the image img = shift(img) # add some noise to the image img = noise(img) img = transform.resize(img, output_shape=(25,15)) return filter.threshold_adaptive(img, block_size=25)
def rotate(self, angle, resize=True, **kw): ''' rotates an image using degrees if `resize` is True then the new image will be resized to fit the entire rotated image ''' multiplier = imagearray.dtype_max(self.dtype) return Image((transform.rotate(self, angle, resize, preserve_range=True, **kw))).convert_type(self.dtype)
def getBox(self, angle,origin,final_shape): ''' Given an angle, origin, and final image shape, this returns a single 2D image of a shape placed at the 'origin' and rotated to be facing 'angle'. ''' image=np.zeros(final_shape) r=self.radius x0=int(np.round(origin[0]-r)) y0=int(np.round(origin[1]-r)) bbox=[x0,y0,x0+int(2*r),y0+int(2*r)] box=rotate(self.box,angle) outOfBounds=[0,0,0,0] #This tells us how many pixels out of bounds our box is, so we can crop it. if bbox[0]<0: outOfBounds[0]=0-bbox[0] bbox[0]=0 if bbox[1]<0: outOfBounds[1]=0-bbox[1] bbox[1]=0 if bbox[2]>final_shape[0]: outOfBounds[2]=bbox[2]-final_shape[0] bbox[2]=final_shape[0] if bbox[3]>final_shape[1]: outOfBounds[3]=bbox[3]-final_shape[1] bbox[3]=final_shape[1] box=box[outOfBounds[0]:box.shape[0]-outOfBounds[2],outOfBounds[1]:box.shape[1]-outOfBounds[3]] image[bbox[0]:bbox[2],bbox[1]:bbox[3]]=box return image
def erect(img, angels=(-45, 45, 5), split=4): assert img[0][0] == 0 scores = [] for i in range(angels[0], angels[1], angels[2]): tmp = rotate(img, i, resize=True) best_split = select_best_split(tmp, split=split) tmp_1 = tmp[:, 0: best_split[0]] tmp_2 = tmp[:, best_split[0]: best_split[1]] tmp_3 = tmp[:, best_split[1]: best_split[2]] tmp_4 = tmp[:, best_split[2]:] #calculate the max connected array scores.append((i, _max_connected_ratio(tmp_1), _max_connected_ratio(tmp_2), _max_connected_ratio(tmp_3), _max_connected_ratio(tmp_4) )) #sorted(scores, key=lambda s : np.mean([s[1], s[2], s[3], s[4]]), reverse = True) return sorted(scores, key=lambda s : np.mean([s[1], s[2], s[3], s[4]]), reverse = True)[0][0]
def on_openImage_button_clicked(event): global source_image global filename global image global result global id try: dir_class = wx.FileDialog(None,'select image file', filename,"","",wx.OPEN) except: dir_class = wx.FileDialog(None,'select image file', os.getcwd(),"","",wx.OPEN) if dir_class.ShowModal() == wx.ID_OK: print dir_class.GetPath() filename = dir_class.GetPath() if '.jpg' in filename or '.JPG' in filename or '.png' in filename: result = [] image_name_textBox.SetValue(filename) image = cv2.imread(filename) image = rotate(image, -90) * 255.0 image = image.astype(np.uint8) if image.shape[0]*image.shape[1] > 1200*1200: image = cv2.resize(image, (int(image.shape[1]*0.7), int(image.shape[0]*0.7))) cv2.imshow('viewer', image) source_image = image.copy() dir_class.Destroy() id = str(uuid.uuid4())
def rotate_image(im, rotation_angle): ''' Rotates the image to a random angle < max_rotate ''' return rotate(im, rotation_angle)
weight="models/weight/InceptionResNetV2.h5", loss='rmse') plate = pd.read_csv('Images_distribution.csv', sep=';') df = pd.read_csv('Results_to_submit.csv', sep=';') df = pd.merge(df, plate, on=['id']) filesname = df['id'].values X_test = np.zeros((len(filesname), 128, 64, 3)) i = 0 index = [] for file in tqdm(filesname): im = imread('./test/' + file) size = im.shape[:2] if size[0] <= size[1] / 2: X_test[i, :, :, :] = resize(rotate(im, 90, resize=True), (128, 64, 3)) else: print('Image with context..' + file) im_true = copy.deepcopy(im) # test to extract by Yolo enforced by ROI result = ROI(im_true, net) if result is not None: print('Processed by Yolo') r = result['pred'] window = [ result['w_topleft']['x'], result['w_bottom']['x'], result['w_topleft']['y'], result['w_bottom']['y'] ] xw = window[0]
return ad_image if __name__ == '__main__': # file = "D:\藏文识别\相关文献\data\Test_character\chos lugs-pan chen blo chos kyi rgyal mtshan gsung 'bum-1-0007_1_09.png" # path = 'D:\藏文识别\相关文献\data\Test_character' # path = r'C:\Users\Zqc\Desktop\第二次数据-holiday\第二次数据-寒假\ResizeTwo' k = 0 for i in os.listdir(path): file = os.path.join(path,i) if file.endswith('.png'): k = k + 1 figure(k) image = io.imread(file) image = transform.rotate(image,180) RiGHT = True print('File: ',file) print('Right starting') image = adjunction_image(image) image = get_fileter_image(np.where(image > 0,1,0)) image_copy = copy.copy(image) row,col = image.shape # pointx_right, pointy_right,spilt_point = drop_failing(file, position='right') pointx_left, pointy_left,spilt_point = drop_failing(file, position='right') pointx_left_copy = copy.copy(pointx_left) pointy_left_copy = copy.copy(pointy_left) pointx_left.insert(0,0) pointy_left.insert(0,0) pointx_left.append(0) pointy_left.append(row - 1)
def htr(filepath): image = cv2.imread(filepath, 0) #change filename rows, cols = image.shape kernel = np.ones((9, 9), np.uint8) erode = cv2.erode(image, kernel, iterations=1) angle = determine_skew(erode) img = rotate(image, angle, resize=True) * 255 img = np.uint8(img) print('\ngot image') # mser properties _delta = 5 _min_area = 60 _max_area = 14400 _max_variation = 0.25 _min_diversity = .2 _max_evolution = 200 _area_threshold = 1.01 _min_margin = 0.003 _edge_blur_size = 5 mser = cv2.MSER_create(_delta, _min_area, _max_area, _max_variation, _min_diversity, _max_evolution, _area_threshold, _min_margin, _edge_blur_size) regions, boundingBoxes = mser.detectRegions(img) out_image_2 = np.zeros(img.shape, dtype='uint8') regions2 = [] area_regions = [] for region in regions: region = np.asarray(region) min1 = np.amin(region[:, 0]) max1 = np.amax(region[:, 0]) min2 = np.amin(region[:, 1]) max2 = np.amax(region[:, 1]) if max1 != min1 and max2 != min2: e = float(max2 - min2) / float(max1 - min1) ac = float(len(region)) / ((max2 - min2) * (max1 - min1)) if e > 0.1 and e < 10 and ac > 0.2: regions2.append(region) area_regions.append((max2 - min2) * (max1 - min1)) out_image_2[region[:, 1], region[:, 0]] = 255 area_regions = np.asarray(area_regions) regions = regions2 n, bins = np.histogram(area_regions, bins="auto") avg = 0 num = 0 a, b = bins[np.argmax(n)], bins[np.argmax(n) + 1] for i in range(len(area_regions)): if area_regions[i] > a and area_regions[i] < b: avg += area_regions[i] num += 1 avg = avg / float(num) kernell = np.ones((1, int(0.7 * np.sqrt(avg))), np.uint8) appx_size = int(0.7 * np.sqrt(avg)) out_image_3 = cv2.dilate(out_image_2, kernell, iterations=1) kernel2 = np.ones((int(0.2 * np.sqrt(avg)), 1), np.uint8) out_image_4 = cv2.dilate(out_image_3, kernel2, iterations=1) cnts, _ = cv2.findContours( out_image_4.astype(np.uint8).copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) regions1 = [] for i in range(len(cnts)): x, y, w, h = cv2.boundingRect(cnts[i]) include = True for j in range(len(cnts)): if j != i: x1, y1, w1, h1 = cv2.boundingRect(cnts[j]) if x >= x1 and y >= y1 and x + w <= x1 + w1 and y + h <= y1 + h1: include = False if (h > 2 * appx_size or w > 2 * appx_size or w * h > 100) and include: regions1.append([x, y, w, h]) regions1 = np.array(regions1) area = regions1[:, 2] * regions1[:, 3] area = np.array(sorted(area)) / (rows * cols) regions2 = [[] for i in range(len(regions1))] regions2[0].append(regions1[0]) line_idx = 0 for i in range(1, len(regions1)): x, y, w, h = regions1[i] xa, ya, wa, ha = regions1[i - 1] a = max(y, ya) b = min(h + y, ha + ya) if (b - a) > 0: regions2[line_idx].append(regions1[i]) else: line_idx = line_idx + 1 regions2[line_idx].append(regions1[i]) regions2 = np.array(regions2) regions2 = [x for x in regions2 if x != []] regions3 = [] for i in range(len(regions2) - 1, -1, -1): array = np.array(regions2[i]) g = np.argsort(array[:, 0]) lin = array[g, :] regions3.append(lin) content = u'' for line in regions3: LineString = '' for i in range(len(line[:, 0])): x, y, w, h = line[i, :] w = img[y:y + h, x:x + w] # _, wordImage = cv2.threshold(word,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) Word = predict(w, model2) LineString += Word + ' ' LineString += '\n' content += LineString return content
def create_tilings(colors_comb, colors, n1, n2, n_total, a, l, omega): n_rep = int(n_total / n1**2 / n2**2) h, w = colors[0].shape size = l - omega // 2 tilings = {} center = l - 1 - omega // 2 # l - size of image crop, a - resulting size of tile for i in range(n_total): c = [] for num in colors_comb[i]: #print(num) c.append(colors[int(num)][:]) final = np.zeros((2 * size, 2 * size)) for k in range(4): if k == 0: c[k], c[(k + 1) % 4] = merge_two_parts(c[k], c[(k + 1) % 4], omega) merged = np.hstack([c[k][:, :w - omega], c[(k + 1) % 4]]) final[:l] = merged elif k == 1: c[1] = np.rot90(c[1], k=1) c[2] = np.rot90(c[2], k=1) c[1], c[2] = merge_two_parts(c[1], c[2], omega) merged = np.hstack([c[1][:, :-omega], c[2]]) final[:, -l:] = np.rot90(merged, k=1, axes=(1, 0)) c[1] = np.rot90(c[1], k=1, axes=(1, 0)) c[2] = np.rot90(c[2], k=1, axes=(1, 0)) elif k == 2: c[3], c[2] = merge_two_parts(c[3], c[2], omega) merged = np.hstack([c[3], c[2][:, omega:]]) final[-l:] = merged elif k == 3: c[0] = np.rot90(c[0], k=1) c[3] = np.rot90(c[3], k=1) c[0], c[3] = merge_two_parts(c[0], c[3], omega) merged = np.hstack([c[0][:, :-omega], c[3]]) final[:, :l] = np.rot90(merged, k=1, axes=(1, 0)) c[0] = np.rot90(c[0], k=1, axes=(1, 0)) c[3] = np.rot90(c[3], k=1, axes=(1, 0)) ''' for j in range(omega//2, l): if k == 0: tile_ind = j-omega//2 if tile_ind < a: ind1 = tile_ind indices2 = np.arange(a-tile_ind-1,a) indices1 = np.arange(ind1+1) else: ind1 = 2*a-1-tile_ind-1 indices2 = np.arange(ind1+1) indices1 = np.arange(tile_ind-a+1, a) final[indices1, indices2] = merged[j, np.arange(center-ind1, center+ind1+1,2)] elif k == 1: tile_ind = j-omega//2 if tile_ind < a: ind1 = tile_ind indices1 = np.arange(a-tile_ind-1,a) indices2 = np.arange(a-1, a-tile_ind-1-1,-1) else: ind1 = 2*a - 1 - tile_ind indices1 = np.arange(ind1+1) indices2 = np.arange(2*a-1-tile_ind, -1, -1) final[indices1, indices2] = merged[j, np.arange(center-ind1, center+ind1+1,2)] elif k == 2: tile_ind = j-omega//2 if tile_ind < a: ind1 = tile_ind indices1 = np.arange(a-1,a-1-tile_ind-1,-1) indices2 = np.arange(tile_ind,-1,-1) else: ind1 = 2*a - 1 - tile_ind indices1 = np.arange(ind1, -1, -1) indices2 = np.arange(a-1, (a-ind1-2), -1) final[indices1, indices2] = merged[j, np.arange(center-ind1, center+ind1+1,2)] elif k == 3: tile_ind = j-omega//2 if tile_ind < a: ind1 = tile_ind indices1 = np.arange(tile_ind, -1, -1) indices2 = np.arange(tile_ind+1) else: ind1 = 2*a - 1 - tile_ind indices1 = np.arange(a-1, tile_ind-a-1, -1) indices2 = np.arange(tile_ind-a, a) final[indices1, indices2] = merged[j, np.arange(center-ind1, center+ind1+1,2)] if i == 0: tmp = c[2][:] ''' #final = np.rot90(final, k = k, axes=(1, 0)) plt.imsave('final_{0}_{1}.png'.format(k, colors_comb[i]), final, cmap='gray') #print(final.shape) final = rotate(final[omega // 2:-omega // 2, omega // 2:-omega // 2], -45, resize=True) #print(final.shape) #print(size) a = final.shape[0] // 2 print(a) center = final.shape[0] - a plt.imsave('final_{}.png'.format(colors_comb[i]), final[a // 2:-a // 2, a // 2:-a // 2], cmap='gray') #print(final[a//2:-a//2, a//2:-a//2].shape) #print(size) #print(colors_comb[i]) tilings.update({colors_comb[i]: final[a // 2:-a // 2, a // 2:-a // 2]}) #print(colors_comb[i]) #print(tilings.keys()) return a, tilings
best_score = score best_name = name return best_name # extracting and saving the LBP descriptors refs = { 'brick': local_binary_pattern(brick, n_points, radius, 'uniform'), 'grass': local_binary_pattern(grass, n_points, radius, 'uniform'), 'gravel': local_binary_pattern(gravel, n_points, radius, 'uniform') } # classify rotated textures and testing its robustness to rotation print('Rotated images matched against references using LBP:') print('original: brick, rotated: 30deg, match result: ', match(refs, transform.rotate(brick, angle=30, resize=False))) print('original: brick, rotated: 70deg, match result: ', match(refs, transform.rotate(brick, angle=70, resize=False))) print('original: grass, rotated: 145deg, match result: ', match(refs, transform.rotate(grass, angle=145, resize=False))) # plot histograms of LBP of textures fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(nrows=2, ncols=3, figsize=(9, 6)) plt.gray() ax1.imshow(brick) ax1.axis('off') ax4.hist(refs['brick']) ax4.set_ylabel('Percentage')
def test_kinect_ims(data_folder, shape_class, augment=True): data_list = [] import ipdb ipdb.set_trace() for fn in os.listdir("data/" + data_folder + "/"): if "kinect_data" in fn: new_data = np.load("data/" + data_folder + "/" + fn) new_data_processed = processimgs.process_raw_camera_data( new_data, shape_class) data_list.append(new_data_processed) if len(data_list) > 0: camera_data = np.concatenate(data_list, axis=0) else: camera_data = data_list[0] if augment: num_rotations = 10 num_shear = 10 num_color = 10 num_random_noise = 10 new_camera_data = camera_data.copy() for im in camera_data: for _ in range(num_rotations): new_im = rotate(im, angle=np.random.uniform(low=-9, high=9)) new_camera_data = np.vstack( [new_camera_data, new_im.reshape((1, ) + new_im.shape)]) for _ in range(num_shear): tf = AffineTransform( shear=np.random.uniform(low=-0.9, high=0.9)) new_im = transform.warp(im, tf, order=1, preserve_range=True, mode='wrap') new_camera_data = np.vstack( [new_camera_data, new_im.reshape((1, ) + new_im.shape)]) for _ in range(num_random_noise): new_im = 255 * random_noise(im / 255., var=0.01**2) new_camera_data = np.vstack( [new_camera_data, new_im.reshape((1, ) + new_im.shape)]) for _ in range(num_color): new_im = im * np.random.uniform(low=0.9, high=1.1) new_camera_data = np.vstack( [new_camera_data, new_im.reshape((1, ) + new_im.shape)]) new_im = im + np.random.uniform(low=-0.1 * 255, high=0.1 * 255) new_camera_data = np.vstack( [new_camera_data, new_im.reshape((1, ) + new_im.shape)]) camera_data = new_camera_data image = camera_data[0, :, :] camera_data = camera_data.reshape( (camera_data.shape[0], camera_data.shape[1] * camera_data.shape[2])) idxs = list(range(camera_data.shape[0])) np.random.shuffle(idxs) camera_data = camera_data[idxs] my_vae, encoder, decoder, inputs, outputs, output_tensors = vae.make_dsae( image.shape[0], image.shape[1], n_channels=1) n_train = 0.1 vae.train_vae(my_vae, camera_data, n_train, inputs, outputs, output_tensors, n_epochs=30) model_folder = "models/" + data_folder model_fn = model_folder + "/test_weights.h5y" if not os.path.exists(model_folder): os.mkdir(model_folder) my_vae.save_weights(model_fn)
def frame_rotate(array, angle, imlib='opencv', interpolation='lanczos4', cxy=None, border_mode='constant'): """ Rotates a frame or 2D array. Parameters ---------- array : numpy ndarray Input image, 2d array. angle : float Rotation angle. imlib : {'opencv', 'skimage'}, str optional Library used for image transformations. Opencv is faster than Skimage or scipy.ndimage. interpolation : str, optional For Skimage the options are: 'nearneig', bilinear', 'bicuadratic', 'bicubic', 'biquartic' or 'biquintic'. The 'nearneig' interpolation is the fastest and the 'biquintic' the slowest. The 'nearneig' is the poorer option for interpolation of noisy astronomical images. For Opencv the options are: 'nearneig', 'bilinear', 'bicubic' or 'lanczos4'. The 'nearneig' interpolation is the fastest and the 'lanczos4' the slowest and more accurate. 'lanczos4' is the default for Opencv and 'biquartic' for Skimage. cxy : float, optional Coordinates X,Y of the point with respect to which the rotation will be performed. By default the rotation is done with respect to the center of the frame; central pixel if frame has odd size. border_mode : {'constant', 'edge', 'symmetric', 'reflect', 'wrap'}, str optional Pixel extrapolation method for handling the borders. 'constant' for padding with zeros. 'edge' for padding with the edge values of the image. 'symmetric' for padding with the reflection of the vector mirrored along the edge of the array. 'reflect' for padding with the reflection of the vector mirrored on the first and last values of the vector along each axis. 'wrap' for padding with the wrap of the vector along the axis (the first values are used to pad the end and the end values are used to pad the beginning). Default is 'constant'. Returns ------- array_out : numpy ndarray Resulting frame. """ if array.ndim != 2: raise TypeError('Input array is not a frame or 2d array') y, x = array.shape if cxy is None: cy, cx = frame_center(array) else: cx, cy = cxy if imlib == 'skimage': if interpolation == 'nearneig': order = 0 elif interpolation == 'bilinear': order = 1 elif interpolation == 'bicuadratic': order = 2 elif interpolation == 'bicubic': order = 3 elif interpolation == 'biquartic' or interpolation == 'lanczos4': order = 4 elif interpolation == 'biquintic': order = 5 else: raise ValueError('Skimage interpolation method not recognized') if border_mode not in [ 'constant', 'edge', 'symmetric', 'reflect', 'wrap' ]: raise ValueError('Skimage `border_mode` not recognized.') min_val = np.min(array) im_temp = array - min_val max_val = np.max(im_temp) im_temp /= max_val array_out = rotate(im_temp, angle, order=order, center=cxy, cval=np.nan, mode=border_mode) array_out *= max_val array_out += min_val array_out = np.nan_to_num(array_out) elif imlib == 'opencv': if no_opencv: msg = 'Opencv python bindings cannot be imported. Install opencv or' msg += ' set imlib to skimage' raise RuntimeError(msg) if interpolation == 'bilinear': intp = cv2.INTER_LINEAR elif interpolation == 'bicubic': intp = cv2.INTER_CUBIC elif interpolation == 'nearneig': intp = cv2.INTER_NEAREST elif interpolation == 'lanczos4': intp = cv2.INTER_LANCZOS4 else: raise ValueError('Opencv interpolation method not recognized') if border_mode == 'constant': bormo = cv2.BORDER_CONSTANT # iiiiii|abcdefgh|iiiiiii elif border_mode == 'edge': bormo = cv2.BORDER_REPLICATE # aaaaaa|abcdefgh|hhhhhhh elif border_mode == 'symmetric': bormo = cv2.BORDER_REFLECT # fedcba|abcdefgh|hgfedcb elif border_mode == 'reflect': bormo = cv2.BORDER_REFLECT_101 # gfedcb|abcdefgh|gfedcba elif border_mode == 'wrap': bormo = cv2.BORDER_WRAP # cdefgh|abcdefgh|abcdefg else: raise ValueError('Opencv `border_mode` not recognized.') M = cv2.getRotationMatrix2D((cx, cy), angle, 1) array_out = cv2.warpAffine(array.astype(np.float32), M, (x, y), flags=intp, borderMode=bormo) else: raise ValueError('Image transformation library not recognized') return array_out
def jet_detect(img, calibratemean, calibratestd): '''Finds the jet from the online camera roi using Canny edge detection and Hough line transform This method first compares the mean of the ROI image to the mean of the calibrated ROI. Then Canny edge detection is used to detect the edges of the jet in the ROI and convert the original image to a binary image. Hough line transform is performed on the binary image to determine the approximate position of the jet. Peak-finding is performed on several horizontal slices of the image, and a line is fitted to these points to determine the actual position of the jet. If a peak is found that is not in the approximate position of the jet determined by the Hough line transform, that point is not considered in the line fitting. Parameters ---------- img : ndarray ROI of the on-axis image mean : float mean of calibration ROI image with jet (see calibrate()) calibratestd : float standard deviation calibration ROI image with jet (see calibrate()) Returns ------- rho : float Distance from (0,0) to the line in pixels theta : float Angle of the shortest vector from (0,0) to the line in radians ''' # compare mean & std of current image to mean & std of calibrate image mean, std = image_stats(img) if (mean < calibratemean * 0.8) or (mean > calibratemean * 1.2): raise ValueError('ERROR mean: no jet') try: # use canny edge detection to convert image to binary binary = canny(img, sigma=2, use_quantiles=True, low_threshold=0.9, high_threshold=0.99) # perform Hough Line Transform on binary image h, angles, d = hough_line(binary) res = hough_line_peaks(h, angles, d, min_distance=1, threshold=int(img.shape[0]/3)) # keep only valid lines valid = [] for _, theta, dist in zip(*res): jetValid = True # jet must be within 45 degrees of vertical if (theta < np.radians(-45)) or (theta > np.radians(45)): jetValid = False # jet must start from top edge of imagei yint = dist / np.sin(theta) xint = np.tan(theta) * yint if (dist < 0) or (xint > binary.shape[1]): jetValid = False # jet must be within [x] pixels width #if (cam_utils.get_jet_width(img, rho, theta) * pxsize > 0.01): # jetValid = false # print('ERROR width: not a jet') if (jetValid): valid.append([theta, dist]) except Exception: raise ValueError('ERROR hough: no jet') # use local maxes to determine exact jet position # line-fitting cannot be performed on vertical line (which is highly likely due to # nature of jet) so rotate image first imgr = rotate(img, 90, resize=True, preserve_range=True) jet_xcoords = [] jet_ycoords = [] for x in range(10): # try to find local maxes (corresponds to jet) in 10 rows along height of image) col = int(imgr.shape[1] / 10 * x) ymax = peak_local_max(imgr[:,col], threshold_rel=0.9, num_peaks=1)[0][0] # check if point found for max is close to jet lines found with Hough transform miny = imgr.shape[0] maxy = 0 for theta, dist in valid: xint = dist / np.sin(theta) y = imgr.shape[0] - ((xint - col) * np.tan(theta)) if (y < miny): miny = y if (y > maxy): maxy = y # if x found using local max is close to lines found with Hough transform, keep it if (ymax >= (miny - 5)) and (ymax <= (maxy + 5)): jet_xcoords.append(col) jet_ycoords.append(ymax) try: # fit a line to the points found using local max m, b = np.polyfit(jet_xcoords, jet_ycoords, 1) theta = -np.arctan(m) rho = np.cos(theta) * (imgr.shape[0] - b) except Exception: raise ValueError('ERROR polyfit: no jet') return rho, theta
plt.imshow(org_img_disc_region) plt.title('org_img_disc_region') plt.show() # plt.imshow(org_disc_region) # plt.title('org_disc_region') # plt.show() # # plt.imshow(org_cup_region) # plt.title('org_cup_region') # plt.show() # Disc and Cup segmentation by M-Net run_time_start = time() Org_img_Disc_flat = rotate( cv2.linearPolar(org_img_disc_region, (DiscROI_size / 2, DiscROI_size / 2), DiscROI_size / 2, cv2.WARP_FILL_OUTLIERS), -90) # plt.imshow(Org_img_Disc_flat) # plt.title('Org_img_Disc_flat') # plt.show() temp_img = pro_process(Org_img_Disc_flat, CDRSeg_size) temp_img = np.reshape(temp_img, (1, ) + temp_img.shape) [prob_6, prob_7, prob_8, prob_9, prob_10] = CDRSeg_model.predict(temp_img) run_time_end = time() # Extract mask prob_map = np.reshape( prob_10, (prob_10.shape[1], prob_10.shape[2], prob_10.shape[3]))
#plt.show() # In[163]: lx, ly = A_grey.shape cropA=A_grey[int(lx/4):int(lx*3/4),int(ly/4):int(ly*3/4)] plt.imshow(cropA, cmap=plt.cm.gray) # In[166]: from skimage import transform rotA = transform.rotate(A_grey, 45) plt.imshow(rotA, cmap=plt.cm.gray) # In[175]: fudA=A_grey[::-1] plt.imshow(fudA, cmap=plt.cm.gray) # In[181]: from skimage import filters gfiltA = filters.gaussian(A_grey, sigma=(10,10), multichannel=True)
def data_augmentation(data): """Perform data augmentation on the provided image and labels. The data augmentation techniques used in this function are rotations, horizontal and vertical mirrorings and rotated mirrorings""" # Pre-compute horizontal and vertical mirrorings h_i_mirror = np.fliplr(data) v_i_mirror = np.flipud(data) # Return list with all data augmentations # The 270º rotations are done in two steps because if they are done on one # step, the result image is not resized well return [ # Original data data, # Rotations rotate(data, angle=180, resize=False), rotate(data, angle=90, resize=True), rotate(rotate(data, angle=90, resize=True), angle=180, resize=False), # Mirrorings h_i_mirror, v_i_mirror, # Rotated mirrorings rotate(h_i_mirror, angle=180, resize=False), rotate(h_i_mirror, angle=90, resize=True), rotate(rotate(h_i_mirror, angle=90, resize=True), angle=180, resize=False), rotate(v_i_mirror, angle=180, resize=False), rotate(v_i_mirror, angle=90, resize=True), rotate(rotate(v_i_mirror, angle=90, resize=True), angle=180, resize=False) ]
ax[0].set_xlim(0, col) ax[0].set_ylim(row, 0) else: ax[0].axis('off') ax[1].axis('off') plt.tight_layout() # Scaling scale_factor = 0.2 image_rescaled = rescale(Img, scale_factor) show_paired(Img, image_rescaled, "Rescaled") # Roation Angle = 30 image_rotated = rotate(Img, Angle) show_paired(Img, image_rotated, "Rotated") # Horizontal Flip horizontal_flip = Img[:, ::-1] show_paired(Img, horizontal_flip, 'Horizontal Flip') # Vertical Flip vertical_flip = Img[::-1, :] show_paired(Img, vertical_flip, 'vertical Flip') # Intensity rescaling Min_Per, Max_Per = 5, 95 min_val, max_val = np.percentile(Img, (Min_Per, Max_Per)) better_contrast = exposure.rescale_intensity(Img, in_range=(min_val, max_val))
(ROI_mask_result - ROI_mask_result.min())).astype(np.uint8) ROI_mask_result[ROI_mask_result == 255] = 200 ROI_mask_result[ROI_mask_result == 0] = 255 ROI_mask_result[ROI_mask_result == 200] = 0 ROI_mask_result[(ROI_mask_result > 0) & (ROI_mask_result < 255)] = 128 # plt.imshow(ROI_mask_result) # plt.title('ROI_mask_result') # plt.show() if is_polar_coordinate: if is_only_image == False: ROI_mask_result = rotate( cv2.linearPolar(ROI_mask_result, (DiscROI_size / 2, DiscROI_size / 2), DiscROI_size / 2, cv2.INTER_NEAREST + cv2.WARP_FILL_OUTLIERS), -90) # plt.imshow(ROI_mask_result) # plt.title('ROI_mask_result') # plt.show() ROI_img_result = rotate( cv2.linearPolar(org_img_disc_region, (DiscROI_size / 2, DiscROI_size / 2), DiscROI_size / 2, cv2.INTER_NEAREST + cv2.WARP_FILL_OUTLIERS), -90) else: ROI_img_result = org_img_disc_region
def imrotate(im,angle): # in degrees, with respect to center return trfm.rotate(im,angle)
region_image = [] for region in measure.regionprops(labels, intensity_image=None, cache=True): if region.area > max: max = region.area region_label = region.label region_image = region.filled_image image = region_image #END THRESHOLDING #for i in range(0, len(area_array)): #print "%s %.23f" % (threshold_array[i], float(area_array[i])/(float(len(new_img)*len(new_img[1])))) #FIX IMAGE ROTATION AND CONVERT TO OPENCV2 FORMAT if(exif_orientation_array[0] == "Rotated"): if(exif_orientation_array[2] == "CCW"): image = rotate(image, -int(exif_orientation_array[1]), resize = True) if(exif_orientation_array[2] == "CW"): image = rotate(image, int(exif_orientation_array[1]), resize = True) cv_image = img_as_ubyte(image) for y in range(int(.8*len(cv_image)), len(cv_image)): counter = 0 for x in range(0, len(cv_image[0])): if cv_image[y][x] > 140: counter += 1 if cv_image[y][x] < 140: if counter < .1 * len(cv_image[0]) : for i in range(x - counter, x): cv_image[y][i] = 0 counter = 0 labels = measure.label(cv_image) max = 0
def rescale_synthetic_HPBW(header, c_freq, diameter, vmodel, a_term_upscale, phase_centre, para_angle): tb = casatools.table() qa = casatools.quanta() me = casatools.measures() try: ### Set sizes size = np.array([header['NAXIS1'], header['NAXIS2']]) cdelt = np.array([ header['CDELT1'] * a_term_upscale, header['CDELT2'] * a_term_upscale ]) centre = np.array([header['CRVAL1'], header['CRVAL2']]) total_size = size * cdelt #print('Header found') ret_singular = False except: #print('No header found reverting to some default values') size = [512, 512] cdelt = [1.388888888889e-07, 1.388888888889e-07] centre = [header[0], header[1]] ret_singular = True ### Open voltage model #print('open fits') hdu = fits.open(vmodel) vhead = hdu[0].header #print('reorder fits') vdata = hdu[0].data.squeeze() vdata = vdata.byteswap().newbyteorder() hdu.close() ### Rotate data #try: #print('sklearn rotate') ct = np.where(vdata == vdata.max()) #print('rotate') #print(vdata.shape,para_angle,ct[0][0],ct[1][0]) vdata = rotate(image=vdata, angle=para_angle, center=[ct[0][0], ct[1][0]]) #except: #from scipy.ndimage import rotate #print('scipy rotate') #vdata = rotate(input=vdata,angle=para_angle,reshape=False) ### Rescale model to diameter vhead['CDELT1'] = vhead['CDELT1'] * (vhead['DIAMETER'] / diameter) vhead['CDELT2'] = vhead['CDELT2'] * (vhead['DIAMETER'] / diameter) ### Rescale model to frequency vhead['CDELT1'] = vhead['CDELT1'] * (vhead['CRVAL3'] / c_freq) vhead['CDELT2'] = vhead['CDELT2'] * (vhead['CRVAL3'] / c_freq) ### Set phase centres vhead['CRVAL1'] = phase_centre[0] vhead['CRVAL2'] = phase_centre[1] #print('wcs') ### Set cutout re-sampling wcs = WCS(vhead, naxis=2) #print('wcs convert') centre_p = wcs.all_world2pix(centre[0] * u.deg, centre[1] * u.deg, 0) scale = vhead['CDELT1'] / np.abs(cdelt[0]) ## Add hard total pixel scaling factor # (bigger machines can have > 2e4 but probably overkill) sz = 2000 while scale * sz > 2e4: sz = int(sz - 2) if sz < 2: print("error size less than 2") sys.exit() hdu_cut = Cutout2D(data=vdata,wcs=wcs,\ position=[centre_p[0],centre_p[1]],\ size=[sz,sz]) ## Adjust scales to take this into account vhead = hdu_cut.wcs.to_header() vhead['CDELT1'] = vhead['CDELT1'] / scale vhead['CDELT2'] = vhead['CDELT2'] / scale vhead['CRPIX1'] = vhead['CRPIX1'] * scale vhead['CRPIX2'] = vhead['CRPIX2'] * scale data = rescale(hdu_cut.data, scale) wcs = WCS(vhead, naxis=2) centre_p = wcs.all_world2pix(centre[0] * u.deg, centre[1] * u.deg, 0) hdu_cut = Cutout2D(data=data, wcs=wcs,\ position=[centre_p[0],centre_p[1]],\ size=size) if ret_singular == False: return hdu_cut.data else: hc = hdu_cut.data.shape return hdu_cut.data[int(hc[0] / 2), int(hc[1] / 2)]
def drop_failing(file, position = 'left',rotate = True): image = io.imread(file) image = adjunction_image(image) if(rotate == True): image = transform.rotate(image, 180) # rember delete image = image.astype(int) image = np.where(image > 0.9, 1, 0) print("image shape :" + str(image.shape)) row, column = image.shape image = get_fileter_image(image) pointx = [] pointy = [] temp_x = temp_y = 0 spilt_point = [] if(position == "left"): pointx, pointy = get_point_left_start_location(image) if(len(pointx)): temp_y = pointy[-1] temp_x = pointx[-1] while temp_y < row - 1: temp_x, temp_y = get_Next_Point_Left(image, int(temp_x), (temp_y),pointx) if(0 == temp_y and 0 == temp_x): return pointx,pointy,[] if (len(pointx) > 2): if (pointx[-2] == temp_x and pointy[-2] == temp_y): print("left出现回溯现象:[" + str(temp_x) + "," + str(temp_y) + "]") t1_y = temp_y + 1 t1_x = temp_x + 1 index = get_min_index_rol(pointx,pointy) tempx, tempy = get_min_point(image,temp_x + 2,temp_y,index) if(tempx == temp_x + 1 and tempy == temp_y): pointy.append(temp_y + 1) pointx.append(temp_x + 1) temp_y = temp_y + 1 temp_x = temp_x + 1 else: for i in range(1, len(pointx)): if (pointx[len(pointx) - i] == tempx): count = len(pointx) - i; break print('count:', count) print("[" + str(tempx) + "," + str(tempy) + "],index = ",index) '''路径最优滴水算法''' pointx = pointx[:count + 1] pointy = pointy[:count + 1] pointy.append(tempy ) pointx.append(tempx ) temp_y = tempy temp_x = tempx '''传统滴水算法''' # pointy.append(temp_y + 1) # pointx.append(temp_x + 1) # temp_y = temp_y + 1 # temp_x = temp_x + 1 spilt_point.append([t1_x,t1_y,temp_x, temp_y - 1]) else: pointy.append(temp_y) pointx.append(temp_x) else: if (pointx[-1] > temp_x and pointy[-1] == temp_y): print(temp_x,temp_y) pointy.append(temp_y + 1) pointx.append(temp_x + 1) temp_y = temp_y + 1 temp_x = temp_x + 1 else: pointy.append(temp_y) pointx.append(temp_x) else: flag = False pointx, pointy = get_point_right_start_location(image) if (len(pointx)): temp_y = pointy[-1] temp_x = pointx[-1] else: return 0,0,[] while temp_y < row - 1: temp_x, temp_y = get_Next_Point_Right(image, int(temp_x), int(temp_y)) if (len(pointx) > 2): if (pointx[-2] == temp_x and pointy[-2] == temp_y): print("right出现回溯现象:[" + str(temp_x) + "," + str(temp_y) + "]") index = get_min_index(pointx, pointy) print('index: ',index,'point:',get_min_index_rol(pointx, pointy)) pointy = pointy[:index + 1] pointx = pointx[:index + 1] temp_y = pointy[-1] + 1 temp_x = pointx[-1] pointy.append(temp_y) pointx.append(temp_x) ty2 = find(image, temp_x - 1, temp_y) spilt_point.append([temp_x, temp_y , temp_x, ty2 - 1]) else: pointy.append(temp_y) pointx.append(temp_x) else: if (pointx[-1] < temp_x and pointy[-1] == temp_y): pointy.append(temp_y + 1) pointx.append(temp_x - 1) temp_y = temp_y + 1 temp_x = temp_x - 1 else: pointy.append(temp_y) pointx.append(temp_x) return pointx,pointy,spilt_point
""" import math import matplotlib.pyplot as plt import numpy as np import pandas as pd from skimage.draw import ellipse from skimage.measure import label, regionprops, regionprops_table from skimage.transform import rotate image = np.zeros((600, 600)) rr, cc = ellipse(300, 350, 100, 220) image[rr, cc] = 1 image = rotate(image, angle=15, order=0) rr, cc = ellipse(100, 100, 60, 50) image[rr, cc] = 1 label_img = label(image) regions = regionprops(label_img) ##################################################################### # We use the :py:func:`skimage.measure.regionprops` result to draw certain # properties on each region. For example, in red, we plot the major and minor # axes of each ellipse. fig, ax = plt.subplots() ax.imshow(image, cmap=plt.cm.gray)
trX = trX.reshape(trX.__len__(), 28, 28) teX_rotated = np.empty(teX.shape) trX_rotated = np.empty(trX.shape) trX_filtred = np.empty(trX.shape) teX_filtred = np.empty(teX.shape) trX_s_1 = np.empty(trX.shape) teX_s_1 = np.empty(teX.shape) trX_s_2 = np.empty(trX.shape) teX_s_2 = np.empty(teX.shape) for enum in range(teX.__len__()): teX_rotated[enum] = rotate(teX[enum], -35, preserve_range=True) teX_filtred[enum] = gaussian(teX_rotated[enum], sqrt(0.8), preserve_range=True) for i in range(10000): teX_s_1[i] = np.dot(left_matrix, teX_filtred[i]) teX_s_2[i] = np.dot(teX_s_2[i], right_matrix) print("Modified_teX") for enum in range(trX.__len__()): trX_rotated[enum] = rotate(trX[enum], -35, preserve_range=True) trX_filtred[enum] = gaussian(trX_rotated[enum], sqrt(0.8), preserve_range=True)
def generate_logo(filename): """ Load component images, apply a sinogram, and assemble to form the svmbir logo. Args: filename: Name of image file used to generate the sinogram for inclusion in the logo. Returns: None """ # Load the svmbir image, convert to negative, display, and save image = read_grey(filename) image = 1-image plt.imshow(image, cmap=plt.cm.Greys_r) plt.title("Original") plt.show() svmbir_letters = np.copy(image) # Apply the radon transform and do gamma correction to improve contrast theta = np.linspace(0., 360., max(image.shape), endpoint=False) sinogram = radon(image, theta=theta, circle=True) sinogram_scaled = sinogram / np.amax(sinogram) gamma_corrected = exposure.adjust_gamma(sinogram_scaled, 0.4) # Display the sinogram and gamma corrected version fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4.5)) ax1.set_title("Radon transform\n(Sinogram)") ax1.set_xlabel("Projection angle (deg)") ax1.set_ylabel("Projection position (pixels)") ax1.imshow(sinogram, cmap=plt.cm.Greys_r, extent=(0, 360, 0, sinogram.shape[0]), aspect='auto') ax2.set_title("Gamma corrected") ax2.imshow(gamma_corrected, cmap=plt.cm.Greys_r, extent=(0, 360, 0, sinogram.shape[0]), aspect='auto') fig.tight_layout() plt.show() # Save the gamma-corrected, rotated sinogram sinogram_int = np.round(gamma_corrected*255) sinogram_copy = np.copy(sinogram_int) sinogram_rot = rotate(sinogram_copy, 90) imsave('sinogram_rot.png', sinogram_rot.astype(np.uint8)) # Do the reconstruction and display # sinogram_int = imread('sinogram_rot.png') # sinogram_int = rotate(sinogram_int, -90) sinogram = exposure.adjust_gamma(sinogram_copy / 255, 2) image_recov = iradon(sinogram, theta=theta) image_recov_orig = iradon(np.round(sinogram_scaled * 255) / 255, theta) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4.5)) ax1.set_title("Recon from original") ax1.imshow(image_recov_orig, cmap=plt.cm.Greys_r, aspect='equal') ax2.set_title("Recon from gamma corrected") ax2.imshow(image_recov, cmap=plt.cm.Greys_r,aspect='equal') fig.tight_layout() plt.show() # Load the images for the logo sino = sinogram_rot.astype(float)/255 # read_grey('sinogram_rot.png') mbir_letters = read_grey('images/mbir.png') arrow_left = read_grey('images/arrow_left.png', channel=3) arrow_right = read_grey('images/arrow_right.png', channel=3) svmbir_text = read_grey('images/text.png', channel=3) # Set dimensions for logo elements - rescale images as needed spacer = 5 pad = 10 new_width = sino.shape[1] + svmbir_letters.shape[0] mbir_letters = rescale(mbir_letters, new_width / mbir_letters.shape[1]) mbir_letters = exposure.adjust_gamma(mbir_letters, 0.2) mbir_letters[mbir_letters > 0.95] = 1 mbir_letters[mbir_letters < 0.05] = 0 new_height = sino.shape[0] + mbir_letters.shape[0] + spacer svmbir_text = 1 - rescale(svmbir_text, new_height / svmbir_text.shape[0]) svmbir_text = exposure.adjust_gamma(svmbir_text, 1.5) svmbir_text[svmbir_text > 0.95] = 1 svmbir_text[svmbir_text < 0.05] = 0 height = sino.shape[0] + mbir_letters.shape[0] + 3 * spacer width = mbir_letters.shape[1] + svmbir_text.shape[1] + 2 * spacer # Get the empty image and add the sinogram logo = np.zeros((height, width)) i = spacer j = spacer logo = copy_in(logo, sino, i, j) # Add the vertical bar and the svmbir letters with arrows i = spacer j = sino.shape[1] white_bar_ver = np.ones((sino.shape[1], spacer)) logo = copy_in(logo, white_bar_ver, i, j) j = sino.shape[1] + spacer arrow_left = rescale(arrow_left, (svmbir_letters.shape[1]/2) / arrow_left.shape[1]) logo = copy_in(logo, arrow_left, i, j) j = j + arrow_left.shape[1] arrow_right = rescale(arrow_right, (svmbir_letters.shape[1]/2) / arrow_right.shape[1]) logo = copy_in(logo, arrow_right, i, j) j = sino.shape[1] + spacer logo = copy_in(logo, svmbir_letters, i, j, method="max") # Add the svmbir text i = spacer j = sino.shape[1] + svmbir_letters.shape[1] + spacer logo = copy_in(logo, svmbir_text, i, j) # Add the horizontal bar and the MBIR text i = spacer + sino.shape[0] j = spacer white_bar_hor = np.ones((spacer, sino.shape[1] + spacer + svmbir_letters.shape[1])) logo = copy_in(logo, white_bar_hor, i, j) i = i + spacer logo = copy_in(logo, mbir_letters, i, j) # Display the logo plt.imshow(logo, cmap=plt.cm.Greys_r) plt.show() imsave('logo.png', logo)
def _rotate(img, k): return sktf.rotate(img, angle=k, mode='constant', cval=0.0, resize=False)
def deskew(self,_img,principal_angle=11.25): grayscale = rgb2gray(_img) angle = principal_angle + determine_skew(grayscale) rotated = rotate(_img, angle, resize=True) * 255 print(-angle) return -principal_angle,rotated.astype(np.uint8)
sobel = filters.sobel(gray) # image = zoom(image, 0.4) t2 = time.time() r = transform.radon(sobel) print("radon transform cost time: {}s".format(time.time() - t2)) # print(r.shape) (m, n) = r.shape c = 1 for i in range(m): for j in range(n): if r[0, 0] < r[i, j]: r[0, 0] = r[i, j] c = j angle = 90 - c dst = transform.rotate(image, angle) print("cost time: {}s".format(time.time() - t1)) plt.figure() plt.subplot(321) plt.title("source image") plt.imshow(image) plt.subplot(322) plt.title("gray image") plt.imshow(gray) plt.subplot(323) plt.title("sobel") plt.imshow(sobel)
def __call__(self, img): angle = np.random.choice(a = self.rotations,p = self.rotation_probabilities) img = transform.rotate(angle=angle,image=img,mode='edge') return img
start_time = time.time() train_labels = pd.read_csv("C:/work/dEYENET/labels/trainLabels_master.csv") train_labels['image'] = train_labels['image'].str.rstrip('.jpeg') train_labels_no_DR = train_labels[train_labels['level'] == 0] train_labels_DR = train_labels[train_labels['level'] >= 1] image_list_no_DR = [i for i in train_labels_no_DR['image']] image_list_DR = [i for i in train_labels_DR['image']] print("Mirroring Non-DR Images") mirror_img('C:/work/dEYENET/train_resized_256/', 1, image_list_no_DR) # Rotate all images that have any level of DR print("Rotating 90 Degrees") rotate('C:/work/dEYENET/train_resized_256/', 90, image_list_DR) print("Rotating 120 Degrees") rotate('C:/work/dEYENET/train_resized_256/', 120, image_list_DR) print("Rotating 180 Degrees") rotate('C:/work/dEYENET/train_resized_256/', 180, image_list_DR) print("Rotating 270 Degrees") rotate('C:/work/dEYENET/train_resized_256/', 270, image_list_DR) print("Mirroring DR Images") mirror_img('C:/work/dEYENET/train_resized_256/', 0, image_list_DR) print("Completed") print("--- %s seconds ---" % (time.time() - start_time))
import matplotlib.pyplot as plt # im = data.astronaut() # img1 = rgb2gray(im) # brief 用来做SFM不行 from skimage import io, color img1 = color.rgb2gray( io.imread('E:/OwnWork/Leaf/TestImage/leafs/leafpgm/leaf1.jpg')) img2 = color.rgb2gray( io.imread('E:/OwnWork/Leaf/TestImage/leafs/leafpgm/leaf2.jpg')) # tform = tf.AffineTransform(scale=(1.2, 1.2), translation=(0, -100)) # img2 = tf.warp(img1, tform) img3 = tf.rotate(img1, 25) keypoints1 = corner_peaks(corner_harris(img1), min_distance=5) keypoints2 = corner_peaks(corner_harris(img2), min_distance=5) keypoints3 = corner_peaks(corner_harris(img3), min_distance=5) extractor = BRIEF() extractor.extract(img1, keypoints1) keypoints1 = keypoints1[extractor.mask] descriptors1 = extractor.descriptors extractor.extract(img2, keypoints2) keypoints2 = keypoints2[extractor.mask] descriptors2 = extractor.descriptors
def image_rotated(request, image_ref): from skimage.transform import rotate yield rotate(image_ref, request.param, order=1)
def __getitem__(self, index, vid=None): ''' Returns transformed image and mask (0-1 range, float) ''' self.file_lock.acquire() with h5py.File(self.fname) as h5file: if self.return_volume: # return volume samples = h5file["samples"]["coronal"] masks = h5file["masks"]["coronal"] vi = self.volume_ids[index] if self.adni: image, target = self.adni_vols.get_by_name(vi) else: image = np.zeros(self.volume_shape, dtype=np.float32) target = np.zeros(self.volume_shape, dtype=np.float32) j = 0 for i in self.ids: if vid is not None: vi = vid if i.split("_")[0] == vi: # Rotate back to original volume orientation image[:, j, :], target[:, j, :] = rotate( samples.get(i)[:], -90, resize=True), rotate(masks.get(i)[:], -90, resize=True) j += 1 print("Subject " + str(vi)) else: # return slice samples = h5file["samples"][self.orientation] masks = h5file["masks"][self.orientation] i = self.ids[index] if self.e2d: preindex = index - 1 if preindex < 0: preindex = index postindex = index + 1 if postindex == len(self.ids): postindex = index pri = self.ids[preindex] poi = self.ids[postindex] center_img, target = samples.get(i)[:], masks.get(i)[:] image = np.zeros( (3, center_img.shape[0], center_img.shape[1]), dtype=center_img.dtype) vol_i = i.split('_')[0] if pri.split('_')[0] != vol_i: pri = i if poi.split('_')[0] != vol_i: poi = i image[0] = samples.get(pri)[:] image[1] = center_img image[2] = samples.get(poi)[:] else: image, target = samples.get(i)[:], masks.get(i)[:] self.file_lock.release() if self.float16 and not self.return_volume: image, target = self.tofloat32(image, target) if self.transform is not None: image, target = self.transform(image, target) return image, target
def Rotate(image_array, max_right_degree=10, max_left_degree=10): random_degree = random.uniform(-max_right_degree, max_left_degree) return rotate(image_array, random_degree)