def test_jp2k_16b_regression(self): """ Regression test for 16b Jpeg 2K compressed image. Read the block from a slide and compares it with a reference image. The reference image is obtained by reading of the same region by the slideo and savint it as a tif file. """ # Image to test image_path = get_test_image_path("svs", "jp2k_1chnl.svs") # Image to test reference_image_path = get_test_image_path( "svs", "jp2k_1chnl.regression_x700_y400_w500_y400.tif") # Read reference image reference_image = cv.imread(reference_image_path, cv.IMREAD_UNCHANGED) x_beg = 700 y_beg = 400 width = 500 height = 400 rect = (x_beg, y_beg, width, height) new_size = (100, 80) # open the slide slide = slideio.open_slide(image_path, "SVS") self.assertTrue(slide is not None) scene = slide.get_scene(0) self.assertTrue(scene is not None) block_raster = scene.read_block(rect, size=new_size) self.assertTrue(np.array_equal(block_raster, reference_image))
def test_jp2k_16b_read_block(self): """ Read a block of Jpeg2K compressed 16 bit image. Reads a block from a slide and compares it with a block exported by ImageScope software. """ # Image to test image_path = get_test_image_path("svs", "jp2k_1chnl.svs") # Image to test reference_image_path = get_test_image_path( "svs", "jp2k_1chnl.region_x700_y400_w500_y400.tif") # Read reference image reference_image = cv.imread(reference_image_path, cv.IMREAD_UNCHANGED) x_beg = 700 y_beg = 400 width = 500 height = 400 # Read region with slideio slide = slideio.open_slide(image_path, "SVS") self.assertTrue(slide is not None) scene = slide.get_scene(0) self.assertTrue(scene is not None) region = scene.read_block((x_beg, y_beg, width, height)) # call structural difference score scores_cf = cv.matchTemplate(region.astype(np.float32), reference_image.astype(np.float32), cv.TM_CCOEFF_NORMED)[0][0] # calculate square normed errors scores_sq = cv.matchTemplate(region.astype(np.float32), reference_image.astype(np.float32), cv.TM_SQDIFF_NORMED)[0][0] self.assertLess(0.99, scores_cf) self.assertLess(scores_sq, 0.0002)
def test_jpegxr_rgb_resample_block(self): """ Read a block of rgb jpegxr compressed image. Read the block from a slide and compares it with a reference image. """ # Image to test image_path = get_test_image_path("czi", "jxr-rgb-5scenes.czi") # Reference channel images ref_image_paths = get_test_image_path( "czi", "jxr-rgb-5scenes/jxr-rgb-5scenes_s3_x2500_y3000_w400_h600.png") scaling_params = {1.0: 0.99, 1.5: 0.94, 2.0: 0.92, 3.0: 0.85} reference_image = cv.imread(ref_image_paths) slide = slideio.open_slide(image_path, "CZI") scene = slide.get_scene(3) # check accuracy of resizing for different scale coeeficients for resize_cof, thresh in scaling_params.items(): new_size = (int(round(reference_image.shape[1] / resize_cof)), int(round(reference_image.shape[0] // resize_cof))) reference_raster = cv.resize(reference_image, new_size) image_raster = scene.read_block((2500, 3000, 400, 600), size=new_size, channel_indices=[0, 1, 2]) scores_cf = cv.matchTemplate(reference_raster, image_raster, cv.TM_CCOEFF_NORMED)[0][0] self.assertLess(thresh, scores_cf)
def test_jp2k_16b_resample_block(self): """ Test for resampling of a 16bit block. A region of 16 bit, jpeg 2000 compressed image is read and resized with different scale. An reference block is exportded by ImageScope software on scale 1:1 and resized with opencv with the same scale coefficients as the original region. """ # Image to test image_path = get_test_image_path("svs", "jp2k_1chnl.svs") # Image to test reference_image_path = get_test_image_path( "svs", "jp2k_1chnl.region_x700_y400_w500_y400.tif") # Read reference image reference_image = cv.imread(reference_image_path, cv.IMREAD_UNCHANGED) x_beg = 700 y_beg = 400 width = 500 height = 400 rect = (x_beg, y_beg, width, height) size = (width, height) # open the slide slide = slideio.open_slide(image_path, "SVS") self.assertTrue(slide is not None) scene = slide.get_scene(0) self.assertTrue(scene is not None) scaling_params = { 1.0: (0.9999, 2.e-7), 1.5: (0.99, 0.002), 2.0: (0.999, 1.e-7), 3.0: (0.98, 0.01), 5.0: (0.98, 0.01) } for resize_cof, params in scaling_params.items(): new_size = (int(round(size[0] / resize_cof)), int(round(size[1] / resize_cof))) resized_raster = scene.read_block(rect, size=new_size) cv_resized_raster = cv.resize(reference_image, new_size) # call structural difference score score_cf = cv.matchTemplate(resized_raster.astype(np.float32), cv_resized_raster.astype(np.float32), cv.TM_CCOEFF_NORMED)[0][0] # calculate square normed errors score_sq = cv.matchTemplate(resized_raster.astype(np.float32), cv_resized_raster.astype(np.float32), cv.TM_SQDIFF_NORMED)[0][0] self.assertLess(params[0], score_cf) self.assertLess(score_sq, params[1])
def test_jpegxr_rgb_regression(self): """ Regression test for rgb jpegxr compressed image. Read the block from a slide and compares it with a reference image. The reference image is obtained by reading of the same region by the slideo and saving it as png file. """ image_name = "jxr-rgb-5scenes.czi" folder = "czi" driver = "CZI" ext = ".png" # Image to test image_path = get_test_image_path(folder, image_name) rect = (4000, 2000, 1000, 800) new_size = (250, 200) scene_index = 2 # export_image_region(folder, # image_name, driver, scene_index, # rect, new_size, ext # ) slide = slideio.open_slide(image_path, driver) scene = slide.get_scene(scene_index) for channel_index in range(scene.num_channels): image_raster = scene.read_block(rect, new_size, channel_indices=[channel_index]) regr_image_path = get_regression_image_path( folder, image_name, scene_index, channel_index, rect, ext) reference_image = cv.imread(regr_image_path, cv.IMREAD_UNCHANGED) self.assertTrue(np.array_equal(image_raster, reference_image))
def test_jpegxr_16b_read_block(self): """ Read a block of jpegxr compressed 16 bit image. Reads a block from a slide and compares it with a block read by czifile package. Small deviations can be explained by different order in processing of tiles. Czi file contains overlapped tiles. Rasters can be slightly different in the overlapped areas (depends of tile order). """ # Image to test image_path = get_test_image_path("czi", "jxr-16bit-4chnls.czi") slide = slideio.open_slide(image_path, "CZI") self.assertTrue(slide is not None) scene = slide.get_scene(0) self.assertTrue(scene is not None) image_raster = scene.read_block() reference_raster = np.transpose( czi.imread(image_path)[0, 0, 0, :, :, :, 0], (1, 2, 0)) image_raster = image_raster[:, :, 0] reference_raster = reference_raster[:, :, 0] # calculate square normed errors scores_sq = cv.matchTemplate(reference_raster.astype(np.float32), image_raster.astype(np.float32), cv.TM_SQDIFF_NORMED)[0][0] # call structural difference score scores_cf = cv.matchTemplate(reference_raster.astype(np.float32), image_raster.astype(np.float32), cv.TM_CCOEFF_NORMED)[0][0] self.assertLess(0.99, scores_cf) self.assertLess(scores_sq, 0.002)
def test_jpegxr_rgb_read_block(self): """ Read a block of rgb jpegxr compressed image. Read the block from a slide and compares it with a reference image. """ # Image to test image_path = get_test_image_path("czi", "jxr-rgb-5scenes.czi") # Reference channel images ref_image_paths = get_test_image_path( "czi", "jxr-rgb-5scenes/jxr-rgb-5scenes_s3_x2500_y3000_w400_h600.png") slide = slideio.open_slide(image_path, "CZI") self.assertTrue(slide is not None) scene = slide.get_scene(3) self.assertTrue(scene is not None) image_raster = scene.read_block((2500, 3000, 400, 600)) reference_raster = cv.imread(ref_image_paths, cv.IMREAD_UNCHANGED) scores_cf = cv.matchTemplate(reference_raster, image_raster, cv.TM_CCOEFF_NORMED)[0][0] self.assertLess(0.99, scores_cf)
def test_jpegxr_rgb_regression_small(self): """ Regression test for rgb jpegxr compressed image. Read the block from a slide and compares it with a reference image. The reference image is obtained by reading of the same region by the slideo and saving it as png file. Old version of the jpegxr codec delivered corupted images for the scene 2 with low scaling """ # Image to test image_path = get_test_image_path("czi", "jxr-rgb-5scenes.czi") # Reference channel images ref_image_path = get_test_image_path( "czi", "jxr-rgb-5scenes.czi.regression_s2.png") reference_image = cv.imread(ref_image_path, cv.IMREAD_UNCHANGED) slide = slideio.open_slide(image_path, "CZI") scene = slide.get_scene(2) # check accuracy of resizing for different scale coeeficients image_raster = scene.read_block(size=(400, 0)) self.assertTrue(np.array_equal(image_raster, reference_image))
def test_jpegxr_16b_regression(self): """ Regression test for 16b jpegxr compressed image. Read the block from a slide and compares it with a reference image. The reference image is obtained by reading of the same region by the slideo and savint it as a tif file. """ # Image to test image_path = get_test_image_path("czi", "jxr-16bit-4chnls-2.czi") # Reference image ref_image_path = get_test_image_path( "czi", "jxr-16bit-4chnls-2/regression_x5000_y2000_w1000_h500.tif") region_size = (1000, 500) region_new_size = (300, 150) region_rect = (5000, 2000, region_size[0], region_size[1]) slide = slideio.open_slide(image_path, "CZI") self.assertTrue(slide is not None) scene = slide.get_scene(0) self.assertTrue(scene is not None) image = scene.read_block(region_rect, size=region_new_size) reference_image = cv.imread(ref_image_path, cv.IMREAD_UNCHANGED) self.assertTrue(np.array_equal(image, reference_image))
def test_jpegxr_16b_resample_block(self): """ Test for resampling of a block. A region of 16 bit, jpegxr compressed image is read with 1:1 scale and resampled with different scales. 1:1 block is resized in memory by opencv and compared with resampled region from slideio. """ # Image to test image_path = get_test_image_path("czi", "jxr-16bit-4chnls-2.czi") region_size = (1000, 500) region_rect = (5000, 2000, region_size[0], region_size[1]) slide = slideio.open_slide(image_path, "CZI") self.assertTrue(slide is not None) scene = slide.get_scene(0) self.assertTrue(scene is not None) origin_raster = scene.read_block(region_rect) scaling_params = { 1.0: (0.9999, 1.e-7), 1.5: (0.99, 0.0004), 2.0: (0.98, 0.0035), 3.0: (0.95, 0.007), 5.0: (0.95, 0.008) } for resize_cof, params in scaling_params.items(): new_size = (int(round(region_size[0] / resize_cof)), int(round(region_size[1] / resize_cof))) resized_raster = scene.read_block(region_rect, size=new_size) cv_resized_raster = cv.resize(origin_raster, new_size) # call structural difference score scores_cf = cv.matchTemplate(resized_raster.astype(np.float32), cv_resized_raster.astype(np.float32), cv.TM_CCOEFF_NORMED)[0][0] # calculate square normed errors scores_sq = cv.matchTemplate(resized_raster.astype(np.float32), cv_resized_raster.astype(np.float32), cv.TM_SQDIFF_NORMED)[0][0] self.assertLess(params[0], scores_cf) self.assertLess(scores_sq, params[1])