1
votes

Afternoon,

I am doing some deep learning with whole slide images (biomedical images of physical samples). Im using openslide to tile up the whole slide image (5gb in size) into smaller patches of the whole image and then convert this to RGB color space (I believe the patch is in RGBA as a PIL image object). If I use 5x magnification I find that When I save the image down I find a weird noise is present in the pixel that looks like a lot of the pixels are fluorescent (not present in higher magnifications). My question is does anyone knows why the Openslide library read_region function is causing this or if they know how I can do some post processing to remove this artifact from the image? I have tried to ways of convering the image see below

1.

for w in range(boundaries[0][0], boundaries[0][1], 500):
        for h in range(boundaries[1][0], boundaries[1][1], 500):
            patch = scan.read_region((w-2000, h-2000), 3, (500, 500))
            img2 = img[h-2000:h+2000, w-2000:w+2000,:]
            patch=np.asarray(patch)
            patchRGB = cv2.cvtColor(patch, cv2.COLOR_RGBA2RGB)
            print(np.mean(patchRGB), img2.shape)
            if (img2.shape == (4000, 4000, 3) and np.mean(patchRGB) < 200) and np.mean(patchRGB) > 50:
                    cv2.imwrite('output/test/images/'+os.path.basename(ndpi)[:-5]+'_'+str(w)+'_'+str(h)+'.png', patchRGB)
                    cv2.imwrite('output/test/masks/'+os.path.basename(ndpi)[:-5]+'_'+str(w)+'_'+str(h)+'_masks.png', img2) 

2.

for w in range(boundaries[0][0], boundaries[0][1], 500):
        for h in range(boundaries[1][0], boundaries[1][1], 500):
            patch = scan.read_region((w-2000, h-2000), 3, (500, 500))
            img2 = img[h-2000:h+2000, w-2000:w+2000,:]
            patchRGB = patch.convert('RGB')
            print(np.mean(patchRGB), img2.shape)
            if (img2.shape == (4000, 4000, 3) and np.mean(patchRGB) < 200) and np.mean(patchRGB) > 50:
                    patchRGB.save('output/5x/images/'+os.path.basename(ndpi)[:-5]+'_'+str(w)+'_'+str(h)+'.png')
                    cv2.imwrite('output/5x/masks/'+os.path.basename(ndpi)[:-5]+'_'+str(w)+'_'+str(h)+'_masks.png', img2) 

patch from whole slide image containing noise

1
The way you have asked your question makes the number of people likely to answer you vanishingly small. You are looking for someone who knows OpenSlide, PIL/Pillow, OpenCV, Numpy and who is prepared to work out all the missing import statements, wade through all your superfluous nested loops, unnecessary file writing and name generation, and guess what your image should look like. If you removed the loops and looked at one square, removed the file-writing, added the import statements and provided "before/correct" and "after/incorrect" images, you'll hopefully get luckier. Good luck.Mark Setchell
Thanks for feedback on title - I will edit the code and concentrate on a single image. I included the file writing as I wasn't sure if it was something that was happening whilst writing the file down.maracuja
Can you specify when this happens? After the conversion or after the magnification? What should be in place of these pixels?code-lukas
It could be that this is either an interpolation error during magnification or some sort of overflow during the RGB conversion...code-lukas

1 Answers

0
votes

I would start by breaking the question down to find out where the problem is introduced.

  1. Start by checking the file. Try opening it in QuPath and make sure the file opens with no errors and the noise is not present. I sometimes find noise from the slide scanner and have to re-scan.
  2. Check how the openslide patch looks. I like to use a Jupyter notebook for this so that you can quickly call plt.imshow(patch) to see how it looks.
  3. Then check the RGB conversion. plt.imshow(patchRGB) to visualise it and then see if the noise is there yet.
  4. If it still looks dodgy at this point then I would look into trying different saving methods and see if the noise is introduced there.