forked from xq141839/NuSegDG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDensityMapGen.py
46 lines (37 loc) · 1.79 KB
/
DensityMapGen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
from skimage import io, transform, color, img_as_ubyte
import numpy as np
from torch.utils.data import Dataset
import cv2
import torch
import torchvision.transforms as pytorch_transforms
import torch.nn.functional as F
from albumentations.pytorch.transforms import ToTensor
import albumentations as A
from scipy.ndimage import gaussian_filter
from tqdm import tqdm
img_list = os.listdir("E:\Research\medsam_v2\code\datasets\TNBC\mask_1024")
for img_name in tqdm(img_list):
image_path = f'E:\Research\medsam_v2\code\datasets\TNBC\mask_1024\{img_name}'
img = cv2.imread(image_path, 0) # Load in grayscale mode
if img is None:
print(f"Failed to load image from {image_path}. Check if the file exists and is accessible.")
continue # Skip this iteration and move to the next image
img[img < 255] = 0
target = np.zeros(img.shape, dtype=np.uint8)
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(img)
for i in range(1, num_labels):
one_nuclei = np.zeros(img.shape, dtype= np.uint8)
row, col = int(centroids[i][0]), int(centroids[i][1])
one_nuclei[col][row]= 255
one_nuclei= 10.0 * (one_nuclei[:,:]>0)
one_nuclei = gaussian_filter(one_nuclei, sigma=(3,3), mode='constant', radius=10)
normalised =(one_nuclei - one_nuclei.min()) / (one_nuclei.max() - one_nuclei.min())
normalised *= 128
normalised[normalised !=0] +=128
normalised[normalised >255] = 255
normalised[normalised <129] = 0
normalised[normalised >0] = 255
target += normalised.astype(np.uint8)
output_path = f"E:\Research\medsam_v2\code\datasets\TNBC\DensityMap\{img_name}"
cv2.imwrite(output_path, target)