def test_threads(num_threads): # not testing if we use multiple threads # just checking if the API throws an exception img = 23 * np.ones((100, 100), dtype=np.uint8) background = rolling_ball(img, radius=10, num_threads=num_threads) background = rolling_ball(img, radius=10, nansafe=True, num_threads=num_threads)
def _rolling_ball(tile, ball_radius: int, light_background: bool): """ Applies the rolling-ball algorithm to a single tile. Args: tile: A tile, usually from an ome.tif file. ball_radius: The radius of the ball to use for calculating the background. light_background: Whether the image has a light background. Returns: An image with its background subtracted away. """ # Get the shape of the original image, so we can reshape the result at the end. shape = numpy.shape(tile) # squeeze the image into a 2-d array tile = numpy.squeeze(tile) # invert the image if it has a light background if light_background: tile = util.invert(tile) # use the rolling ball algorithm to calculate the background and subtract it from the image. background = restoration.rolling_ball(tile, radius=ball_radius) tile = tile - background # if the image had a light backend, invert the result. result = util.invert(tile) if light_background else tile result = numpy.reshape(result, shape) return result
def test_correctness(self): # calculate the result with the plugin code with BioReader(self.infile.name) as reader: with BioWriter(self.outfile.name, metadata=reader.metadata) as writer: rolling_ball( reader=reader, writer=writer, ball_radius=self.ball_radius, light_background=False, ) # read the image we just wrote into a numpy array with BioReader(self.outfile.name) as reader: plugin_result = reader[:] # calculate the true result background = restoration.rolling_ball(self.random_image, radius=self.ball_radius) true_result = self.random_image - background # assert correctness self.assertTrue(numpy.all(numpy.equal(true_result, plugin_result)), f'The plugin resulted in a different image') return
def test_radial_gradient(): # spot light source at top left corner spot_radius = 50 x, y = np.meshgrid(range(5), range(5)) img = np.sqrt(np.clip(spot_radius**2 - y**2 - x**2, 0, None)) background = rolling_ball(img, radius=5) assert np.allclose(img - background, np.zeros_like(img))
def test_linear_gradient(): # linear light source centered at top left corner x, y = np.meshgrid(range(100), range(100)) img = (y * 20 + x * 20) expected_img = 19 * np.ones_like(img) expected_img[0, 0] = 0 background = rolling_ball(img, radius=1) assert np.allclose(img - background, expected_img)
def test_preserve_peaks(radius): x, y = np.meshgrid(range(100), range(100)) img = 0 * x + 0 * y + 10 img[10, 10] = 20 img[20, 20] = 35 img[45, 26] = 156 expected_img = img - 10 background = rolling_ball(img, radius=radius) assert np.allclose(img - background, expected_img)
def test_nan_const(): img = 123 * np.ones((100, 100), dtype=float) img[20, 20] = np.nan img[50, 53] = np.nan kernel_shape = (10, 10) x = np.arange(-kernel_shape[1] // 2, kernel_shape[1] // 2 + 1)[np.newaxis, :] y = np.arange(-kernel_shape[0] // 2, kernel_shape[0] // 2 + 1)[:, np.newaxis] expected_img = np.zeros_like(img) expected_img[y + 20, x + 20] = np.nan expected_img[y + 50, x + 53] = np.nan kernel = ellipsoid_kernel(kernel_shape, 100) background = rolling_ball(img, kernel=kernel, nansafe=True) assert np.allclose(img - background, expected_img, equal_nan=True)
def image_list_for_montage(image_name): pixels = tfi.imread(image_name) pixels_float = pixels.astype('float64') pixels_float = pixels_float / 65535.000 ch = pixels_float[:,:,0] input_gs_image = ch input_gs_image = (input_gs_image / input_gs_image.max()) * 255 ch2_u8 = np.uint8(input_gs_image) background = restoration.rolling_ball(ch2_u8, radius=3) image_resized = resize(background, (background.shape[0] // 4, background.shape[1] // 4),anti_aliasing=True) image_resized = np.uint8(image_resized*255) rgb_input_img = np.zeros((np.shape(image_resized)[0], np.shape(image_resized)[1], 3), dtype=np.uint8) rgb_input_img[:, :, 0] = image_resized rgb_input_img[:, :, 1] = image_resized rgb_input_img[:, :, 2] = image_resized rgb_input_img = show_image_adjust(rgb_input_img, 8, 100) return rgb_input_img
def test_const_image(radius): # infinite plane light source at top left corner img = 23 * np.ones((100, 100), dtype=np.uint8) background = rolling_ball(img, radius=radius) assert np.allclose(img - background, np.zeros_like(img))
def test_ellipsoid_const(dtype): img = 155 * np.ones((100, 100), dtype=dtype) kernel = ellipsoid_kernel((25, 53), 50) background = rolling_ball(img, kernel=kernel) assert np.allclose(img - background, np.zeros_like(img)) assert background.dtype == img.dtype
ax[0].axis('off') ax[1].imshow(background, cmap='gray') ax[1].set_title('Background') ax[1].axis('off') ax[2].imshow(image - background, cmap='gray') ax[2].set_title('Result') ax[2].axis('off') fig.tight_layout() image = data.coins() background = restoration.rolling_ball(image) plot_result(image, background) plt.show() ###################################################################### # White background # ---------------- # # If you have dark features on a bright background, you need to invert # the image before you pass it into the algorithm, and then invert the # result. This can be accomplished via: image = data.page() image_inverted = util.invert(image)
def time_rollingball_ndim(self): from skimage.restoration.rolling_ball import ellipsoid_kernel image = data.cells3d()[:, 1, ...] kernel = ellipsoid_kernel((1, 100, 100), 100) restoration.rolling_ball(image, kernel=kernel)
def time_rollingball_nan(self, radius): image = data.coins().astype(float) pos = np.arange(np.min(image.shape)) image[pos, pos] = np.NaN restoration.rolling_ball(image, radius=radius, nansafe=True)
def peakmem_rollingball(self, radius): restoration.rolling_ball(data.coins(), radius=radius)
def time_rollingball(self, radius): restoration.rolling_ball(data.coins(), radius=radius)
montage_DSP = np.concatenate((rowB,rowC,rowD,rowE,rowF),0) plt.imshow(montage_DSP) plt.imsave(os.path.join("/Users/kanferg/Desktop/NIH_Youle/Lab_meeting/20220318/Images/image_processed_DSP","montage_DSP.png"),montage_DSP) pixels = tfi.imread(image_name_sel[1]) pixels_float = pixels.astype('float64') pixels_float = pixels_float / 65535.000 ch = pixels_float[:,:,0] input_gs_image = ch input_gs_image = (input_gs_image / input_gs_image.max()) * 255 ch2_u8 = np.uint8(input_gs_image) background = restoration.rolling_ball(ch2_u8, radius=3) image_resized = resize(background, (background.shape[0] // 4, background.shape[1] // 4),anti_aliasing=True) plt.imshow(np.uint8(image_resized*255)) fig = plt.figure("ch1") plt.imshow(ch) fig = plt.figure("BG") plt.imshow(background) well_list_temp = well_name[0:2] image_list = [] for i in range(len(2)):
def test_ndim(): image = data.cells3d()[:5, 1, ...] kernel = ellipsoid_kernel((3, 100, 100), 100) rolling_ball(image, kernel=kernel)
def time_rollingball_threads(self, threads): restoration.rolling_ball(data.coins(), radius=100, num_threads=threads)