-
Notifications
You must be signed in to change notification settings - Fork 0
/
mias_preprocess.py
120 lines (99 loc) · 3.83 KB
/
mias_preprocess.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import scipy.misc
import matplotlib.image as matlabimg
import re
import pandas as pd
import os
from sklearn.feature_extraction import image
rng = np.random.RandomState(0)
patch_size = (96,96)
global_counter = 0
# read csvFile
data_train = pd.read_csv('./mias/mias.csv')
abnormal = data_train[data_train.abnormality_class != 'NORM']
normal = data_train[data_train.abnormality_class == 'NORM']
# data_train
# HELPER FUNCTIONS
# data_train[data_train.abnormality_class == 'NORM']["abnormality_class"]
# Function for obtaining center crops from an image
def crop_center(x, crop_w, crop_h):
if crop_w is None:
crop_w = crop_h
h, w = x.shape[:2]
j = int(round((h - crop_h) / 2.))
i = int(round((w - crop_w) / 2.))
return x[j:j + crop_h, i:i + crop_w]
# Function for reading PGM files
def read_pgm(filename, byteorder='>'):
"""Return image data from a raw PGM file as numpy array.
Format specification: http://netpbm.sourceforge.net/doc/pgm.html
"""
with open(filename, 'rb') as f:
buffer = f.read()
try:
header, width, height, maxval = re.search(
b"(^P5\s(?:\s*#.*[\r\n])*"
b"(\d+)\s(?:\s*#.*[\r\n])*"
b"(\d+)\s(?:\s*#.*[\r\n])*"
b"(\d+)\s(?:\s*#.*[\r\n]\s)*)", buffer).groups()
except AttributeError:
raise ValueError("Not a raw PGM file: '%s'" % filename)
return np.frombuffer(buffer,
dtype='u1' if int(maxval) < 256 else byteorder + 'u2',
count=int(width) * int(height),
offset=len(header)
).reshape((int(height), int(width)))
def convert_pgm_to_png():
images = []
for i, row in normal.iterrows():
images.append(read_pgm('./mias/pgm/' + row['reference_number'] + '.pgm'))
j = 0;
for i, row in normal.iterrows():
images[j].setflags(write=1)
if (int(row['reference_number'][-3:]) % 2 == 0):
images[j][:324, 700:1024] = np.zeros((324, 324))
else:
images[j][:324, :324] = np.zeros((324, 324))
matlabimg.imsave('./mias/png/' + row['reference_number'] + '.png', images[j], vmin=0, vmax=255, cmap='gray')
j += 1
def convert_pgm_to_png_anomalous():
images = []
for i, row in abnormal.iterrows():
images.append(read_pgm('./mias/pgm/' + row['reference_number'] + '.pgm'))
j = 0;
for i, row in abnormal.iterrows():
images[j].setflags(write=1)
if (int(row['reference_number'][-3:]) % 2 == 0):
images[j][:324, 700:1024] = np.zeros((324, 324))
else:
images[j][:324, :324] = np.zeros((324, 324))
matlabimg.imsave('./mias/png_anomalous/' + row['reference_number'] + '.png', images[j], vmin=0, vmax=255, cmap='gray')
j += 1
def generate_patches(input_image):
# print("in generate patchhes")
global global_counter
input_image = crop_center(input_image, 384, 384)
patches = image.extract_patches_2d(input_image, patch_size, max_patches=50,
random_state=rng)
for counter, i in enumerate(patches):
if np.any(i):
matlabimg.imsave('./data/mias_anomalous/' + str(global_counter) + '.png', i, cmap='gray')
global_counter += 1
#
# convert_pgm_to_png_anomalous()
images = []
# arr = os.listdir(os.getcwd() + "/mias/png_anomalous/")
# arr=arr[1:10]
arr=[]
arr.append(os.getcwd() + "/mias/png_anomalous/mdb063.png")
print(arr)
print("png size ",len(arr))
for img in arr:
# images.append(matlabimg.imread(os.getcwd() + "/mias/png_anomalous/" + img))
images.append(matlabimg.imread(img))
# generate_patches(images[0])
for counter, image_full in enumerate(images):
generate_patches(image_full)