-
Notifications
You must be signed in to change notification settings - Fork 9
/
compute_nrvqa_features.m
1201 lines (1028 loc) · 43.6 KB
/
compute_nrvqa_features.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% compute_nrvqa_features.m
%
% Use this function to compute the No-Reference (NR) quality fetures
% for a wild test video sequence.
%
%
% Input:
% test_video: Path to the test video file (YUV420 format)
% reso: Resolution of the YUV video [width,height]
% blk_len: Length of the block segment used for feature
% computation (e.g. number of frames per second
% for one second blocks)
%
% Output:
% all_features: Resulting NR feature vector, including temporal,
% spatial and motion consistency features
%
function all_features = compute_nrvqa_features(test_video, reso, blk_len)
width = reso(1);
height = reso(2);
% Try to open test_video; if cannot, return
test_file = fopen(test_video,'r');
if test_file == -1
fprintf('Test YUV file not found.');
all_features = [];
return;
end
% Open test video file
fseek(test_file, 0, 1);
file_length = ftell(test_file);
fprintf('Video file size: %d bytes (%d frames)\n',file_length, ...
floor(file_length/width/height/1.5));
frame_start = 1;
frame_end = (floor(file_length/width/height/1.5)-5);
first_frame_loaded = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Loop through all the frames in the frame_range to compute the
% temporal features
%
fprintf('Computing LC features for frames %d..%d\n', ...
frame_start, frame_end);
LC_features_all = [];
for i = frame_start:2:frame_end
% Read frames i-i, i and i+1 (note that frame_start must be > 0)
if first_frame_loaded
prev_YUV_frame = next_YUV_frame;
this_YUV_frame = YUVread(test_file,[width height],i);
next_YUV_frame = YUVread(test_file,[width height],i+1);
else
prev_YUV_frame = YUVread(test_file,[width height],i-1);
this_YUV_frame = YUVread(test_file,[width height],i);
next_YUV_frame = YUVread(test_file,[width height],i+1);
first_frame_loaded = 1;
end
% Compute temporal features for each frame
ftr_vec = compute_LC_features(this_YUV_frame, ...
prev_YUV_frame, ...
next_YUV_frame);
% Add newly computed temporal features to temporal feature matrix
LC_features_all = [LC_features_all; ftr_vec];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Loop through the segments to compute motion consistency features
%
cons_features = [];
LC_features = [];
n_temp_vecs = length(LC_features_all(:,1));
half_blk_len = floor(blk_len/2);
fprintf('Pooling LC and consistency features\n');
if frame_end-frame_start>blk_len
for i=1:half_blk_len:n_temp_vecs-half_blk_len
i_start = i;
i_end = i+half_blk_len;
% Compute onsistency features
blr_si_corr = 0;
if std(LC_features_all(i_start:i_end,1))>0 && ...
std(LC_features_all(i_start:i_end,11))>0
blr_si_corr = corr(LC_features_all(i_start:i_end,1),...
LC_features_all(i_start:i_end,11));
end
cons_features = [cons_features;
std(LC_features_all(i_start:i_end,1:22))...
blr_si_corr];
% Average pooling for Low Complexity features
LC_features = [LC_features;
mean(LC_features_all(i_start:i_end,1:22))];
end
else
cons_features = zeros(1,23);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Loop through the segments to compute spatial features
%
spat_min_distance = min(5, half_blk_len-1);
i_start = 1;
i = 1;
fr_idx = [];
% First, find the representative frames
while i < (n_temp_vecs-half_blk_len)
span = max(i,i_start):i+half_blk_len;
LC_features_all(span,:);
avg_features = mean(LC_features_all(span,:));
diffs = sum(abs(LC_features_all(span,:)-avg_features)');
idx = span(find(diffs == min(diffs)));
fr_idx = [fr_idx idx(1)];
i_start = idx(1)+spat_min_distance;
i = i+half_blk_len;
end
% Compute the High Complexity features for the representative frames
HC_features = [];
for i=fr_idx
YUV_frame = YUVread(test_file,[width height],frame_start+(i-1)*2);
fprintf('Computing HC features for the frame %d\n',...
frame_start+(i-1)*2);
ftrs = compute_HC_features(YUV_frame);
HC_features = [HC_features; ftrs];
end
% Combine feature vectors
all_features = [mean(LC_features) ...
mean(cons_features) ...
mean(HC_features)];
fclose(test_file);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function computes the low complexity features
%
function features = compute_LC_features(this_fr, prev_fr, next_fr)
[height,width,~] = size(this_fr);
% Try to detect interlacing
im_odd_hor = this_fr(1:2:end,:,1);
im_even_hor = this_fr(2:2:end,:,1);
im_odd_ver = this_fr(:,1:2:end,1);
im_even_ver = this_fr(:,2:2:end,1);
vec_v = sort((im_odd_ver(:)-im_even_ver(:)).^2,'descend');
vec_h = sort((im_odd_hor(:)-im_even_hor(:)).^2,'descend');
ver = mean(vec_v(1:floor(0.001*end)));
hor = mean(vec_h(1:floor(0.001*end)));
interlace = 0;
if ver>0 || hor>0
interlace = min(ver,hor)/max(ver,hor);
end
% Simple blurriness estimation
H = [-1 -2 -1; 1 2 1; 0 0 0]./8;
sob_h_this = imfilter(this_fr(:,:,1),H');
sob_v_this = imfilter(this_fr(:,:,1),H);
sob_h_this_2 = imfilter(sob_h_this,H');
sob_v_this_2 = imfilter(sob_v_this,H);
sob_h_this = sob_h_this(4:end-3,4:end-3);
sob_v_this = sob_v_this(4:end-3,4:end-3);
sob_h_this_2 = sob_h_this_2(3:end-2,3:end-2);
sob_v_this_2 = sob_v_this_2(3:end-2,3:end-2);
sob_1 = (sob_h_this(:).^2+sob_v_this(:).^2);
sob_1 = sort(sob_1,'descend');
sob_2 = (sob_h_this_2(:).^2+sob_v_this_2(:).^2);
sob_2 = sort(sob_2,'descend');
mean_sob = mean(sob_1(1:floor(0.1*end)));
mean_sob_2 = mean(sob_2(1:floor(0.1*end)));
blur = 0;
if mean_sob>0
blur = (mean_sob_2/mean_sob);
end
% Initialize parameters
bl_size = floor(width/40);
src_win = floor(width/40);
% The following computations are done with reduced resolution
this_fr = imresize(this_fr,0.5);
prev_fr = imresize(prev_fr,0.5);
next_fr = imresize(next_fr,0.5);
[height,width,~] = size(this_fr);
this_Y = this_fr(:,:,1);
prev_Y = prev_fr(:,:,1);
next_Y = next_fr(:,:,1);
this_fr = ycbcr2rgb(this_fr);
% Apply Sobel filter to the frames
H = [-1 -2 -1; 0 0 0; 1 2 1]./8;
sob_h_this = imfilter(this_Y,H');
sob_v_this = imfilter(this_Y,H);
% Reset edge pixels in the Sobeled frames
sob_h_this(1:4,1:width)=0;
sob_h_this(height-3:height,1:width)=0;
sob_h_this(1:height,1:4)=0;
sob_h_this(1:height,width-3:width)=0;
sob_v_this(1:4,1:width)=0;
sob_v_this(height-3:height,1:width)=0;
sob_v_this(1:height,1:4)=0;
sob_v_this(1:height,width-3:width)=0;
sob_tot = sqrt(sob_v_this.^2+sob_h_this.^2);
sob_h_prev = imfilter(prev_Y,H');
sob_v_prev = imfilter(prev_Y,H);
sob_h_next = imfilter(next_Y,H');
sob_v_next = imfilter(next_Y,H);
H1 = [1 1 1 1 1;1 1 1 1 1;-2 -2 0 1 1;-2 -2 -2 1 1;-2 -2 -2 1 1]./32;
H2 = [-2 -2 -2 1 1;-2 -2 -2 1 1;-2 -2 0 1 1;1 1 1 1 1;1 1 1 1 1]./32;
H3 = [1 1 -2 -2 -2;1 1 -2 -2 -2;1 1 0 -2 -2;1 1 1 1 1;1 1 1 1 1]./32;
H4 = [1 1 1 1 1;1 1 1 1 1;1 1 0 -2 -2;1 1 -2 -2 -2;1 1 -2 -2 -2]./32;
corner_avg(:,:,1) = abs(imfilter(this_Y, H1));
corner_avg(:,:,2) = abs(imfilter(this_Y, H2));
corner_avg(:,:,3) = abs(imfilter(this_Y, H3));
corner_avg(:,:,4) = abs(imfilter(this_Y, H4));
corner_max = max(corner_avg,[],3);
corner_this = corner_max-min(corner_avg,[],3);
mot_threshold = 0.01;
cor_max = sort(corner_max(:),'ascend');
glob_blockiness = 0;
if std2(cor_max(1:floor(0.99*end)))>0
glob_blockiness = 0.5*((mean(cor_max(1:floor(0.99*end)))/ ...
std2(cor_max(1:floor(0.99*end))))^2);
end
% Reset edge pixels in the corner point filtered frame
corner_this(1:src_win+3,1:width)=0;
corner_this(height-src_win-2:height,1:width)=0;
corner_this(1:height,1:src_win+3)=0;
corner_this(1:height,width-src_win-2:width)=0;
corner_this_copy = corner_this(:);
key_pix = zeros((height-6)*(width-6),2);
n_key_pix = 0;
im_y_vec = mod(0:width*height, height)+1;
im_x_vec = floor((0:width*height-1)/height)+1;
sob_this_cp = corner_this_copy(corner_this_copy>mot_threshold);
im_y_vec = im_y_vec(corner_this_copy>mot_threshold);
im_x_vec = im_x_vec(corner_this_copy>mot_threshold);
% In the following loop, find the key pixels
[mx,idx] = max(sob_this_cp);
if ~isempty(idx)
while mx>mot_threshold
i = im_y_vec(idx(1));
j = im_x_vec(idx(1));
n_key_pix = n_key_pix + 1;
key_pix(n_key_pix,:) = [i j];
idx_remove = find(im_y_vec>=i-floor(bl_size) & ...
im_y_vec<=i+floor(bl_size) & ...
im_x_vec>=j-floor(bl_size) & ...
im_x_vec<=j+floor(bl_size));
sob_this_cp(idx_remove)=[];
im_y_vec(idx_remove)=[];
im_x_vec(idx_remove)=[];
[mx,idx] = max(sob_this_cp);
end
end
key_pix=key_pix(1:n_key_pix,:);
non_mot_area = ones(height, width);
num_mot_points = 0;
max_mot_points = (height/bl_size)*(width/bl_size);
%tic
% In the following loop, find the motion vectors for each key pixel
motion_vec = [];
distance_matrix = ones(2*src_win+1);
for i=1:2*src_win+1
for j=1:2*src_win+1
distance_matrix(i,j) = ...
sqrt((1+src_win-i).^2+(1+src_win-j).^2)/sqrt(2*src_win^2);
end
end
distances = distance_matrix(:);
uncertain = 0;
% Loop through the key pixels
for z = 1:n_key_pix
tar_y = key_pix(z,1);
tar_x = key_pix(z,2);
match_y_bw = tar_y;
match_x_bw = tar_x;
match_y_fw = tar_y;
match_x_fw = tar_x;
surr_win_v_prev = sob_v_prev(tar_y-src_win-2:tar_y+src_win+2, ...
tar_x-src_win-2:tar_x+src_win+2);
surr_win_h_prev = sob_h_prev(tar_y-src_win-2:tar_y+src_win+2, ...
tar_x-src_win-2:tar_x+src_win+2);
diff_win_prev = (sob_v_this(tar_y, tar_x)-surr_win_v_prev).^2 + ...
(sob_h_this(tar_y, tar_x)-surr_win_h_prev).^2;
surr_win_v_next = sob_v_next(tar_y-src_win-2:tar_y+src_win+2, ...
tar_x-src_win-2:tar_x+src_win+2);
surr_win_h_next = sob_h_next(tar_y-src_win-2:tar_y+src_win+2, ...
tar_x-src_win-2:tar_x+src_win+2);
diff_win_next = (sob_v_this(tar_y, tar_x)-surr_win_v_next).^2 + ...
(sob_h_this(tar_y, tar_x)-surr_win_h_next).^2;
for i=-1:1
for j=-1:1
if i~=0 || j~=0
diff_win_prev(3:end-2,3:end-2) = ...
diff_win_prev(3:end-2,3:end-2) + ...
(sob_v_this(tar_y+i, tar_x+j)- ...
surr_win_v_prev(3+i:end-2+i,3+j:end-2+j)).^2+ ...
(sob_h_this(tar_y+i, tar_x+j)- ...
surr_win_h_prev(3+i:end-2+i,3+j:end-2+j)).^2;
diff_win_next(3:end-2,3:end-2) = ...
diff_win_next(3:end-2,3:end-2) + ...
(sob_v_this(tar_y+i, tar_x+j)- ...
surr_win_v_next(3+i:end-2+i,3+j:end-2+j)).^2+...
(sob_h_this(tar_y+i, tar_x+j)- ...
surr_win_h_next(3+i:end-2+i,3+j:end-2+j)).^2;
end
end
end
diff_win_prev = diff_win_prev(3:end-2,3:end-2);
diff_win_next = diff_win_next(3:end-2,3:end-2);
orig_diff_bw = diff_win_prev(1+src_win,1+src_win);
orig_diff_fw = diff_win_next(1+src_win,1+src_win);
diff_bw = diff_win_prev(1+src_win,1+src_win);
if orig_diff_bw>0.005
[sorted,idx] = sort(diff_win_prev(:),'ascend');
min_diff = orig_diff_bw;
if length(sorted)>=2
if sorted(1)<=0.8*sorted(2) || ...
distances(idx(1))<distances(idx(2))
min_diff = sorted(1);
else
[idx,~] = find(0.8.*diff_win_prev(:)<=sorted(1));
[~,idx2] = sort(distances(idx),'ascend');
if diff_win_next(idx(idx2(1)))<1.1*sorted(1)
min_diff = diff_win_prev(idx(idx2(1)));
elseif sorted(1)<diff_bw*0.9
min_diff = sorted(1);
end
uncertain = uncertain + 1;
end
if min_diff*1.01<orig_diff_bw
[y,x] = find(diff_win_prev==min_diff);
match_y_bw = tar_y+y(1)-src_win-1;
match_x_bw = tar_x+x(1)-src_win-1;
diff_bw = diff_win_prev(y(1),x(1));
end
end
end
diff_fw = diff_win_next(1+src_win,1+src_win);
if orig_diff_fw>0.005
[sorted,idx] = sort(diff_win_next(:),'ascend');
min_diff = orig_diff_fw;
if length(sorted)>=2
if sorted(1)<0.8*sorted(2) || ...
distances(idx(1))<distances(idx(2))
min_diff = sorted(1);
else
[idx,~] = find(0.8.*diff_win_next(:)<=sorted(1));
[~,idx2] = sort(distances(idx),'ascend');
if diff_win_next(idx(idx2(1)))<1.1*sorted(1)
min_diff = diff_win_next(idx(idx2(1)));
elseif sorted(1)<diff_fw*0.9
min_diff = sorted(1);
end
uncertain = uncertain + 1;
end
if min_diff*1.01<orig_diff_fw
[y,x] = find(diff_win_next==min_diff);
match_y_fw = tar_y+y(1)-src_win-1;
match_x_fw = tar_x+x(1)-src_win-1;
diff_fw = diff_win_next(y(1),x(1));
end
end
end
% Add motion vector to the list of motion vectors
if (orig_diff_bw > diff_bw*1.01 && ...
(tar_y ~= match_y_bw || tar_x ~= match_x_bw)) || ...
(orig_diff_fw > diff_fw*1.01 && ...
(tar_y ~= match_y_fw || tar_x ~= match_x_fw))
non_mot_area(max(1,tar_y-bl_size):min(height,tar_y+bl_size),...
max(1,tar_x-bl_size):min(width,tar_x+bl_size))=0;
non_mot_area(max(1,match_y_bw-bl_size): ...
min(height,match_y_bw+bl_size),...
max(1,match_x_bw-bl_size):...
min(width,match_x_bw+bl_size)) = 0;
non_mot_area(max(1,match_y_fw-bl_size):...
min(height,match_y_fw+bl_size),...
max(1,match_x_fw-bl_size):...
min(width,match_x_fw+bl_size)) = 0;
end
num_mot_points = num_mot_points + 1;
motion_vec = [motion_vec; ...
tar_y-match_y_bw tar_x-match_x_bw ...
match_y_fw-tar_y match_x_fw-tar_x ...
tar_y tar_x ...
orig_diff_bw diff_bw ...
orig_diff_fw diff_fw];
end
%toc
% Compute motion point related statistics
motion_uncertainty = 0.5*uncertain/max_mot_points;
motion_density = 0;
motion_intensity = 0;
std_mot_intensity = 0;
avg_mot_pos = 0;
avg_mot_sprd = 0;
mot_pred_acc = 0;
mot_y = 0.5;
mot_x = 0.5;
jerkiness = 0;
jerk_cons = 0;
motion_vec_bg = [];
num_bg_mot_points = 0;
if num_mot_points>0
motion_density = num_mot_points/(width*height/bl_size^2);
mot_intensity_vec = sqrt(((motion_vec(:,1)./src_win).^2 + ...
(motion_vec(:,2)./src_win).^2 + ...
(motion_vec(:,3)./src_win).^2 + ...
(motion_vec(:,4)./src_win).^2)./4.0);
sum_mot_int = sum(mot_intensity_vec);
motion_intensity = (sum(mot_intensity_vec)/max_mot_points)^0.25;
std_mot_intensity = std(mot_intensity_vec);
if sum_mot_int>0
% Compute motion position in relation with the screen midpoint
avg_motp_y = sum(mot_intensity_vec.*motion_vec(:,5))/...
sum_mot_int;
std_motp_y = sqrt(sum(mot_intensity_vec.*...
(motion_vec(:,5)-avg_motp_y).^2)/sum_mot_int);
avg_mot_pos_y = (avg_motp_y-height/2)/(height/2);
sprd_mot_pos_y = std_motp_y/height;
avg_motp_x = sum(mot_intensity_vec.*motion_vec(:,6))/...
sum_mot_int;
std_motp_x = sqrt(sum(mot_intensity_vec.*...
(motion_vec(:,6)-avg_motp_x).^2)/sum_mot_int);
avg_mot_pos_x = (avg_motp_x-width/2)/(width/2);
sprd_mot_pos_x = std_motp_x/width;
avg_mot_pos = sqrt(avg_mot_pos_y^2+avg_mot_pos_x^2);
avg_mot_sprd = sqrt(sprd_mot_pos_y^2+sprd_mot_pos_x^2);
% Mean motion along x and y axis
mot_y = mean(0.25.*(motion_vec(:,1)+motion_vec(:,3))./ ...
src_win+0.5);
mot_x = mean(0.25.*(motion_vec(:,2)+motion_vec(:,4))./ ...
src_win+0.5);
% Average motion prediction improvement
mot_pred_acc_bw = mean(motion_vec(:,7)-motion_vec(:,8));
mot_pred_acc_fw = mean(motion_vec(:,9)-motion_vec(:,10));
mot_pred_acc = 0.5*(mot_pred_acc_bw+mot_pred_acc_fw).^0.5;
% Motion jerkiness
mot_y_diff = 0.5.*(motion_vec(:,1)'-motion_vec(:,3)')./src_win;
mot_x_diff = 0.5.*(motion_vec(:,2)'-motion_vec(:,4)')./src_win;
mot_diff = sqrt(mot_y_diff.^2+mot_x_diff.^2);
jerkiness = mean(mot_diff.^0.5);
jerk_cons = std(mot_diff.^0.5);
end
avg_mot_x = mean(0.5.*motion_vec(:,2)+0.5.*motion_vec(:,4));
avg_mot_y = mean(0.5.*motion_vec(:,1)+0.5.*motion_vec(:,3));
std_mot_x = std(0.5.*motion_vec(:,2)+0.5.*motion_vec(:,4));
std_mot_y = std(0.5.*motion_vec(:,1)+0.5.*motion_vec(:,3));
for z=1:num_mot_points
mot_x_this = 0.5*motion_vec(z,2)+0.5*motion_vec(z,4);
mot_y_this = 0.5*motion_vec(z,1)+0.5*motion_vec(z,3);
if mot_x_this > avg_mot_x-std_mot_x && ...
mot_x_this < avg_mot_x+std_mot_x && ...
mot_y_this > avg_mot_y-std_mot_y && ...
mot_y_this < avg_mot_y+std_mot_y
num_bg_mot_points = num_bg_mot_points + 1;
motion_vec_bg = [motion_vec_bg; motion_vec(z,:)];
end
end
end
% Compute motion point related statistics
egomotion_density = 0;
egomotion_intensity = 0;
std_egomot_intensity = 0;
avg_egomot_pos = 0;
avg_egomot_sprd = 0;
egomot_pred_acc = 0;
mot_y_bg = 0.5;
mot_x_bg = 0.5;
if num_bg_mot_points>0
egomotion_density = num_bg_mot_points/(width*height/bl_size^2);
bg_mot_intensity_vec = sqrt(((motion_vec_bg(:,1)./src_win).^2 + ...
(motion_vec_bg(:,2)./src_win).^2 + ...
(motion_vec_bg(:,3)./src_win).^2 + ...
(motion_vec_bg(:,4)./src_win).^2) ...
./4.0);
sum_bg_mot_int = sum(bg_mot_intensity_vec);
egomotion_intensity = (sum(bg_mot_intensity_vec)/...
max_mot_points)^0.25;
std_egomot_intensity = std(bg_mot_intensity_vec);
% Compute motion position in relation with the screen midpoint
if sum_bg_mot_int>0
avg_motp_y = sum(bg_mot_intensity_vec.*motion_vec_bg(:,5))/...
sum_bg_mot_int;
std_motp_y = sqrt(sum(bg_mot_intensity_vec.*...
(motion_vec_bg(:,5)-avg_motp_y).^2)/...
sum_bg_mot_int);
avg_mot_pos_y = (avg_motp_y-height/2)/(height/2);
sprd_mot_pos_y = std_motp_y/height;
avg_motp_x = sum(bg_mot_intensity_vec.*motion_vec_bg(:,6))/...
sum_bg_mot_int;
std_motp_x = sqrt(sum(bg_mot_intensity_vec.*...
(motion_vec_bg(:,6)-avg_motp_x).^2)/...
sum_bg_mot_int);
avg_mot_pos_x = (avg_motp_x-width/2)/(width/2);
sprd_mot_pos_x = std_motp_x/width;
avg_egomot_pos = sqrt(avg_mot_pos_y^2+avg_mot_pos_x^2);
avg_egomot_sprd = sqrt(sprd_mot_pos_y^2+sprd_mot_pos_x^2);
% Average egomotion prediction improvement
mot_pred_acc_bw = mean(motion_vec_bg(:,7)-motion_vec_bg(:,8));
mot_pred_acc_fw = mean(motion_vec_bg(:,9)-motion_vec_bg(:,10));
egomot_pred_acc = 0.5*(mot_pred_acc_bw+mot_pred_acc_fw).^0.5;
mot_y_bg = mean(0.25.*(motion_vec_bg(:,1)+...
motion_vec_bg(:,3))./src_win+0.5);
mot_x_bg = mean(0.25.*(motion_vec_bg(:,2)+...
motion_vec_bg(:,4))./src_win+0.5);
end
end
mot_size = sum(sum(1-non_mot_area));
non_mot_size = sum(sum(non_mot_area));
% Simple colorfulness
cr = this_fr(:,:,1);
cg = this_fr(:,:,2);
cb = this_fr(:,:,3);
clrvec = max([cr(:)'; cb(:)'; cg(:)'])-min([cr(:)'; cb(:)'; cg(:)']);
clrvec = sort(clrvec(:),'descend');
colorfulness = mean(mean(clrvec(1:floor(0.1*end))));
static_area_flicker = 0;
static_area_flicker_std = 0;
if non_mot_size>0
% Sum of the pixel differences in the static area
static_area_flicker_bw = sum(non_mot_area(:) .* ...
abs(this_Y(:)-prev_Y(:)))/non_mot_size;
static_area_flicker_fw = sum(non_mot_area(:) .* ...
abs(this_Y(:)-next_Y(:)))/non_mot_size;
static_area_flicker = 0.5*(static_area_flicker_bw + ...
static_area_flicker_fw);
% Variance of pixel differences in the static area
st_diff_bw = abs(this_Y(:)-prev_Y(:));
st_diff_fw = abs(this_Y(:)-next_Y(:));
static_area_flicker_std = sum(non_mot_area(:)' .* ...
abs(max([st_diff_bw'; st_diff_fw']) - ...
static_area_flicker))/non_mot_size;
end
% Spatial activity in the static area
si = std2(sob_tot).^0.25;
%[blur glob_blockiness si]
% Temporal activity standard deviation in the static area
ti_prev = mean(abs(this_Y(:)-prev_Y(:)));
ti_next = mean(abs(this_Y(:)-next_Y(:)));
ti_mean = mean([ti_prev ti_next]).^0.25;
% Normalize static area size
mot_size = mot_size / (width*height);
% Create feature vector: first ten to be used for difference
features = [motion_intensity egomotion_density ...
egomotion_intensity std_mot_intensity ...
std_egomot_intensity avg_mot_pos ...
avg_mot_sprd avg_egomot_pos ...
avg_egomot_sprd mot_pred_acc ...
blur si ...
interlace motion_uncertainty ...
glob_blockiness jerkiness ...
jerk_cons ti_mean ...
mot_y mot_x ...
static_area_flicker static_area_flicker_std ...
mot_y_bg mot_x_bg ...
colorfulness egomot_pred_acc ...
motion_density mot_size ];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function computes the high complexity features
%
function features = compute_HC_features(image)
% Initializations
mono_image = image(:,:,1);
image = ycbcr2rgb(image);
lab_image = rgb2lab(image);
[height,width,depth] = size(image);
% Make Sobeled image
mask = zeros(height,width);
mask(2:end-1,2:end-1)=1;
H = [1 2 1; 0 0 0; -1 -2 -1]./8;
% Make Sobeled image in CIELAB color space
sob_image_lab_x = (imfilter(lab_image(:,:,1)./100.0,H).^2 + ...
imfilter(lab_image(:,:,2)./50.0,H).^2 + ...
imfilter(lab_image(:,:,3)./50.0,H).^2).*mask;
sob_image_lab_y = (imfilter(lab_image(:,:,1)./100.0,H').^2 + ...
imfilter(lab_image(:,:,2)./50.0,H').^2 + ...
imfilter(lab_image(:,:,3)./50.0,H').^2).*mask;
sob_image = sqrt(sob_image_lab_x+sob_image_lab_y);
% Compute fetures for different feature groups
[a,b,sat_image1] = compute_saturation(mono_image,1);
sat_bright = [a b];
[a,b,sat_image2] = compute_saturation(mono_image,0);
sat_dark = [a b];
sat_image = max(sat_image1, sat_image2);
saturation_ftr = [sat_bright sat_dark];
spatial_ftr = spatial_activity_features(sob_image, sat_image);
noisiness_ftr = noise_features(mono_image, sat_image, lab_image);
blockiness_ftr = blockiness_features(sob_image_lab_x.^0.5, ...
sob_image_lab_y.^0.5);
contrast_color_ftr = contrast_chroma_features(lab_image, sat_image);
dct_ftr = dct_features(mono_image);
sharpness_ftr = sharpness_features(sob_image);
% Make the HC feature vector
features = [spatial_ftr saturation_ftr ...
noisiness_ftr blockiness_ftr ...
contrast_color_ftr dct_ftr ...
sharpness_ftr];
end
% This function computes the saturation (bright or dark)
function [len,num,segs] = compute_saturation(image, is_bright)
[height,width] = size(image);
lens = [];
num = 0;
segs = zeros(height,width);
if (is_bright==1 && max(max(image))>0.9) || ...
(is_bright==0 && min(min(image))<0.1)
segs = seg_loop(image,segs,3,3,0.05, is_bright);
for i=1:max(max(segs))
len = length(find(segs==i));
if len<50
segs(find(segs==i))=0;
else
lens = [lens len];
num = num + 1;
end
end
segs(find(segs>0))=1;
end
len = sum(lens)/(width*height);
if num > 0
num = len / num;
end
end
% This function is used for segmentation by measure_saturation
function segim = seg_loop(image, segs, wh, ww, interval, is_bright)
[height,width] = size(image);
segim = segs;
maxi = max(max(image));
mini = min(min(image));
for i=1:height-wh+1
for j=1:width-ww+1
if (is_bright == 1 && ...
min(min(image(i:i+wh-1,j:j+ww-1)))>maxi-interval) || ...
(is_bright == 0 && ...
max(max(image(i:i+wh-1,j:j+ww-1)))<mini+interval)
maxsg = max(max(segim(i:i+wh-1,j:j+ww-1)));
if maxsg>0
segs_temp = reshape(segim(i:i+wh-1,j:j+ww-1),wh*ww,1);
minsg=min(segs_temp(find(segs_temp>0)));
segim(i:i+wh-1,j:j+ww-1)=minsg;
if minsg<maxsg
segim(find(segim==maxsg))=minsg;
end
else
segim(i:i+wh-1,j:j+ww-1)=max(max(segim(:,:)))+1;
end
end
end
end
end
% This function is used to compute noise related features
function out = noise_features(mono_image, sat_im, lab_image)
[height,width] = size(mono_image);
new_im = zeros(height, width, 3);
nonsat_pix = 0;
noise_pix = 0;
noise_int = [];
% Loop through pixels to find noise pixels
for i=5:height-4
for j=5:width-4
if sat_im(i,j)==0
surr_pix = mono_image(i-2:i+2,j-2:j+2);
surr_pix = surr_pix(:);
surr_pix = [surr_pix(1:12); surr_pix(14:25)];
if (mono_image(i,j)>max(surr_pix) || ...
mono_image(i,j)<min(surr_pix))
surr_pix = mono_image(i-4:i+4,j-4:j+4);
if std(surr_pix)<0.05
new_im(i,j,2) = 1;
pix_diff = sqrt( ...
(mean(lab_image(i-3:i+3,j-3:j+3,1))-...
lab_image(i,j,1)).^2 + ...
(mean(lab_image(i-3:i+3,j-3:j+3,2))-...
lab_image(i,j,2)).^2 + ...
(mean(lab_image(i-3:i+3,j-3:j+3,3))-...
lab_image(i,j,3)).^2);
noise_int = [noise_int pix_diff/100];
noise_pix = noise_pix + 1;
end
end
nonsat_pix = nonsat_pix + 1;
end
end
end
a = 0;
b = 0;
c = 0;
if nonsat_pix > 0 && noise_pix > 0
% noise density
a = noise_pix / nonsat_pix;
b = mean(noise_int);
c = std(noise_int);
end
out = [a b c];
end
% This function is used to compute spatial activity features
function out = spatial_activity_features(sobel_image, sat_image)
[height,width] = size(sobel_image);
sob_dists = zeros(1,height*width);
sob_dists2 = zeros(height*width,2);
sob_str = zeros(1,height*width);
sumstr = 0;
n = 0;
for i=1:height
for j=1:width
if sat_image(i,j)==0
if sobel_image(i,j)<0.01
sobel_image(i,j)=0;
end
sumstr = sumstr + sobel_image(i,j);
if sobel_image(i,j) > 0
n = n + 1;
sob_str(n) = sobel_image(i,j);
sob_dists(n) = sqrt((i/height-0.5)^2+(j/width-0.5)^2);
sob_dists2(n,1) = i/height-0.5;
sob_dists2(n,2) = j/width-0.5;
end
end
end
end
sob_str = sob_str(1:n);
sob_dists = sob_dists(1:n);
sob_dists2 = sob_dists2(1:n,:);
a = 0;
b = 0;
c = 0;
d = 0;
if ~isempty(sob_str)>0
a = mean(mean(sobel_image));
b = std2(sobel_image);
d = w_std(sob_dists, sob_str);
mean_y = sum(sob_str'.*sob_dists2(:,1))/sum(sob_str);
mean_x = sum(sob_str'.*sob_dists2(:,2))/sum(sob_str);
c = sqrt(mean_y^2+mean_x^2);
end
out = [a b c d];
end
% Function for "weighted standard deviation", used by function
% measure_spatial_activity
function res = w_std(input, weights)
wg_n = sum(weights);
wg_input = input.*weights;
wg_mean = mean(input.*weights);
res = sqrt(sum((wg_input-wg_mean).^2)/wg_n);
end
% This function is used to compute blockiness index
function blockiness = blockiness_features(sob_y, sob_x)
[height,width] = size(sob_y);
hor_tot = zeros(1,height-4);
ver_tot = zeros(1,width-4);
for i=3:height-2
hor_tot(i)=mean(sob_y(i,:)-sob_x(i,:));
end
for j=3:width-2
ver_tot(j)=mean(sob_x(:,j)-sob_y(:,j));
end
% compute autocorrelations
autocr_hor = zeros(1,23);
autocr_ver = zeros(1,23);
for i=0:22
autocr_hor(i+1) = sum(hor_tot(1:end-i).*hor_tot(1+i:end));
autocr_ver(i+1) = sum(ver_tot(1:end-i).*ver_tot(1+i:end));
end
% Find the highest local maximum (other than 0)
localpeaks = 0;
peakdist = 0;
max_hor = 0;
max_ver = 0;
min_hor = autocr_hor(1);
min_ver = autocr_ver(1);
max_hor_diff = 0;
max_ver_diff = 0;
for i=2:22
if autocr_hor(i)>max(autocr_hor(i-1),autocr_hor(i+1))
localpeaks = localpeaks+1/42;
end
if autocr_hor(i)<min(autocr_hor(i-1),autocr_hor(i+1)) && ...
autocr_hor(i)<min_hor
min_hor = autocr_hor(i);
elseif autocr_hor(i)>max(autocr_hor(i-1),autocr_hor(i+1)) && ...
autocr_hor(i)-min_hor>max_hor_diff
max_hor = autocr_hor(i);
max_hor_diff = max_hor-min_hor;
peakdist = (i-1)/21;
end
if autocr_ver(i)>max(autocr_ver(i-1),autocr_ver(i+1))
localpeaks = localpeaks + 1/42;
end
if autocr_ver(i)<min(autocr_ver(i-1),autocr_ver(i+1)) && ...
autocr_ver(i)<min_ver
min_ver = autocr_ver(i);
elseif autocr_ver(i)>max(autocr_ver(i-1),autocr_ver(i+1)) && ...
autocr_ver(i)-min_ver>max_ver_diff
max_ver = autocr_ver(i);
max_ver_diff = max_ver-min_ver;
peakdist = (i-1)/21;
end
end
a = 0;
if autocr_hor(1)>0 && autocr_ver(1)>0
if max_hor>0 && max_ver>0
a = max((max_hor_diff/autocr_hor(1)), ...
(max_ver_diff/autocr_ver(1)))^0.5;
elseif max_hor>0
a = (max_hor_diff/autocr_hor(1))^0.5;
elseif max_ver>0
a = (max_ver_diff/autocr_ver(1))^0.5;
end
end
b = peakdist;
c = localpeaks;
blockiness = [a b c];
end
% This function is used to compute contrast and chroma related features
function out = contrast_chroma_features(lab_image, sat_image)
a=0;
b=0;
c=0;
d=0;
[height,width,depth] = size(lab_image);
yuv_int = floor(lab_image(:,:,1));
%sat_image = sat_image(:);
yuv_int2 = yuv_int(sat_image(:)==0);
cumu_err = 0;
cumu_tar = 0;
if ~isempty(yuv_int2)
for i=0:100
cumu_tar = cumu_tar + 1/100;
cumu_err = cumu_err + (sum(yuv_int2<=i)/length(yuv_int2) - ...
cumu_tar)/100;
end
a = (cumu_err+1.0)/2.0;
b = 0.5*(1-cumu_err);
else
a = 1;
b = sum(sum(lab_image(:,:,1)))/50;
end
c = sqrt(mean(mean((lab_image(:,:,2)./50).^2 + ...
(lab_image(:,:,3)./50).^2)));
d = 0;
if std2(lab_image(:,:,1))>0
d = 0.01*(std2(lab_image(:,:,2))+std2(lab_image(:,:,3)));
end
out = [a b c d];
end
% This function is used to compute dct derived features
function out = dct_features(im)
% Input is monochrome image
[height,width] = size(im);
out_im = abs(dct2(im)).^.5;
area1 = imresize(out_im(1:floor(height/2),1:floor(width/2)),0.25);
area2 = imresize(out_im(1:floor(height/2),...
width:-1:width-floor(width/2)+1),0.25);
area3 = imresize(out_im(height:-1:height-floor(height/2)+1,...
1:floor(width/2)),0.25);
area4 = imresize(out_im(height:-1:height-floor(height/2)+1,...
width:-1:width-floor(width/2)+1),0.25);
a = max(0,max(corr(area1(:),area2(:)),corr(area1(:),area3(:))));
b = 0;
if mean(area1)>0
b = mean(area4)/mean(area1);
end
c = 0;
if max(mean(area2),mean(area3))>0
c = min(mean(area2),mean(area3))/max(mean(area2),mean(area3));
end
out = [a b c];