Commit 18d6f1b9 authored by Jonathan Barés's avatar Jonathan Barés

Obstacle detection added in 01 and TODO updated

parent dc83e35e
......@@ -9,14 +9,15 @@ import time
import datetime
from PIL import Image
from scipy import misc
from scipy import ndimage as nd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from multiprocessing import Pool
from matplotlib.widgets import RectangleSelector
from matplotlib.widgets import RectangleSelector, Slider, Button
#Input parameter:
##Number of processor for post-processing parallelization:
NbProc=6
NbProc=4
#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#
#Rename file and create time vectors:
......@@ -36,9 +37,10 @@ null=os.system('mkdir pictureWh')
##Loop over picture to get their times:
for it in range(len(nam_pict)):
tmp=Image.open(nam_pict[it])._getexif()[36867]
dt_tmp=datetime.datetime.strptime(tmp, '%Y:%m:%d %H:%M:%S')
timePic[it]=time.mktime(dt_tmp.timetuple())
# tmp=Image.open(nam_pict[it])._getexif()[36867]
# dt_tmp=datetime.datetime.strptime(tmp, '%Y:%m:%d %H:%M:%S')
# timePic[it]=time.mktime(dt_tmp.timetuple())
timePic[it]=np.float(nam_pict[it][8:-4])
##Sort time vector convert in min and save it:
timePic-=np.min(timePic)
......@@ -97,7 +99,7 @@ def MeasurePictureShift(iPct):
yShift,xShift=np.unravel_index(np.argmax(pictCCorShift),(row,col))
yShift=yShift-int(row/2.)
xShift=xShift-int(col/2.)
###Quantify error:
a=np.argmax(pictCCorShift)
###return the shifts:
......@@ -110,7 +112,6 @@ xShiftVec0,yShiftVec0,test0=zip(*p.map(MeasurePictureShift,range(NbStp)))
xShiftVec0=np.array(xShiftVec0); yShiftVec0=np.array(yShiftVec0); test0=np.array(test0)
p.close()
##Detect picture problems and fix it:
###Peak a threshold on image correlation if needed:
plt.plot(np.abs(test0-np.mean(test0)),'o',markersize=5)
......@@ -226,6 +227,81 @@ null=p.map(CropPicture,range(NbStp))
p.close()
#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#
#Get obstacles:
##Load the initial picture (converted in black en white):
pict1=misc.imread('pictureWh/'+'%04d'%0+'.jpg',mode='L')
##Threshold it:
###Function to thresold the picture:
def PictTh(pict, val_th, min_siz):
####Threshold the picture:
pict_th=pict<val_th
####Get disconnected islands:
pict_lbl, nb_lbl=nd.measurements.label(pict_th)
vec_lbl=np.arange(nb_lbl+1)
####Get their size:
siz_lbl=nd.labeled_comprehension(pict_th, pict_lbl, vec_lbl, len, float, 0)
####Remove back ground:
siz_lbl=siz_lbl[1:-1]
vec_lbl=vec_lbl[1:-1]
####Get label of large islands:
vec_lrg=vec_lbl[np.where(siz_lbl>min_siz)]
return (np.isin(pict_lbl,vec_lrg), len(vec_lrg))
###Initialize figure:
fig=plt.figure()
ax=fig.add_subplot(111)
fig.subplots_adjust(left=0.1, bottom=0.12)
plt.axis('off')
###Make default valus
val_th_0=np.median(pict1)
min_siz_0=0.01*pict1.size
###Make initial plot:
ax.imshow(PictTh(pict1,val_th_0,min_siz_0)[0],cmap='gray')
plt.title('number of obstacles: '+str(PictTh(pict1,val_th_0,min_siz_0)[1]))
###Define selection sliders:
th_slider_ax=fig.add_axes([0.2, 0.08, 0.65, 0.03])
th_slider=Slider(th_slider_ax, 'threshold', 0., 255., valinit=val_th_0)
min_slider_ax=fig.add_axes([0.2, 0.05, 0.65, 0.03])
min_slider=Slider(min_slider_ax, 'min size', 0., 0.1*pict1.size, valinit=val_th_0)
###Function happening when values are changed:
def slider_on_changed(val):
pict2plot,nb_obst=PictTh(pict1,th_slider.val,min_slider.val)
ax.imshow(pict2plot,cmap='gray')
ax.set_title('number of obstacles: '+str(nb_obst))
plt.draw()
th_slider.on_changed(slider_on_changed)
min_slider.on_changed(slider_on_changed)
###Select threshold values:
plt.show()
plt.close()
###Store results:
mask_obst=PictTh(pict1,th_slider.val,min_slider.val)[0]
obst_lbl,nb_obst=nd.measurements.label(mask_obst)
###Display obstacles:
####Make storage folder:
os.system('mkdir obstacle')
####Create figure:
fig=plt.figure()
plt.imshow(1-mask_obst,cmap='gray')
for it_obst in range(1,nb_obst+1):
I,J=np.where(obst_lbl==it_obst)
plt.text(np.mean(J),np.min(I)-10,str(it_obst),horizontalalignment='center',color='black')
plt.axis('equal')
plt.axis('off')
plt.savefig('obstacle/numbering.png',bbox_inches='tight',dpi=200)
plt.close()
###Save obstacle map:
misc.imsave('obstacle/obstacleMask.png',obst_lbl)
#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#~/"\~#
#Create movies:
......
Faire tourner sur un set d'images test
Utiliser un widget pour le choix de la valeur de seuillage
Utiliser un widget pour le choix de la valeur de seuillage pour la detection de racine
Prendre en compte la détection des obstacles
Faire un code initial donnant un YAML avec les paramètres d'entrée
Refonder proprement le code
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment