Colour correction of whole-slide image
Colour correction can be applied to microscopy images in order to standardized the colour representation. In this example, the colour of the source image will be matched to the target image which is supposed to be the standardized sample and it will be done using histogram matching method. This easily implemented on normal images. But limited by the computer memory size, it is hard to implement for overall whole-slide images with large size. So with libMI, whole-slide images as source image can be efficiently read and modified tile by tile to realize this process. The result is shown in the following figure.
# Read the original and target images x = 0 while x < source.GetWidth(): y = 0 while y < source.GetHeight(): w = min(1000, source.GetWidth() - x) h = min(1000, source.GetHeight() - y) source.SetCurrentRegion((x, y), (w, h)) source.SetRegionSize((w, h)) tile = source.ReadRegion() target.SetCurrentRegion((0, 0), (target.GetWidth(), target.GetHeight())) target.SetRegionSize((1000, 1000)) tar_top = target.ReadRegion() try: # Output image img_result = tile # Runt the loop through BGR channels for k in range(3): img_result[:, :, k] = hist_match(tile[:, :, k], tar_top[:, :, k]) source.WriteSegm(img_result,None) print('color correction: %d, %d' % (x, y)) except IndexError: print('backgroud: %d, %d' % (x, y)) y = y + 1000 x = x + 1000
We read the image data from source and target images, do color correction and write it back to the source image. We proceed this operation tile by tile until the whole image color correction is completed.