-
Notifications
You must be signed in to change notification settings - Fork 1
/
median_noise.py
79 lines (75 loc) · 2.79 KB
/
median_noise.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import numpy as np
import pydicom
from algorithms.sorting import quick_sort
import pectoral_muscle
def noise_removal(DDSM):
for i in (DDSM):
ds = pydicom.dcmread(i)
# initialize the y part of the pixel in the array
y = 0
# initialize x
x = 0
# size of image
pixels = ds.pixel_array.shape[0] * ds.pixel_array.shape[1]
for pixel in range(pixels):
# define the pixel we're looking at
"""
n = ds.pixel_array[y , x]
n_up = ds.pixel_array[y - 1, x]
n_upl = ds.pixel_array[y - 1, x - 1]
n_l = ds.pixel_array[y, x - 1]
n_downl = ds.pixel_array[y + 1, x - 1]
n_down = ds.pixel_array[y + 1, x]
n_downr = ds.pixel_array[y + 1, x + 1]
n_r = ds.pixel_array[y, x + 1]
n_upr = ds.pixel_array[y - 1, x + 1]
window = [n, n_up, n_upr, n_upl, n_l, n_downl, n_down, n_downr, n_r]
"""
window = pectoral_muscle.neighbors(y, x, ds.pixel_array)
# sorting
window = quick_sort.sort(window)
# set value to pixel\
ds.pixel_array[y, x] = ds.pixel_array[window[len(window) / 2][0], window[len(window) / 2][1]]
if x == ds.pixel_array.shape[1] - 1:
y = y + 1
x = 0
continue
x += 1
# print("done with iteration " + str(pixel) +" for median noise")
ds.PixelData = ds.pixel_array.tostring()
ds.save_as(i)
def noise_removal_single(i):
ds = pydicom.dcmread(i)
#initialize the y part of the pixel in the array
y = 0
#initialize x
x = 0
#size of image
pixels = ds.pixel_array.shape[0]*ds.pixel_array.shape[1]
for pixel in range(pixels):
# define the pixel we're looking at
"""
n = ds.pixel_array[y , x]
n_up = ds.pixel_array[y - 1, x]
n_upl = ds.pixel_array[y - 1, x - 1]
n_l = ds.pixel_array[y, x - 1]
n_downl = ds.pixel_array[y + 1, x - 1]
n_down = ds.pixel_array[y + 1, x]
n_downr = ds.pixel_array[y + 1, x + 1]
n_r = ds.pixel_array[y, x + 1]
n_upr = ds.pixel_array[y - 1, x + 1]
window = [n, n_up, n_upr, n_upl, n_l, n_downl, n_down, n_downr, n_r]
"""
window = pectoral_muscle.neighbors(y, x, ds.pixel_array)
#sorting
window = quick_sort.sort(window)
#set value to pixel\
ds.pixel_array[y, x] = ds.pixel_array[window[len(window)/2][0], window[len(window)/2][1]]
if x == ds.pixel_array.shape[1]-1:
y = y + 1
x = 0
continue
x += 1
#print("done with iteration " + str(pixel) +" for median noise")
ds.PixelData = ds.pixel_array.tostring()
ds.save_as(i)