def __readimage(self, fn): try: im = imageio.volread(fn) except Exception: # require tifffile only if imageio fails from skimage.external.tifffile import tifffile im = tifffile.imread(fn) return self.__fix_image_dimensions(im)
def getmaskarray(self): if self.mfn is None: return None try: im = imageio.imread(self.mfn, flatten=True) except Exception: # require tifffile only if imageio fails from skimage.external.tifffile import tifffile im = tifffile.imread(self.mfn) return im
def false_color_images(folder, files1, files2, k): beta2 = 0.05 beta4 = 1.00 beta6 = 0.544 beta1 = 0.65 beta3 = 0.85 beta5 = 0.35 temp_0 = tiff.imread(files1[k]) temp_1 = tiff.imread(files2[k]) temp_0 = temp_0.astype(float) temp_1 = temp_1.astype(float) temp_0 = temp_0 - 50 temp_1 = temp_1 - 50 ind = np.where(temp_0 < 0) temp_0[ind] = 0 ind = np.where(temp_1 < 0) temp_1[ind] = 0 #temp_0 = cv.resize(temp_0,(int(temp_0.shape[0]*1.414), temp_0.shape[1]), interpolation = cv.INTER_CUBIC) #temp_1 = cv.resize(temp_1,(int(temp_1.shape[0]*1.414), temp_1.shape[1]), interpolation = cv.INTER_CUBIC) im = np.zeros((temp_0.shape[0], temp_0.shape[1], 3)) im[:, :, 0] = np.multiply(np.exp(-temp_0 * beta1 / 1200), np.exp(-temp_1 * beta2 / 1200)) im[:, :, 1] = np.multiply(np.exp(-temp_0 * beta3 / 1200), np.exp(-temp_1 * beta4 / 1200)) im[:, :, 2] = np.multiply(np.exp(-temp_0 * beta5 / 1200), np.exp(-temp_1 * beta6 / 1200)) im = im * 255 im = im.astype('uint8') oldname = files1[k] #newname = oldname.replace('CH000001', '') newname = oldname.replace(folder, folder + '_rgb') tifffile.imsave(newname, im) return
def _get_batches_of_transformed_samples(self, index_array): batch_x = [] batch_y = [] for batch_index, image_index in enumerate(index_array): city, id = self.image_ids[image_index] for data_dir in self.data_dirs: city_dir_name = data_dir.split("/")[-1] if city in data_dir: img_name = self.image_name_template.format(id=id) if self.clahe: data_dir = os.path.join(self.wdata_dir, city_dir_name) path = os.path.join(data_dir, img_name) else: path = os.path.join(data_dir, img_name) break arr = tifffile.imread(path) image = np.stack([ arr[..., 4], arr[..., 2], arr[..., 1], arr[..., 0], arr[..., 3], arr[..., 5], arr[..., 6], arr[..., 7] ], axis=-1) if self.stretch_and_mean: image = stretch_8bit(image) * 255 if self.ohe_city: ohe_city = np.zeros((image.shape[0], image.shape[1], 4), dtype="float32") ohe_city[..., cities.index(city)] = 2047 image = np.concatenate([image, ohe_city], axis=-1) image = np.array(image, dtype="float32") lines = self.masks_dict[id] mask = np.zeros((image.shape[0], image.shape[1], 1)) # lines in wkt format, pygeoif if os.path.exists("masks/" + id + ".png"): mask = img_to_array( load_img("masks/" + id + ".png", grayscale=True)) / 255. else: mask = np.zeros((image.shape[0], image.shape[1], 1)) # lines in wkt format, pygeoif for line in lines: if "LINESTRING EMPTY" == line: continue points = pygeoif.from_wkt(line).coords for i in range(1, len(points)): pt1 = (int(points[i - 1][0]), int(points[i - 1][1])) pt2 = (int(points[i][0]), int(points[i][1])) cv2.line(mask, pt1, pt2, (1, ), thickness=self.thickness) cv2.imwrite("masks/" + id + ".png", mask * 255) ori_height = image.shape[0] ori_width = image.shape[1] mask = self.transform_mask(mask, image) if self.random_transformer is not None: image, mask = self.random_transformer.random_transform( image, mask) if self.stretch_and_mean: mean_band = mean_bands[city] for band in range(len(mean_band)): image[..., band] -= mean_band[band] if self.crop_shape is not None: crops = 0 tries = 0 while crops < self.crops_per_image: tries += 1 if self.random_transformer is None: y_start = (ori_height - self.crop_shape[0]) // 2 x_start = (ori_width - self.crop_shape[1]) // 2 else: y_start = random.randint( 0, ori_height - self.crop_shape[0] - 1) x_start = random.randint( 0, ori_width - self.crop_shape[1] - 1) y_end = y_start + self.crop_shape[0] x_end = x_start + self.crop_shape[1] crop_image = image[y_start:y_end, x_start:x_end, :] crop_mask = mask[y_start:y_end, x_start:x_end, :] if self.random_transformer is None: batch_x.append(crop_image) batch_y.append(crop_mask) crops += 1 elif np.count_nonzero(crop_image) > 100 or tries > 20: batch_x.append(crop_image) batch_y.append(crop_mask) crops += 1 else: batch_x.append(image) batch_y.append(mask) batch_x = np.array(batch_x, dtype="float32") batch_y = np.array(batch_y, dtype="float32") if self.preprocessing_function == 'caffe': batch_x_rgb = batch_x[..., :3] batch_x_bgr = batch_x_rgb[..., ::-1] batch_x[..., :3] = batch_x_bgr if not self.stretch_and_mean: batch_x = batch_x / 8. - 127.5 else: if self.stretch_and_mean: batch_x = batch_x / 255 else: batch_x = batch_x / 1024. - 1 return self.transform_batch_x(batch_x), self.transform_batch_y(batch_y)
def _get_batches_of_transformed_samples(self, index_array): batch_x = [] batch_y = [] for batch_index, image_index in enumerate(index_array): city, id = self.image_ids[image_index] for data_dir in self.data_dirs: city_dir_name = data_dir.split("/")[-1] if city in data_dir: img_name = self.image_name_template.format(id=id) if self.clahe: data_dir = os.path.join(self.wdata_dir, city_dir_name) path = os.path.join(data_dir, img_name) else: path = os.path.join(data_dir, img_name) break arr = tifffile.imread(path) image = np.stack([arr[..., 4], arr[..., 2], arr[..., 1], arr[..., 0], arr[..., 3], arr[..., 5], arr[..., 6], arr[..., 7]], axis=-1) if self.stretch_and_mean: image = stretch_8bit(image) * 255 if self.ohe_city: ohe_city = np.zeros((image.shape[0], image.shape[1], 4), dtype="float32") ohe_city[..., cities.index(city)] = 2047 image = np.concatenate([image, ohe_city], axis=-1) image = np.array(image, dtype="float32") lines = self.masks_dict[id] mask = np.zeros((image.shape[0], image.shape[1], 1)) # lines in wkt format, pygeoif if os.path.exists("masks/" + id + ".png"): mask = img_to_array(load_img("masks/" + id + ".png", grayscale=True)) / 255. else: mask = np.zeros((image.shape[0], image.shape[1], 1)) # lines in wkt format, pygeoif for line in lines: if "LINESTRING EMPTY" == line: continue points = pygeoif.from_wkt(line).coords for i in range(1, len(points)): pt1 = (int(points[i - 1][0]), int(points[i - 1][1])) pt2 = (int(points[i][0]), int(points[i][1])) cv2.line(mask, pt1, pt2, (1,), thickness=self.thickness) cv2.imwrite("masks/" + id + ".png", mask * 255) ori_height = image.shape[0] ori_width = image.shape[1] mask = self.transform_mask(mask, image) if self.random_transformer is not None: image, mask = self.random_transformer.random_transform(image, mask) if self.stretch_and_mean: mean_band = mean_bands[city] for band in range(len(mean_band)): image[..., band] -= mean_band[band] if self.crop_shape is not None: crops = 0 tries = 0 while crops < self.crops_per_image: tries += 1 if self.random_transformer is None: y_start = (ori_height - self.crop_shape[0]) // 2 x_start = (ori_width - self.crop_shape[1]) // 2 else: y_start = random.randint(0, ori_height - self.crop_shape[0] - 1) x_start = random.randint(0, ori_width - self.crop_shape[1] - 1) y_end = y_start + self.crop_shape[0] x_end = x_start + self.crop_shape[1] crop_image = image[y_start:y_end, x_start:x_end, :] crop_mask = mask[y_start:y_end, x_start:x_end, :] if self.random_transformer is None: batch_x.append(crop_image) batch_y.append(crop_mask) crops += 1 elif np.count_nonzero(crop_image) > 100 or tries > 20: batch_x.append(crop_image) batch_y.append(crop_mask) crops += 1 else: batch_x.append(image) batch_y.append(mask) batch_x = np.array(batch_x, dtype="float32") batch_y = np.array(batch_y, dtype="float32") if self.preprocessing_function == 'caffe': batch_x_rgb = batch_x[..., :3] batch_x_bgr = batch_x_rgb[..., ::-1] batch_x[..., :3] = batch_x_bgr if not self.stretch_and_mean: batch_x = batch_x / 8. - 127.5 else: if self.stretch_and_mean: batch_x = batch_x / 255 else: batch_x = batch_x / 1024. - 1 return self.transform_batch_x(batch_x), self.transform_batch_y(batch_y)
from skimage import filters gfiltA = filters.gaussian(A_grey, sigma=(10,10), multichannel=True) plt.imshow(gfiltA, cmap=plt.cm.gray) # In[184]: from skimage import filters sofiltA = filters.sobel(A_grey) plt.imshow(sofiltA, cmap=plt.cm.gray) # In[194]: from skimage.external.tifffile import tifffile # In[195]: mdimage=tifffile.imread("multipage_tif_example.tif", key=0) # In[ ]: