Skip to content

Commit ee9ec14

Browse files
authored
Add files via upload
Slow_Students è il main (completo) che usa le funzioni Inizialization_images, minimum_analysis e frequency_analysis Image_Analysis e Image_Inizialization insieme sono equivalenti a Slow_Students ma permettono di fare separatamente la applicazione della maschera scrivendo il risultato su file (30 min). PROBLEMI: 1- background, dove sbaglio? 2- la riga 61 del file minimum_analysis mi dà errore (credo sia dovuto al fatto che j è definito come una lista, non ho capito bene cosa stia facendo...) Marco puoi aiutarmi?
1 parent 500d485 commit ee9ec14

File tree

6 files changed

+595
-0
lines changed

6 files changed

+595
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
2+
#-------------------------------FUNCTION DEFINITION-----------------------------
3+
def Make_a_rectangoular_crop(img_collection, topx, topy, bottomx, bottomy):
4+
5+
#import matplotlib.pyplot as plt
6+
from PIL import Image
7+
8+
img_collection_cropped = []
9+
#img = ((100,100))
10+
11+
for i in range(len(img_collection)):
12+
img = img_collection[i]
13+
14+
cropped = img[topx:bottomx, topy:bottomy]
15+
img_collection_cropped.append(cropped)
16+
#plt.matshow(cropped)
17+
#plt.show()
18+
19+
return(img_collection_cropped)
20+
21+
22+
#--------------------------Contours plotting------------------------------------
23+
24+
25+
def drawShape(img, coordinates, color):
26+
#This function drows all contours founded on the mask
27+
# In order to draw our line in red
28+
img = skimage.color.gray2rgb(img)
29+
30+
# Make sure the coordinates are expressed as integers
31+
coordinates = coordinates.astype(int)
32+
33+
img[coordinates[:, 0], coordinates[:, 1]] = color
34+
35+
return img
36+
37+
38+
def Contours_printing(img_float, contours):
39+
# Display the image and plot all contours found
40+
fig, ax = plt.subplots()
41+
ax.imshow(img_float, interpolation='nearest', cmap=plt.cm.gray)
42+
43+
for n, contour in enumerate(contours):
44+
ax.plot(contour[:, 1], contour[:, 0], linewidth=2)
45+
46+
ax.axis('image')
47+
ax.set_xticks([])
48+
ax.set_yticks([])
49+
plt.show()
50+
51+
#Create a Black mask of the same dimentions of the image
52+
mask = np.zeros_like(img_float) # Create mask where white is what we want, black otherwise
53+
54+
#Drow contours on 'mask'
55+
for contour in contours:
56+
mask = drawShape(mask, contour, [255, 0, 0])
57+
58+
#------------------------Find best contour--------------------------------------
59+
60+
def Find_Contours(img):
61+
62+
import numpy as np
63+
from PIL import Image
64+
import skimage
65+
from skimage import data, io, filters, measure, img_as_float, img_as_uint
66+
from matplotlib import pyplot as plt
67+
68+
Contour_Limit = 0.05
69+
70+
contour = []
71+
72+
while (len(contour) != 1):
73+
contour = measure.find_contours(img, Contour_Limit)
74+
Contour_Limit += 0.001
75+
#print Contour_Limit
76+
77+
return contour
78+
79+
def Find_a_mask(img_collection):
80+
#This function finds and drows contures of an image given.
81+
#It returns the cropped image as a numpy array of float
82+
83+
# Find contours the best Contour_Limit for the first image of the serie
84+
contours = Find_Contours(img_collection[0])
85+
86+
return(contours)
87+
88+
#-------------------------------------------------------------------------------
89+
90+
91+
def Inside_outside_check(point, contours):
92+
#Mi raccomando Contours lo si deve passare come Contours[0]
93+
94+
from shapely.geometry import Point
95+
from shapely.geometry.polygon import Polygon
96+
97+
point = Point(point)
98+
polygon = Polygon(contours)
99+
return(polygon.contains(point))
100+
101+
#-------------------------------------------------------------------------------
102+
103+
def Apply_a_mask(img_collection, Contours, DIM_X, DIM_Y):
104+
105+
import numpy as np
106+
#import matplotlib.pyplot as plt
107+
108+
img_collection=np.asarray(img_collection)
109+
110+
#print len(img_collection)
111+
112+
for t in range(len(img_collection)):
113+
114+
for x in range(DIM_X):
115+
for y in range(DIM_Y):
116+
point = (x,y)
117+
if not(Inside_outside_check(point, Contours[0])):
118+
#se il punto e' nel contorno
119+
img_collection[t,x,y] = 'nan'
120+
#plt.matshow(img_collection[t])
121+
#plt.show()
122+
123+
return img_collection

Slow_Students-master/Slow_Waves.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#===============================================================================
2+
# MAIN
3+
#===============================================================================
4+
5+
import os
6+
import numpy as np
7+
from pylab import *
8+
from scipy.signal import butter, lfilter, filtfilt, freqz
9+
from scipy.optimize import curve_fit
10+
import matplotlib.pyplot as plt
11+
12+
import skimage
13+
from skimage import data, io, filters, measure
14+
from skimage import img_as_float, img_as_uint
15+
16+
from Inizialization_images import drawShape, Make_a_rectangoular_crop, Find_a_mask, Apply_a_mask, Inside_outside_check
17+
from frequency_analysis import bandpass_filter, multiplot
18+
from minimum_analysis import find_minimum, min_interpol, parab_fit, min_analysis
19+
20+
#------------------------------PARAMETER DEFINITION-----------------------------
21+
22+
# Filter requirements.
23+
order = 6
24+
fs = 25.0 # sample rate, Hz
25+
lowcut = 0.3 # desired cutoff frequency of the filter, Hz
26+
highcut = 2.5
27+
28+
SAMPLING_TIME = 40. # ms (unit)
29+
DIM_X = 100 #image dimension
30+
DIM_Y = 100
31+
32+
MACRO_PIXEL_DIM = 2 #pixel dimention of our new 'macro-pixel'
33+
34+
ZOOM = 4 #minimum fit parameter
35+
t_min = 0 #time range for minimum research
36+
t_max = 2
37+
38+
#-----------------------------IMAGE LOADING-------------------------------------
39+
40+
path = "/Users/Mac/Desktop/Uni/LabII/data/t1/provevideo"
41+
42+
43+
img_path_list = []
44+
for i in range(1, 4):
45+
img_path = '1_' + str(i) + '.tif'
46+
filename = os.path.join(skimage.data_dir, path + img_path)
47+
48+
img_path_list.append(filename)
49+
50+
# Load all the collection of the images
51+
img_collection_float = img_as_float(io.imread_collection(img_path_list))
52+
53+
'''
54+
DA RICONTROLLARE IL BACKGROUND
55+
img_background = np.zeros((100, 100), np.float64)
56+
57+
# Evaluate the background of the images as the mean over the whole set
58+
for i in img_collection_float:
59+
# Convert all images to float, range -1 to 1, to avoid dangerous overflows
60+
img_background += i
61+
62+
img_background /= len(img_collection_float)
63+
64+
# Substract from each image the background
65+
for i in img_collection_float:
66+
i -= img_background
67+
'''
68+
69+
img_collection_UP = []
70+
img_collection_DOWN = []
71+
72+
img_collection_UP = Make_a_rectangoular_crop(img_collection_float, 0, 0, 19, 100)
73+
img_collection_DOWN = Make_a_rectangoular_crop(img_collection_float, 20, 0, 100, 100)
74+
75+
76+
#-----------------------Analysis of Down images
77+
DIM_X=80
78+
DIM_Y=100
79+
80+
Contours_DOWN = Find_a_mask(img_collection_DOWN)
81+
82+
'''
83+
#DA VELOCIZZARE QUESTO PUNTO
84+
'''
85+
img_collection_DOWN = Apply_a_mask(img_collection_DOWN, Contours_DOWN, DIM_X, DIM_Y)
86+
87+
88+
# Now we need to reduce the noise from the images by performing a spatial smoothing
89+
img_collection_reduced = []
90+
img_collection_reduced = measure.block_reduce(img_collection_DOWN, (1, MACRO_PIXEL_DIM, MACRO_PIXEL_DIM), np.mean)
91+
print img_collection_reduced.shape
92+
93+
94+
95+
#--------------------------------Spectrum analysis------------------------------
96+
97+
# Next we take the fourier transformation of the input along the temporal axis
98+
img_spectrum = np.fft.rfftn(img_collection_reduced, axes = [0])
99+
img_spectrum_freq = np.fft.rfftfreq(img_collection_reduced.shape[0], d = 1. / SAMPLING_TIME)
100+
101+
# We take the mean of the spectrum
102+
spectrum_mean = measure.block_reduce(img_spectrum, (1, np.size(img_collection_reduced,1), np.size(img_collection_reduced,2)), np.mean)
103+
104+
xmax = np.amax(img_collection_reduced[:, 5, 7])
105+
xmin = np.amin(img_collection_reduced[:, 5, 7])
106+
107+
plt.plot(img_collection_reduced[300:800, 5, 7])
108+
#plt.show()
109+
110+
#Clean the signal through a bandpass-pass filter
111+
112+
clean_signals = bandpass_filter(img_collection_reduced)
113+
114+
#To have an idea we choose randomly the pixel 5,7
115+
plt.plot(clean_signals[20, 20,:])
116+
#plt.show()
117+
118+
#plt.hist(clean_signals[5, 7, :], bins='auto', range = (xmin , xmax))
119+
#plt.yscale('log')
120+
#plt.show()
121+
122+
123+
#Multiplot of different pixels
124+
#plt.plot = multiplot(10, 40, 10, 10, 40, 10, 650, 750)
125+
#plt.show()
126+
127+
128+
#----------------------------------MINIMUM ANALYSIS-------------------------------
129+
130+
#Identify min and max values
131+
minimum_signal = []
132+
minimum_signal = min_analysis(clean_signals, ZOOM, t_min, t_max)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#===============================================================================
2+
# MAIN
3+
#===============================================================================
4+
5+
import os
6+
import numpy as np
7+
from pylab import *
8+
from scipy.signal import butter, lfilter, filtfilt, freqz
9+
from scipy.optimize import curve_fit
10+
import matplotlib.pyplot as plt
11+
12+
import skimage
13+
from skimage import data, io, filters, measure
14+
from skimage import img_as_float, img_as_uint
15+
16+
from Inizialization_images import drawShape, Make_a_rectangoular_crop, Find_a_mask, Apply_a_mask, Inside_outside_check
17+
from frequency_analysis import bandpass_filter, multiplot
18+
from minimum_analysis import find_minimum, min_interpol, parab_fit, min_analysis
19+
20+
#------------------------------PARAMETER DEFINITION-----------------------------
21+
22+
# Filter requirements.
23+
order = 6
24+
fs = 25.0 # sample rate, Hz
25+
lowcut = 0.3 # desired cutoff frequency of the filter, Hz
26+
highcut = 2.5
27+
28+
SAMPLING_TIME = 40. # ms (unit)
29+
DIM_X_REDUCED = 40 #image dimension
30+
DIM_Y_REDUCED = 50
31+
32+
MACRO_PIXEL_DIM = 2 #pixel dimention of our new 'macro-pixel'
33+
34+
ZOOM = 4 #minimum fit parameter
35+
t_min = 0 #time range for minimum research
36+
t_max = 2
37+
38+
#-----------------------------IMAGE LOADING-------------------------------------
39+
40+
Images2D = np.loadtxt('inizialized_images.txt')
41+
print Images2D.shape
42+
print Images2D
43+
44+
img_collection_reduced = np.reshape(Images2D, ((1000, DIM_X_REDUCED, DIM_Y_REDUCED)),order='C')
45+
print img_collection_reduced.shape
46+
#plt.matshow(img_collection_reduced[0])
47+
#plt.show()
48+
49+
#--------------------------------Spectrum analysis------------------------------
50+
51+
# Next we take the fourier transformation of the input along the temporal axis
52+
img_spectrum = np.fft.rfftn(img_collection_reduced, axes = [0])
53+
img_spectrum_freq = np.fft.rfftfreq(img_collection_reduced.shape[0], d = 1. / SAMPLING_TIME)
54+
55+
# We take the mean of the spectrum
56+
spectrum_mean = measure.block_reduce(img_spectrum, (1, np.size(img_collection_reduced,1), np.size(img_collection_reduced,2)), np.mean)
57+
58+
xmax = np.amax(img_collection_reduced[:, 5, 7])
59+
xmin = np.amin(img_collection_reduced[:, 5, 7])
60+
61+
plt.plot(img_collection_reduced[300:800, 5, 7])
62+
#plt.show()
63+
64+
#Clean the signal through a bandpass-pass filter
65+
66+
clean_signals = bandpass_filter(img_collection_reduced)
67+
68+
#To have an idea we choose randomly the pixel 5,7
69+
plt.plot(clean_signals[20, 20,:])
70+
#plt.show()
71+
72+
#plt.hist(clean_signals[5, 7, :], bins='auto', range = (xmin , xmax))
73+
#plt.yscale('log')
74+
#plt.show()
75+
76+
77+
#Multiplot of different pixels
78+
#plt.plot = multiplot(10, 40, 10, 10, 40, 10, 650, 750)
79+
#plt.show()
80+
81+
82+
#----------------------------------MINIMUM ANALYSIS-------------------------------
83+
84+
#Identify min and max values
85+
minimum_signal = []
86+
minimum_signal = min_analysis(clean_signals, ZOOM, t_min, t_max)

0 commit comments

Comments
 (0)