Let's Run Jinyeah

[Python] Brain MRI Thresholding 본문

Programming/Medical Image Processing

[Python] Brain MRI Thresholding

jinyeah 2022. 5. 26. 00:09

What is Thresholding?

Thresholding is a type of image segmentation. It converts an image from colour or grayscale into a binary image that is simply black and white.

Overall Process of Thresholding (fixed-level thresholding)

1) load the original image

2) convert it to grayscale

- grayscale images containe pixel values in the range from 0 to 1

3) de-noise image ex.blurring

- not necessary, but important to remove noise to find good threshold value in practice

4) apply the threshold 't'

5) de-noise image

- not necessary, but needed to obtaine smooth and clean binary mask

 

How to find threshold value 't'?

※ create a histogram of the grayscale image
- interpret the peaks in the histogram to determine an appropriate threshold

 

Example

★​ My application

extract brain region in brain MRI image to improve computational efficiency

Input Image [3, 256, 256]
mask

1) draw histogram of the grayscale image

# Convert to grayscale image [0,1]
image = image.sum(axis=0)
tmp0 = (image- image.min()) / (image.max() - image.min())

# Draw histogram
plt.hist(tmp0.flatten(), 100)
plt.savefig('gray-scale histogram.png')
plt.close()

grayscale histogram

2) find threshold (interpret histogram)

  • find out that the pixels between [0,0.1] consist of 78% of whole image using cumulative distribution function
  • choose the pixel value which is 75 percentile as a threshold value
# generate culminative distribution function(cdf)
pixel_num, bins = np.histogram(tmp0, 100, [0, tmp0.max()])
cdf = np.cumsum(pixel_num) / sum(pixel_num)

# find threshold value
thr = np.where(cdf > 0.75)[0][0]
# or use percentile
thr_percent = 0.75
thr = np.percentile(tmp00, thr_percent * 100)

3) generate binary removing noise

# generate mask
mask = np.zeros_like(tmp0)
for j in range(tmp0.shape[0]):
	# mask[j] = tmp[j] > thr
    mask[j] = closing(dilation(erosion((tmp0[j] > thr), se), se), se)

'Programming > Medical Image Processing' 카테고리의 다른 글

[Python] MRI Resampling  (0) 2022.07.08
Magnetic Resonance Imaging (MRI)  (0) 2022.05.13
Comments