-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSM.sce
335 lines (306 loc) · 8.74 KB
/
PSM.sce
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
///////////////////////////////////////////////////
// PSM - Point Shift Measurement
// Scilab programm
// For BOS technique
// requires installation IPCV atom
// first version - 02-2019
// Jury Barinov
///////////////////////////////////////////////////
// Image_1 preprocessing
// remove background, increase contrast
function FrO=Im_pr1(Fr)
disp("Preprocessing File #1");
//to marix
FrO = im2double(Fr)
// negativity
FrO=imcomplement(FrO);
[y,x,z]=size(FrO)
if z==3 then
disp("File #1 is color")
// grayscale
FrO = rgb2gray(FrO)
end
// Setting is bright. and contrast
FrO=imadjust(FrO,[0.2 1],[0 1]); // The options depend on the image quality
// Noise filtering
filter = fspecial('average',7) //
FrO = imfilter(FrO, filter);
// Processing by pieces
// Similar to adaptive level
FrO=AdThreshold(FrO,3,5)
endfunction
///////////////////////////////////////////////////
//Image_2 preprocessing
// remove background, increase contrast
function FrO=Im_pr2(Fr)
disp("Preprocessing File #2");
//to marix
FrO = im2double(Fr)
// negativity
FrO=imcomplement(FrO);
[y,x,z]=size(FrO)
if z==3 then
disp("File #2 is color")
// grayscale
FrO = rgb2gray(FrO)
end
// Setting is bright. and contrast
FrO=imadjust(FrO,[0.05 1],[0 1]); // The options depend on the image quality
// Noise filtering
filter = fspecial('average',7) // хорошо
FrO = imfilter(FrO, filter);
// Processing by pieces
// Similar to adaptive level
FrO=AdThreshold(FrO,7,4)
endfunction
///////////////
// Binarization by parts
// I-input im.
// k - number of pieces of partition
function [B]=AdThreshold(I,ky,kx)
//
[yp,xp]=size(I)
dy = fix(yp/ky)
dx = fix(xp/kx)
dyy = yp - ky*dy
dxx = xp - kx*dx
B=zeros(I)
for j=1:ky
for i=1:kx
y=(j-1)*dy+1:j*dy
x=(i-1)*dx+1:i*dx
th = imgraythresh( I( y,x ) );
B(y,x) = im2bw(I(y,x),th);
end
end
for i=1:kx
y=ky*dy+1:ky*dy+dyy
x=(i-1)*dx+1:i*dx
th = imgraythresh( I( y,x ) );
B(y,x) = im2bw(I(y,x),th);
end
for j=1:ky-1
y=(j-1)*dyy+1:j*dyy
x=kx*dx+1:kx*dx+dxx
th = imgraythresh( I( y,x ) );
B(y,x) = im2bw(I(y,x),th);
end
endfunction
///////////////////////////////////////////////////
// find points
// Fr1 - background screen
// Fr2 - distorted image
function [ctr1, ctr2, BB1,BB2,A1,A2]=Detect_point(Fr1, Fr2)
disp("Detect blobs...")
// edge search
disp("For #1")
S_edge1 = edge(Fr1, 'fftderiv',0.3); // the ratio depends on the image quality
[A1,BB1,ctr1]=Fr_Bkg(S_edge1)
// edge search
disp("For #2")
S_edge2 = edge(Fr2, 'fftderiv',0.05); // the ratio depends on the image quality
[A2,BB2,ctr2]=Fr_Mv(S_edge2)
disp("Time="+string(toc())+"s")
endfunction
//////////////////////////
// point search function on the background screen
// Fr-background screen photo
// BB-selected areas
// cntr0-output array of point centers in the selected area
function [A,BB,cntr0]=Fr_Bkg(Fr)
disp("Time="+string(toc())+"s")
[S_labeled1,n1] = imlabel(Fr);
disp("Found blobs="+string(n1))
disp("Time="+string(toc())+"s")
// find blobs
[A, BB,cntr0] = _imblobprop(S_labeled1)
endfunction
///////////////////////////////////////////////////
// search for offset points on distorted image
// Fr - photo of the background screen with offset points;
// BB-selected areas
// cntr0-output array of point centers in the selected area
function [A,BB,cntr0]=Fr_Mv(Fr)
disp("Time="+string(toc())+"s")
[S_labeled2,n2] = imlabel(Fr);
disp("Found blobs="+string(n2))
disp("Time="+string(toc())+"s")
// find blobs
[A, BB,cntr0] = _imblobprop(S_labeled2)
endfunction
///////////////////////////////////////////////////
// find neighbor points
// searches for points close to the background screen points
// cntr1-background screen points
// cntr2-shift points
// rd is the search radius
function[su,dc,Nn]= find_poins(cntr1, cntr2,A1,A2,rd)
disp("F7")
dc=[]
h = length(cntr1(1,:))
Nn = zeros(h,10)
N=0
for i=1:h
n=find( ( (cntr1(1,i)-cntr2(1,:))^2 + (cntr1(2,i)-cntr2(2,:))^2 )<=rd^2 ) // search within radius
if ~isempty(n)then
dc(2,i)=mean(cntr1(2,i)-cntr2(2,n))// the average distance in x
dc(1,i)=mean(cntr1(1,i)-cntr2(1,n))// the average distance in y
su(1,i)=-(dc(1,i)-cntr1(1,i)) // x shift
su(2,i)=-(dc(2,i)-cntr1(2,i)) // y shift
Nn(i,1:length(n)) = n
N=N+length(n)
else
su(1,i)=cntr1(1,i)
su(2,i)=cntr1(2,i)
end
end
if N < 0.1*h then // if images are very different
disp("Files are very different")
else
disp("Blobs for calc.="+string(N))
end
endfunction
///////////
// Removing the wrong points
function [A,cntr,BB]=sort(A,cntr,BB)
disp("Sort")
mx=max(A)
mn=min(A)
ma=mean(A)
if mx>=2*ma then
n=find(A>mx*0.7)
A(n)=[]
cntr(:,n)=[]
BB(:,n)=[]
disp("dell(1)="+string(length(n)))
end
n=find(A<0.6*mean(A))
disp("dell(2)="+string(length(n)))
cntr(:,n)=[]
BB(:,n)=[]
endfunction
///////////
// Other IPCV function "imblobprop" implementation
// Works about three times faster
function [A, BB, ctr] = _imblobprop(imin)
n = double(max(imin));
A = zeros(1,n);
BB = zeros(4,n);
ctr = zeros(2,n);
for cnt = 1:n
[r,c] = find(imin==cnt);
A(cnt) = length(r)
minx = min(c);
miny = min(r);
maxx = max(c);
maxy = max(r);
w = maxx-minx; // +1
h = maxy-miny; // +1
BB(:,cnt) = [minx; miny; w; h];
avgx = mean(c);
avgy = mean(r);
ctr(:,cnt) = [avgx; avgy];
end
endfunction
///////////////
// Finding the shift
function [i,xm,ym]=shift(BB1,BB2,Nn)
i=find(Nn(:,1)>0)
xm1=BB1(1,i)-BB2(1,Nn(i,1))
xm=2*xm1+(BB1(3,i)-BB2(3,Nn(i,1)))
ym1=BB1(2,i)-BB2(2,Nn(i,1))
ym=2*ym1+(BB1(4,i)-BB2(4,Nn(i,1)))
endfunction
///////////
// Averaging
function [out,X,Y]=avr(cntr,mv)
//
dx=5 // averaging range
//
dy=round(max(cntr(2,:))/3) // // averaging range to y=3
X=[round(min(cntr(1,:))):dx:round(max(cntr(1,:)))]
Y=[round(min(cntr(2,:))):dy:round(max(cntr(2,:)))]
out=double(zeros(length(X),length(Y)))
for x=1:length(X)
nx=find((cntr(1,:)<(X(x)+0.5*dx)) & (cntr(1,:)>(X(x)-0.5*dx)))
//nx=i(ni)
//disp(nx)
if ~isempty(nx)then
for y=1:length(Y)
ny=find((cntr(2,nx)<(Y(y)+0.5*dy)) & (cntr(2,nx)>(Y(y)-0.5*dy)))
//disp(ny)
if ~isempty(ny)then
out(x,y)=mean(mv(nx(ny)))
else
out(x,y)=%nan
end
end
else
out(x,:)=%nan
end
end
endfunction
////////////////////////////
// Main
////////////////////////////
// To insert a file path
filename_1="F:/TMP/file1.png"//"Image file path" // background image
filename_2="F:/TMP/file2.png"//"Image file path" // distorted image
S1=imread(filename_1)
S2=imread(filename_2)
rd=15 // search radius
S12=Im_pr1(S1)
S22=Im_pr2(S2)
tic();
[ys,xs]=size(S1)
f1 = scf();
f1.figure_size= [xs,ys];
subplot(1,2,1)
imshow(S12);
xtitle("File #1")
//
subplot(1,2,2)
imshow(S22);
xtitle("File #2")
//
disp("Time="+string(toc())+"s")
//
[ctr0, ctr1, BB1, BB2,A1,A2]=Detect_point(S12, S22)
disp("Time="+string(toc())+"s")
//
[A1,ctr0,BB1]=sort(A1,ctr0,BB1)
[A2,ctr1,BB2]=sort(A2,ctr1,BB2)
//
[su,dc,Nn]= find_poins(ctr0, ctr1, A1,A2,rd)
disp("Time="+string(toc())+"s")
//
[i,xm,ym]=shift(BB1,BB2,Nn)
f2 = scf();
gcf().color_map = hotcolormap(64);
subplot(1,2,1)
scatter3(ctr0(1,i),ys-ctr0(2,i)+1,(xm),5,xm)
xtitle("Shift X")
subplot(1,2,2)
scatter3(ctr0(1,i),ys-ctr0(2,i)+1,(ym),5,ym)
xtitle("Shift Y")
//
[out,X,Y]=avr(ctr0(:,i),xm)
//
f3 = scf();
plot(X,out(:,:),"b." )
xgrid(color("grey"));
xtitle("All shift points X")
//
// If necessary, delete comments
///////////////
// save to file
//yy=out(:,1)' // 1 or 2 ...
//Dell Nan
//k = find(~isnan(out(:,1))) // 1 or 2 ...
//yy = out(k,1) // 1 or 2 ...
//Mx=[X(k)',yy]
// saves only the x shift.
//savepath="path_to_save"
//filesave="name_file"
//csvWrite(Mx, savepath+filesave+".txt",ascii(9));
//