Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is contrast enhancement applied on extracted patches ? #93

Open
abhi1998goyal opened this issue Feb 6, 2025 · 5 comments
Open

Is contrast enhancement applied on extracted patches ? #93

abhi1998goyal opened this issue Feb 6, 2025 · 5 comments

Comments

@abhi1998goyal
Copy link

abhi1998goyal commented Feb 6, 2025

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

@abhi1998goyal abhi1998goyal changed the title Is stain normalization applied on extracted patches ? Is contrast enhancement applied on extracted patches ? Feb 6, 2025
@guillaumejaume
Copy link
Collaborator

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

@abhi1998goyal
Copy link
Author

abhi1998goyal commented Feb 7, 2025

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 !

@guillaumejaume
Copy link
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

@abhi1998goyal
Copy link
Author

abhi1998goyal commented Feb 8, 2025

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

@pauldoucet
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants