forked from alok-ai-lab/DeepInsightTab2Image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnowFall.m
482 lines (457 loc) · 12.9 KB
/
SnowFall.m
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
function [xps,yps,As,Bs]=SnowFall(xp,yp,FIG,A,B)
% [xps,yps,As,Bs]=SnowFa;;(xp,yp,FIG,A,B)
% Snowfall compression algorithm for DeepInsight
if nargin<3
FIG=0;
end
% change y-axis so that it can be related with Cartesian Coordinates
if size(xp,1)>size(xp,2)
xp=xp';
yp=yp';
end
yp=-yp;
z = [xp;yp];
M = mean(z',1)';
M = round(M);
L = length(xp);
if FIG==1
% figure % remove after testing;
Ainit=abs(max(xp)-min(xp))+1;
Binit=abs(max(yp)-min(yp))+1;
N=max([Ainit,Binit]);
disp_cnt=1;
if N<200
%N=100;
X=ones(N);
idx=sub2ind(size(X),xp,-yp);
X(idx)=0;
figure; imagesc(X'); grid on;
%M(1)=49;%11;%5;%3;%5;
%M(2)=-50;%-12;%-6;%-4;%-6;
hold on;
X(M(1),-M(2))=0.5;
imagesc(X');
hold off;
pause(0.5);
F(1)=getframe(gcf);
drawnow
im_cnt=2;
else
disp('size of pixel frame is very large');
end
%###################
end
%center
xc=M(1);
yc=M(2);
% distance of {xp,yp} from center {xc,yc}
for j=1:L
D(j)=norm(z(:,j)-M);
end
[d,inx]=sort(D);
center_taken = 0;
for j=1:L
xpn = xp(inx(j));
ypn = yp(inx(j));
zpn=[xpn;ypn];
% Find samples in area A
if xpn<=xc & yc <= ypn
% Quadrant 1
%search xp <= xu <= xc and yc <=yu <= yp
r=[(z(1,:)<=xc & z(1,:)>=xpn);(z(2,:)<=ypn & z(2,:) >= yc)];
r=and(r(1,:),r(2,:));
zu=z(:,r); % xu and yu in area A
elseif xpn <= xc & ypn <= yc
% Quadrant 2
%search xp <= xu <= xc and yp <= yu < yc
r=[(z(1,:)<=xc & z(1,:)>=xpn);(z(2,:)<= yc & z(2,:) >= ypn)];
r=and(r(1,:),r(2,:));
zu=z(:,r); % xu and yu in area A
elseif xpn >= xc & yc >= ypn
% Quadrant 3
%search xp >= xu > xc and yc >= yu >= yp
r=[(z(1,:) >= xc & z(1,:) <= xpn);(z(2,:)<= yc & z(2,:) >= ypn)];
r=and(r(1,:),r(2,:));
zu=z(:,r); % xu and yu in area A
elseif xpn >= xc & ypn >= yc
% Quadrant 4
%search xp >= xu > xc and yp >= yu > yc
r=[(z(1,:) >= xc & z(1,:) <= xpn);(z(2,:) >= yc & z(2,:) <= ypn)];
r=and(r(1,:),r(2,:));
zu=z(:,r); % xu and yu in area A
end
%Gradient of point xpn and ypn wrt xc and yc
m = (ypn-yc)/(xpn-xc);
if isnan(m)==1
m=0;
end
if sum(r)<=2
% If no point at xc,yc then place a point
if center_taken == 0
xp_prime = xc;
yp_prime = yc;
center_taken = 1;
else
% only two points in a given area A ({xp,yp} and {xc,yc})
if m==0
if xpn < xc
xp_prime = xc - 0.5;
yp_prime = yc;
else
xp_prime = xc + 0.5;
yp_prime = yc;
end
elseif isinf(m)==1
if ypn < yc
yp_prime = yc - 0.5;
xp_prime = xc;
else
yp_prime = yc + 0.5;
xp_prime = xc;
end
else
if xpn < xc
xp_prime = xc - 0.5;
yp_prime = m*xp_prime + (yc - m*xc);
else
xp_prime = xc + 0.5;
yp_prime = m*xp_prime + (yc - m*xc);
end
end
clear xp_tmp yp_tmp dcp xi_tmp yi_tmp dcp
xp_tmp(1) = floor(xp_prime); yp_tmp(1) = floor(yp_prime);
xp_tmp(2) = floor(xp_prime); yp_tmp(2) = ceil(yp_prime);
xp_tmp(3) = ceil(xp_prime); yp_tmp(3) = floor(yp_prime);
xp_tmp(4) = ceil(xp_prime); yp_tmp(4) = ceil(yp_prime);
Cond=[];
for cnd=1:4
if xc==xp_tmp(cnd) & yc==yp_tmp(cnd)
Cond = [Cond,cnd];
end
end
% ensure no overlaps
TotalCnd=1:4;
if length(Cond)>0
Y=TotalCnd~=Cond(1);
if length(Cond)>1
for cnd=1:length(Cond)
Y=and(Y,TotalCnd~=Cond(cnd));
end
end
LeftCond = TotalCnd(Y);
if m~=0 & isinf(m)~=1
xi_tmp = ((yp_tmp(LeftCond) + xp_tmp(LeftCond)./m) - (yc - m*xc))/(m+1/m);
yi_tmp = m.*xi_tmp + (yc - m*xc);
clear dpi_tmp
dpi_tmp = sqrt((xi_tmp-xp_tmp(LeftCond)).^2 + (yi_tmp-yp_tmp(LeftCond)).^2);
[row,inx_pi_tmp]=min(dpi_tmp);
inx_pi_tmp=LeftCond(inx_pi_tmp);
xp_prime = xp_tmp(inx_pi_tmp);
yp_prime = yp_tmp(inx_pi_tmp);
else
dcp=sqrt((xc-xp_tmp(LeftCond)).^2 + (yc-yp_tmp(LeftCond)).^2);
[row,inx_cp]=min(dcp);
inx_cp=LeftCond(inx_cp);
xp_prime = xp_tmp(inx_cp);
yp_prime = yp_tmp(inx_cp);
end
else
if xpn < xc
xp_prime = floor(xp_prime);
else
xp_prime = ceil(xp_prime);
end
if ypn < yc
yp_prime = floor(yp_prime);
else
yp_prime = ceil(yp_prime);
end
end
% if xpn < xc
% xp_prime = floor(xp_prime);
% else
% xp_prime = ceil(xp_prime);
% end
% if ypn < yc
% yp_prime = floor(yp_prime);
% else
% yp_prime = ceil(yp_prime);
% end
end
z(1,inx(j))=xp_prime;
z(2,inx(j))=yp_prime;
else
% if n number of points are in area A (where, n>2)
% for a=1:size(zu,2)
% dr(a) = norm(zu(:,a)-zpn);
% end
dr = sqrt((xpn-zu(1,:)).^2 + (ypn-zu(2,:)).^2);
[distr,inx_r] = sort(dr);
zr = zu(:,inx_r(2)); % because first point is xpn,ypn
dpr = dr(inx_r(2));
xr=zr(1);
yr=zr(2);
if m==0
if xpn < xr
xi = xr - 0.5;
yi = yc;
else
xi = xr + 0.5;
yi = yc;
end
%xi = xr;
%yi = yc;
elseif isinf(m)==1
if ypn < yr
yi = yr - 0.5;
xi = xc;
else
yi = yr + 0.5;
xi = xc;
end
%xi = xc;
%yi = yr;
else
xi = ((yr + xr/m) - (yc - m*xc))/(m+1/m);
yi = m*xi + (yc - m*xc);
end
clear xp_prime yp_prime
xp_tmp(1) = floor(xi); yp_tmp(1) = floor(yi);
xp_tmp(2) = floor(xi); yp_tmp(2) = ceil(yi);
xp_tmp(3) = ceil(xi); yp_tmp(3) = floor(yi);
xp_tmp(4) = ceil(xi); yp_tmp(4) = ceil(yi);
Cond=[];
for cnd=1:4
if xr==xp_tmp(cnd) & yr==yp_tmp(cnd)
Cond = [Cond,cnd];
end
end
TotalCnd=1:4;
if length(Cond)>0
Y=TotalCnd~=Cond(1);
if length(Cond)>1
for cnd=1:length(Cond)
Y=and(Y,TotalCnd~=Cond(cnd));
end
end
LeftCond = TotalCnd(Y);
else
LeftCond=1:4; %no overlap
end
if length(LeftCond)>0 & length(LeftCond)<=4
%i.e. some locations overlap with (xr,yr) when LeftCond >0 & LeftCond <4
% and no overlap when LeftCond ==4
dpi = sqrt((xpn-xp_tmp(LeftCond)).^2 + (ypn-yp_tmp(LeftCond)).^2);
inx_dpi = dpi<dpr;
if sum(inx_dpi)==0
dcp=sqrt((xc-xp_tmp(LeftCond)).^2 + (yc-yp_tmp(LeftCond)).^2);
[row,inx_cp]=min(dcp);
inx_cp=LeftCond(inx_cp);
xp_prime = xp_tmp(inx_cp);
yp_prime = yp_tmp(inx_cp);
else
dist = dpi(inx_dpi);
inx_dpi = LeftCond(inx_dpi);
[row,col]=max(dist);
inx_dpi = inx_dpi(col);
xp_prime = xp_tmp(inx_dpi);
yp_prime = yp_tmp(inx_dpi);
end
elseif length(LeftCond)==0
%i.e. all locations overlap with (xr,yr)
if m==0
if xpn < xi
xp_prime = xi - 0.5;
yp_prime = yi;
else
xp_prime = xi + 0.5;
yp_prime = yi;
end
elseif isinf(m)==1
if ypn < yi
yp_prime = yi - 0.5;
xp_prime = xi;
else
yp_prime = yi + 0.5;
xp_prime = xi;
end
else
if xpn < xi
xp_prime = xi - 0.5;
yp_prime = m*xp_prime + (yc - m*xc);
else
xp_prime = xi + 0.5;
yp_prime = m*xp_prime + (yc - m*xc);
end
end
if xpn < xi
xp_prime = floor(xp_prime);
else
xp_prime = ceil(xp_prime);
end
if ypn < yi
yp_prime = floor(yp_prime);
else
yp_prime = ceil(yp_prime);
end
% elseif length(LeftCond)==4
% %i.e. no location overlap with (xr,yr)
% for cnd=1:4
% dpi(cnd)=sqrt((xpn-xp_tmp(cnd))^2 + (ypn-yp_tmp(cnd))^2);
% % 1) floor(xi), floor(yi)
% % 2) floor(xi), ceil(yi)
% % 3) ceil(xi), floor(yi)
% % 4) ceil(xi), ceil(yi)
% end
% inx_dpi = dpi < dpr;
% dist = dpi(inx_dpi);
% inx_dpi = LeftCond(inx_dpi);
% [row,col] = max(dist);
% inx_dpi = inx_dpi(col);
% xp_prime = xp_tmp(inx_dpi);
% yp_prime = yp_tmp(inx_dpi);
end
clear dpi dr
z(1,inx(j))=xp_prime;
z(2,inx(j))=yp_prime;
end
if FIG==1
% remove later
if N<200
X(xpn,-ypn)=1;
X(xp_prime,-yp_prime)=0;
hold on;
%X=ones(N);
%idx=sub2ind(size(X),z(1,:),-z(2,:));
%X(idx)=0;
imagesc(X'); %grid on;
%pause(0.1);
F(im_cnt)=getframe(gcf);
drawnow
im_cnt=im_cnt+1;
else
if disp_cnt==1
disp('Pixel size is very large');
disp_cnt=disp_cnt+1;
end
end
% ############
end
end
xps=z(1,:);
yps=-z(2,:);
As=abs(max(xps)-min(xps))+1;
Bs=abs(max(yps)-min(yps))+1;
%zoomed
xps = xps - (max(xps)-As);
yps = yps - (max(yps)-Bs);
if As<350 & Bs<350
%if FIG==1
Y=ones(As,Bs);
idy=sub2ind(size(Y),xps,yps);
Y(idy)=0;
figure; imagesc(Y'); grid on;
%end
end
if exist('A')==1 & exist('B')==1
if (As>=A & Bs>B) | (As>A & Bs>=B)
yps=-yps;
A=A-1;
B=B-1;
xps = round(1+(A*(xps-min(xps))/(max(xps)-min(xps))));
yps = round(1+(-B)*(yps-max(yps))/(max(yps)-min(yps)));
As=max(xps);
Bs=max(yps);
Z=ones(As,Bs);
idz=sub2ind(size(Z),xps,yps);
Z(idz)=0;
figure;imagesc(Z'); grid on;
elseif As>A | Bs>B %This IF statement added on 31-Oct-2019
yps=-yps;
A=A-1;
B=B-1;
xps = round(1+(A*(xps-min(xps))/(max(xps)-min(xps))));
yps = round(1+(-B)*(yps-max(yps))/(max(yps)-min(yps)));
As=max(xps);
Bs=max(yps);
Z=ones(As,Bs);
idz=sub2ind(size(Z),xps,yps);
Z(idz)=0;
figure;imagesc(Z'); grid on;
end %#### newly added
end
% % Rotate axis using Convec-Hull algorithm
% yps = -yps;
% % should have a nearly square bounding rectangle
% [xrect,yrect] = minboundrect(xps,yps);
%
%
% %gradient (m) of a line y=mx+c
% grad = (yrect(2)-yrect(1))/(xrect(2)-xrect(1));
% theta = atan(grad);
%
% %Rotation matrix
% %theta=180-theta
% R=[cos(theta) sin(theta);-sin(theta) cos(theta)];
%
% % rotated rectangle
% zrect = R*[xrect';yrect'];
%
% % rotated data
% z = R*[xps;yps];
% z=z';
%
% zUp=round(z);
% zr=size(unique(zUp,'rows'));
% zUp=floor(z);
% zf=size(unique(zUp,'rows'));
% zUp=ceil(z);
% zc=size(unique(zUp,'rows'));
% clear zUp
% if zr(1)==size(z,1)
% z=round(z);
% z=(unique(z,'rows'));
% xps=z(:,1);
% yps=-z(:,2);
% As=abs(max(xps)-min(xps))+1;
% Bs=abs(max(yps)-min(yps))+1;
% xps = xps - (max(xps)-As);
% yps = yps - (max(yps)-Bs);
% elseif zf(1)==size(z,1)
% z=floor(z);
% z=(unique(z,'rows'));
% xps=z(:,1);
% yps=-z(:,2);
% As=abs(max(xps)-min(xps))+1;
% Bs=abs(max(yps)-min(yps))+1;
% xps = xps - (max(xps)-As);
% yps = yps - (max(yps)-Bs);
% elseif zc(1)==size(z,1)
% z=ceil(z);
% z=(unique(z,'rows'));
% xps=z(:,1);
% yps=-z(:,2);
% As=abs(max(xps)-min(xps))+1;
% Bs=abs(max(yps)-min(yps))+1;
% xps = xps - (max(xps)-As);
% yps = yps - (max(yps)-Bs);
% end
%
% Z=ones(As,Bs);
% idz=sub2ind(size(Z),xps,yps);
% Z(idz)=0;
% figure; imagesc(Z'); grid on;
if FIG==1
%video
writerObj = VideoWriter('SnowFall_Video.avi');
writerObj.FrameRate = 10;
open(writerObj);
% write the frames to the video
for i=1:length(F)
% convert the image to a frame
frame = F(i) ;
writeVideo(writerObj, frame);
end
% close the writer object
close(writerObj);
end