Commit 4802c4a0 authored by Jonathan Barés's avatar Jonathan Barés

bug fixed (integer-float problem)

parent 447f33f2
......@@ -43,7 +43,7 @@ def toggle_selector(event):
def select(self):
global current_ax, x1, y1, x2, y2, list_area
current_ax.plot([x1,x2, x2, x1, x1],[y1, y1, y2, y2, y1],'-r')
list_area.append(np.array([x1, y1, x2, y2]))
list_area.append(np.array([x1, y1, x2, y2]).astype('int'))
## Define correlation fonction for tracking:
def CorrelVal(X,roi_0,roi_1):
......@@ -51,13 +51,19 @@ def CorrelVal(X,roi_0,roi_1):
## Extraction of the variables:
dI=X[0]
dJ=X[1]
## Shift the picture:
roi_1_rs=nd.interpolation.shift(roi_1,(dI,dJ),order=1,mode='constant',cval=0.,prefilter=False)
## Reduction to the center of the ROI:
roi_0_r=roi_0[int(len(roi_0)*rr):int(len(roi_0)*(1.-rr)),int(len(roi_0)*rr):int(len(roi_0)*(1.-rr))]
roi_1_rs_r=roi_1_rs[int(len(roi_0)*rr):int(len(roi_0)*(1.-rr)),int(len(roi_0)*rr):int(len(roi_0)*(1.-rr))]
## Correlation:
s=np.sum(abs(roi_1_rs_r-roi_0_r))/(len(roi_0)**2)
if max(X)>3:
s=1e10
else:
## Shift the picture:
roi_1_rs=nd.interpolation.shift(roi_1,(dI,dJ),order=1,mode='constant',cval=0.,prefilter=False)
## Reduction to the center of the ROI:
roi_0_r=roi_0[int(len(roi_0)*rr):int(len(roi_0)*(1.-rr)),int(len(roi_0)*rr):int(len(roi_0)*(1.-rr))]
roi_1_rs_r=roi_1_rs[int(len(roi_0)*rr):int(len(roi_0)*(1.-rr)),int(len(roi_0)*rr):int(len(roi_0)*(1.-rr))]
## Correlation:
s=np.sum(abs(roi_1_rs_r-roi_0_r))/(len(roi_0)**2)
# ~ s=1/(np.sum(roi_1_rs_r*roi_0_r)/(len(roi_0)**2))
return s
## Main fonctions to detect pattern motion:
......@@ -72,25 +78,27 @@ def MainTrack(it_a):
y2=list_area[it_a][3]
### Load current position :
I_cur=IJ_stp[it_a,0,it_img]
J_cur=IJ_stp[it_a,1,it_img]
I_n=I_cur.copy(); J_n=J_cur.copy()
I_cur=int(IJ_stp[it_a,0,it_img])
J_cur=int(IJ_stp[it_a,1,it_img])
I_n=int(IJ_stp[it_a,0,it_img])
J_n=int(IJ_stp[it_a,1,it_img])
### FFT approximation:
#### Extraction of the ROI:
I_roi_m_0=int(y1); I_roi_M_0=int(y2); J_roi_m_0=int(x1); J_roi_M_0=int(x2)
I_roi_m_0=y1; I_roi_M_0=y2; J_roi_m_0=x1; J_roi_M_0=x2
roi_0=image_0[I_roi_m_0:I_roi_M_0,J_roi_m_0:J_roi_M_0]
# ~ plt.imshow(roi_0)
# ~ plt.savefig('10.png')
# ~ plt.close()
# ~ from PIL import Image
# ~ roi_plot=roi_0.astype('uint8')
# ~ img=Image.fromarray(roi_plot)
# ~ img.save('0.png')
I_roi_m_1=int(y1+I_n); I_roi_M_1=I_roi_m_1+roi_0.shape[0]; J_roi_m_1=int(x1+J_n); J_roi_M_1=J_roi_m_1+roi_0.shape[1]
I_roi_m_1=y1+I_n; I_roi_M_1=I_roi_m_1+roi_0.shape[0]; J_roi_m_1=x1+J_n; J_roi_M_1=J_roi_m_1+roi_0.shape[1]
roi_1=image_1[I_roi_m_1:I_roi_M_1,J_roi_m_1:J_roi_M_1]
# ~ plt.imshow(roi_1)
# ~ plt.savefig('11.png')
# ~ plt.close()
# ~ roi_plot=roi_1.astype('uint8')
# ~ img=Image.fromarray(roi_plot)
# ~ img.save('1.png')
#### Measurement shift between picture from FFT:
##### Compute Fourier transform:
......@@ -108,15 +116,15 @@ def MainTrack(it_a):
J_n=J_n-J_sft
### Extract the new ROI:
I_roi_m_n=int(y1+I_n); I_roi_M_n=int(y2+I_n); J_roi_m_n=int(x1+J_n); J_roi_M_n=int(x2+J_n)
I_roi_m_n=y1+I_n; I_roi_M_n=I_roi_m_n+roi_0.shape[0]; J_roi_m_n=x1+J_n; J_roi_M_n=J_roi_m_n+roi_0.shape[1]
roi_1=image_1[I_roi_m_n:I_roi_M_n,J_roi_m_n:J_roi_M_n]
# ~ plt.imshow(roi_1)
# ~ plt.savefig('12.png')
# ~ plt.close()
# ~ roi_plot=roi_1.astype('uint8')
# ~ img=Image.fromarray(roi_plot)
# ~ img.save('2.png')
### Optimisation of the correlation:
ext_res=minimize(CorrelVal,np.array([0.,0.]),args=(roi_0,roi_1),method='Nelder-Mead',tol=1e-4,options={'maxiter':3000,'maxfev':5000,'disp':False})
ext_res=minimize(CorrelVal,np.array([0.,0.]),args=(roi_0,roi_1),method='Nelder-Mead',tol=1e-10,options={'maxiter':3000,'maxfev':5000,'disp':False})
### Final and accurate position measurement:
dI_0=ext_res.x[0]
dJ_0=ext_res.x[1]
......@@ -190,7 +198,6 @@ IJ_stp[:,0:2,0]=IJ_0
image_0=cv2.imread(nam_pict[0],0).astype('float')
## Loop over the pictures:
for it_img in range(nb_pict-1):
print('picture '+str(it_img))
### Load the current picture:
......@@ -234,3 +241,6 @@ for it_img in range(nb_pict-1):
os.system('ffmpeg -y -r 10 -f image2 -i tmp/%05d.png -qscale 10 figure/movie.avi')
os.system('rm -rf tmp')
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