Skip to content

Is contrast enhancement applied on extracted patches ? #93

Open

Description

@abhi1998goyal

I notice a clear difference in patches provided in patch directory to those that are created using dump_patches. Is their any normalization applied on these patches after extraction from WSI ?

for st in iter_hest('data_path', id_list=['TENX99']):
patch_save_dir = 'path_dir'
new_patch_size = 256
st.dump_patches(patch_save_dir, name='demo', target_patch_size=new_patch_size, target_pixel_size=0.5
)
256*256 size at 0.5um/pixel images provided in patches directory of hest data is on left and one extracted from .tif using above method is on right side.

Image

Activity

  1. changed the title [-]Is stain normalization applied on extracted patches ?[/-] [+]Is contrast enhancement applied on extracted patches ?[/+] on Feb 6, 2025
  2. guillaumejaume commented on Feb 7, 2025

    @guillaumejaume
    Collaborator

    No contrast enhancement nor stain norm has been applied. What you show could be compression artifacts.

  3. abhi1998goyal commented on Feb 7, 2025

    @abhi1998goyal
    Author
    import h5py
    import numpy as np
    from PIL import Image
    
    h5_file_path = "/home/demo.h5"
    
    with h5py.File(h5_file_path, "r") as h5f:
    
        barcodes = h5f['barcode'][:]
        barcodes = barcodes.astype(str)  
    
        if '221x099' in barcodes:
           barcode_index = np.where(barcodes == '221x099')[0][0]
    
        img_patch = h5f["img"][barcode_index]  
        img = Image.fromarray(img_patch)
        img.save('/home/TENX99_221x099.png' , format="PNG", compress_level=0)
    

    It could be compression but I don't see why there should be compression. I am doing the same for both the patches/TENX99.h5 and demo.h5 created from TENX99.tif . The picture from patches/TENX99.h5 is far better. Please help !

  4. guillaumejaume commented on Feb 7, 2025

    @guillaumejaume
    Collaborator

    tbh, I don't see a huge difference between both images. Could you check the actual difference between both arrays using https://numpy.org/doc/2.1/reference/generated/numpy.allclose.html

  5. abhi1998goyal commented on Feb 8, 2025

    @abhi1998goyal
    Author
    import h5py
    import numpy as np
    from PIL import Image
    import os
    import matplotlib.pyplot as plt
    
    for st in iter_hest('/home/hest_data', id_list=['TENX99']):
        patch_save_dir = 'home/'
        new_patch_size = 224
        st.dump_patches(
        patch_save_dir,
        name='demo',
        target_patch_size=new_patch_size,
        target_pixel_size=0.5
    )
    
    h5_file_path = "/home/demo.h5"
    
    with h5py.File(h5_file_path, "r") as h5f:
        barcodes = h5f['barcode'][:]
        barcodes = barcodes.astype(str)  
        if '221x099' in barcodes:
           barcode_index = np.where(barcodes == '221x099')[0][0]
        img_patch = h5f["img"][barcode_index]  
    
    
    hest_h5_path = os.path.join('/home/hest_data/patches', "TENX99.h5")
    with h5py.File(hest_h5_path, "r") as h5f2:
        barcodes = h5f2['barcode'][:]
        barcodes = barcodes.astype(str)
        if '221x099' in barcodes:
           barcode_index1 = np.where(barcodes == '221x099')[0][0]
        img_patch_2=h5f2["img"][barcode_index1]    
    
    
    are_similar = np.allclose(img_patch, img_patch_2, atol=1) 
    print(are_similar)
    
    are_similar = np.allclose(img_patch, img_patch_2, atol=10) 
    print(are_similar)
    
    are_similar = np.allclose(img_patch, img_patch_2, atol=60) 
    print(are_similar)
    
    are_similar = np.allclose(img_patch, img_patch_2, atol=70) 
    print(are_similar)
    
    fig, axes = plt.subplots(1, 2, figsize=(6, 3))
    
    axes[0].imshow(img_patch)
    axes[0].set_title("Image from demo.h5")
    axes[0].axis("off")
    
    axes[1].imshow(img_patch_2)
    axes[1].set_title("Image from TENX99.h5")
    axes[1].axis("off")
    
    plt.tight_layout()
    plt.show()
    
    diff = 3*np.abs(img_patch.astype(int) - img_patch_2.astype(int)) 
    
    plt.imshow(diff, cmap="hot")  
    plt.colorbar()
    plt.title("Pixel Differences")
    plt.axis("off")
    plt.show()
    

    Output: False False False True
    Image

    Image

  6. pauldoucet commented on Feb 8, 2025

    @pauldoucet
    Collaborator

    Hi @abhi1998goyal,
    We slightly modified dump_patches after creating the patches in patches/. The variation you see might be due to a small offset/zoom difference between the old and the new implementation, but I don't think it affects the quality

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions