def image_gallery_png(self, ch5, ofile, n_galleries=50, rsfactor=0.4, gsize=100): """Resolution of png gallerie can be adjusted by the resampling factor (default=0.4). File size is large""" for name in sorted(self.data.keys()): data = self.data[name] if data is None: continue image = np.array([]) for objidx, track, coords in data.itertracks(n_galleries): pos = ch5.get_position(coords.well, coords.position) img = self._load_gallery(pos, objidx, coords.region, gsize) img = self._draw_labels(img, track) try: image = np.vstack((image, img)) except ValueError: image = img fn = ofile.replace('_gallery.png', '-%s_gallery.png' % name) vimage = vigra.RGBImage(image.swapaxes(1, 0)) vimage = vigra.sampling.resampleImage(vimage, rsfactor) vimage.writeImage(fn)
def make_gallery(self, grey_subimages=True): # n_ch >= 2 n_ch = len(self._channel.merge_regions)+1 self.make_target_dir() holder = self._channel.get_region(self._channel.regkey) for label in holder: iname = self.gallery_name(label) center = holder[label].oCenterAbs gallery = GalleryRGBImage(((n_ch)*self._size, self._size, 3), dtype=np.uint8, nsub=n_ch) for i, (channel, region) in enumerate(self._channel.sub_channels()): image = channel.meta_image.image.toArray() hexcolor = Colors.channel_hexcolor(channel.strChannelId) sholder = channel.get_region(region) sample = sholder[label] roi, coff = self._i_sub_image(center, image.shape) sub_img = self.cut(image, roi) gallery.set_sub_image(i, sub_img, hexcolor, grey_subimages) # contour in a single sub image contour = np.array(sample.crack_contour) - \ np.array(sample.oCenterAbs) contour += self._size/2 + coff gallery.add_contour(i, contour, sample.strHexColor) gallery.draw_contour() gallery.draw_merge_contour(holder[label].strHexColor) vigra.RGBImage(gallery).writeImage(iname)
def cellImage2display(cellImage, background=None, nodeColor=(0, 0, 255), edgeColor=(255, 0, 0)): assert False, "cellImage2display needs to be ported to vigranumpy" if hasattr(cellImage, "cellImage"): cellImage = cellImage.cellImage if hasattr(cellImage, "serialize"): cellImage = cellImage.serialize() if background is None: background = cellImage / 4 if background.bands() < 3: background = vigra.RGBImage(background) if background.size() != cellImage.size(): nbg = vigra.RGBImage(cellImage.size()) shift = Point2D((cellImage.width() - background.width()) / 2, (cellImage.height() - background.height()) / 2) nbg.subImage(Rect2D(shift, background.size())).copyValues(background) background = nbg return transformImage( cellImage, background, "\l ci, b: ci %% 4 == 1 ? %r : (ci %% 4 == 2 ? %r : b)" % (edgeColor, nodeColor))
def make_gallery(self): if self._write_tables: self.write_center_tables() for startid, centers in self.centers.iteritems(): iname = join(self._outdir, "P%s__T%05d__O%04d__B%02d.png" \ %((self.position, )+self._split_nodeid(startid))) gallery = GalleryRGBImage( ((len(centers)) * self._size, self._size, 3), dtype=np.uint8, nsub=len(centers)) for i, (frame, objid, center) in enumerate(centers): ifile = join(self._indir, 'P%s_T%05d.jpg' % (self.position, frame)) image = self.load_image(ifile) bbox = self._i_sub_image(center, image.shape[:2]) sub_img = self.cut(image, bbox) gallery.set_sub_image(i, sub_img) vigra.RGBImage(gallery).writeImage(iname)
def adjust_images(in_folder, out_folder, max_width, max_height): image_names = os.listdir(in_folder) image_names = sorted( filter( lambda x: os.path.splitext(x)[-1].lower() in ['.tif', '.tiff', '.png', '.jpg'], image_names)) ref_img = vigra.RGBImage((max_width, max_height)) if not os.path.exists(out_folder): os.makedirs(out_folder) print 'made %s' % out_folder for image_name in image_names: img = vigra.readImage(os.path.join(in_folder, image_name)) width = img.shape[0] height = img.shape[1] if width <= max_width and height <= max_height: avg_color = get_corner_color(img, 5) ref_img[:, :, :] = avg_color offset_x = (max_width - width) / 2 offset_y = (max_height - height) / 2 ref_img[offset_x:offset_x + width, offset_y:offset_y + height, :] = img elif width > max_width and height > max_height: # in this case, we have a crop situation offset_x = (width - max_width) / 2 offset_y = (height - max_height) / 2 ref_img = img[offset_x:offset_x + max_width, offset_y:offset_y + max_height, :] # export filename = os.path.join(out_folder, image_name) vigra.impex.writeImage(ref_img, filename) return
def test_vigra_compare_rgb(): a = np.random.randn(3000000).reshape(1000,1000,3).astype(np.float32) avigra = vigra.RGBImage(a) sigmas = [1.0, 5.0, 10.0] for order in [0,1,2]: for sigma in sigmas: res_ff = ff.gaussian2d(a, order, sigma) res_vigra = np.zeros_like(a) for c in range(avigra.shape[2]): res_vigra[:,:,c] = vigra.filters.gaussianDerivative(a[:,:,c], sigma, [order,order]) print("gaussian ", order, sigma, np.max(np.abs(res_ff - res_vigra))) if not np.allclose(res_ff, res_vigra, atol=1e-6): raise Exception("FAIL: ", order, sigma, np.max(np.abs(res_ff - res_vigra))) for sigma in sigmas: res_ff = ff.gradmag2d(a, sigma) res_vigra = vigra.filters.gaussianGradientMagnitude(avigra, sigma, accumulate=False) print("gradmag2d ", sigma, np.max(np.abs(res_ff - res_vigra))) if not np.allclose(res_ff, res_vigra, atol=1e-6): import IPython; IPython.embed() raise Exception("FAIL: gradmag2d ", sigma, np.max(np.abs(res_ff - res_vigra))) for sigma in sigmas: res_ff = ff.laplacian2d(a, sigma) res_vigra = vigra.filters.laplacianOfGaussian(avigra, sigma) print("laplacian2d ", sigma, np.max(np.abs(res_ff - res_vigra))) if not np.allclose(res_ff, res_vigra, atol=1e-6): raise Exception("FAIL: laplacian2d ", sigma, np.max(np.abs(res_ff - res_vigra))) '''
import fastfilters as ff import numpy as np import sys try: import vigra except ImportError: print("WARNING: vigra not available - skipping tests.") with open(sys.argv[1], 'w') as f: f.write('') exit() a = np.random.randn(3000000).reshape(1000, 1000, 3).astype(np.float32) avigra = vigra.RGBImage(a) sigmas = [1.0, 5.0, 10.0] for order in [0, 1, 2]: for sigma in sigmas: res_ff = ff.gaussian2d(a, order, sigma) res_vigra = np.zeros_like(a) for c in range(avigra.shape[2]): res_vigra[:, :, c] = vigra.filters.gaussianDerivative( a[:, :, c], sigma, [order, order]) print("gaussian ", order, sigma, np.max(np.abs(res_ff - res_vigra))) if not np.allclose(res_ff, res_vigra, atol=1e-6): raise Exception("FAIL: ", order, sigma, np.max(np.abs(res_ff - res_vigra)))