error in block processing in Matlab - matlab

I am trying to apply dct and quantization to 8*8 blocks like this.
img = imread('frames/frm001.png');
ycbcr = rgb2ycbcr(img);
y = ycbcr(:,:,1);
func = #dct2;
whos func;
d1 = blkproc(y,[8 8],func);
qmtx = [16 11 10 16 24 40 51 61;
12 12 14 19 26 58 60 55;
14 13 16 24 40 57 69 56;
14 17 22 29 51 87 80 62;
18 22 37 56 68 109 103 77;
24 35 55 64 81 104 113 92;
49 64 78 87 103 121 120 101;
72 92 95 98 112 100 103 99];
d1 = ceil(d1);
whos d1;
c = #(block_struct) (block_struct.data) ./ qmtx;
d2 = blkproc(d1,[8,8], c );
whos d2;
For this I'm getting the following error
Attempt to reference field of non-structure array.
Error in #(block_struct)(block_struct.data)./qmtx
Error in blkproc (line 103)
firstBlock = feval(fun,x,params{:});
Error in dct (line 28)
d2 = blkproc(d1,[8,8], c );
What might have gone wrong..?

Related

How to sum columns of a matrix for a specified number of columns?

I have a matrix A of size 2500 x 500. I want to sum each 10 columns and get the result as a matrix B of size 2500 x 50. That is, the first column of B is the sum of the first 10 columns of A, the second column of B is the sum of second 10 columns of A, and so on.
How can I do that without a for loop? Since I have to do that hundreds of times and it is highly time consuming to do that using for loop.
First, we "block reshape" A, such that we have the desired number of columns. Therefore, we shamelessly steal the code from the great Divakar, and put in some minimal effort to generalize it. Then, we just need to sum along the second axis, and reshape to the original form.
Here's an example with five columns to be summed:
% Sample input data
A = reshape(1:100, 10, 10).'
[r, c] = size(A);
% Number of columns to be summed
n_cols = 5;
% Block reshape to n_cols, see https://stackoverflow.com/a/40508999/11089932
B = reshape(permute(reshape(A, r, n_cols, []), [1, 3, 2]), [], n_cols);
% Sum along second axis
B = sum(B, 2);
% Reshape to original form
B = reshape(B, r, c / n_cols)
That's the output:
A =
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
B =
15 40
65 90
115 140
165 190
215 240
265 290
315 340
365 390
415 440
465 490
Hope that helps!
This can be done with splitapply. An advantage of this approach is that it works even if the group size does not divide the number of columns (the last group is smaller):
A = reshape(1:120, 12, 10).'; % example 10×12 data (borrowed from HansHirse)
n_cols = 5; % number of columns to sum over
result = splitapply(#(x)sum(x,2), A, ceil((1:size(A,2))/n_cols));
In this example,
A =
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
result =
15 40 23
75 100 47
135 160 71
195 220 95
255 280 119
315 340 143
375 400 167
435 460 191
495 520 215
555 580 239

Line break other than \n in MATLAB

The following b variable seems to have a last character corresponding to a line break but it is not the classical '\n' character. This is very embarrassing for print using this variable:
K>> b
b =
toto_titi
K>> fprintf('This strange variable (%s) is very strange !!!\n', b);
This strange variable (toto_titi
) is very strange !!!
Indeed, the needed print would be:
This strange variable (toto_titi) is very strange !!!
Below, few observed facts with this variable:
K>> class(b)
ans =
char
K>> [b 'end']
ans =
toto_titi
end
K>> b(end)
ans =
K>> regexp(b,'_', 'split')
ans =
'toto' 'titi…'
I confess that I do not exactely understand for the three dots in 'titi…' but I suppose that here is a part of the explanation !!!
A quick and dirty method will be to remove the last invisible character that cause the line break but if in other application the last caharacter in the b variable is a "real" character we will lost this last character (ex. toto_tit in place of toto_titi).
A MATLAB equivalent of the python strip() exist ?
What is this invisible character in the b variable ?
EDIT 1:
A good idea, from rahnema1, is to determine what is the ASCII code for this invisible character:
K>> double(b)
ans =
116 111 116 111 95 116 105 116 105 13
K>> for i = 1 : 60
str = [num2str(i) ' ' char(i) ' '...
num2str(i+32) ' ' char(i+32) ' '...
num2str(i+64) ' ' char(i+64)];
disp(str)
end
1 33 ! 65 A
2 34 " 66 B
3 35 # 67 C
4 36 $ 68 D
5 37 % 69 E
6 38 & 70 F
7 39 ' 71 G
8 40 ( 72 H
9 41 ) 73 I
10
42 * 74 J
11 43 + 75 K
12 44 , 76 L
13
45 - 77 M
14 46 . 78 N
15 47 / 79 O
16 48 0 80 P
17 49 1 81 Q
18 50 2 82 R
19 51 3 83 S
20 52 4 84 T
21 53 5 85 U
22 54 6 86 V
23 55 7 87 W
24 56 8 88 X
25 57 9 89 Y
26 58 : 90 Z
27 59 ; 91 [
28 60 < 92 \
29 61 = 93 ]
30 62 > 94 ^
31 63 ? 95 _
32 64 # 96 `
33 ! 65 A 97 a
34 " 66 B 98 b
35 # 67 C 99 c
36 $ 68 D 100 d
37 % 69 E 101 e
38 & 70 F 102 f
39 ' 71 G 103 g
40 ( 72 H 104 h
41 ) 73 I 105 i
42 * 74 J 106 j
43 + 75 K 107 k
44 , 76 L 108 l
45 - 77 M 109 m
46 . 78 N 110 n
47 / 79 O 111 o
48 0 80 P 112 p
49 1 81 Q 113 q
50 2 82 R 114 r
51 3 83 S 115 s
52 4 84 T 116 t
53 5 85 U 117 u
54 6 86 V 118 v
55 7 87 W 119 w
56 8 88 X 120 x
57 9 89 Y 121 y
58 : 90 Z 122 z
59 ; 91 [ 123 {
60 < 92 \ 124 |
EDIT 2:
Works around the idea of rahnema1 and excaza:
. If we know the ASCII code (by using strrep):
K>> fprintf('This strange variable (%s) is very stange !!!\n', strrep(b,char(13),''));
This strange variable (toto_titi) is very stange !!!
. For remove leading and trailing whitespace (by using strtrim):
fprintf('This strange variable (%s) is very stange !!!\n', strtrim(b));
This strange variable (toto_titi) is very stange !!!

Matlab lego nxt robot doesn’t turn but says it did

I am programming a Lego brick NXT in Matlab to go through a maze. The robot is supposed to drive forward until the touch sensor is activated by a wall.
The robot should
stop
backup
stop
read the ultraSonic (distance) sensor
display the result
If the sensor reads a large distance, the robot turns right and stops and if the sensor reads a small distance, the robot turns left and stops.
Then, the robot continues forward again until it runs into another wall.
Current code:
The robot will hit the wall, stop, back up, stop, and it will display that it’s turning, but it doesn’t turn.
Is there something I am typing incorrectly? Should I initialize the motors before the loop or inside of the loop? I am especially wary of my pauses.
1 %B = NXTMotor('B');
2 %C = NXTMotor('C');
3 OpenSwitch(SENSOR_1);
4 OpenUltrasonic(SENSOR_4);
5
6
7 while 1
8
9 B = NXTMotor('B');
10 C = NXTMotor('C');
11
12 %FORWARD 1
13 disp('move forward');
14 B.Power = 50;
15 C.Power = 50;
16
17 B.SendToNXT();
18 C.SendToNXT();
19 B.WaitFor();
20 C.WaitFor();
21 disp('before getswitch');
22
23 if GetSwitch(SENSOR_1)
24 pause(0.2);
25 B.Power = 0;
26 C.Power = 0;
27 B.SendToNXT();
28 C.SendToNXT();
29 B.WaitFor();
30 C.WaitFor();
31 disp('Hit a wall and stops');
32
33 disp('back up');
34 B.Power = -50;
35 C.Power = -50;
36
37 B.TachoLimit = 720;
38 C.TachoLimit = 720;
39
40 B.SendToNXT();
41 C.SendToNXT();
42 B.WaitFor();
43 C.WaitFor();
44
45 pause(0.5);
46 B.Power = 0;
47 C.Power = 0;
48 B.SendToNXT();
49 C.SendToNXT();
50 B.WaitFor();
51 C.WaitFor();
52 disp('finished back up');
53
54 %ultrasonic
55 distance = GetUltrasonic(SENSOR_4);
56 disp(distance);
57
58 if distance >100
59 %if GetUltrasonic(SENSOR_4) > 100
60
61 %TURN 1
62 disp('turn1 > 100');
63
64 B.Power = 0;
65 C.Power = 100;
66
67 B.TachoLimit = 720;
68 C.TachoLimit = 720;
69
70 B.SendToNXT();
71 C.SendToNXT();
72 B.WaitFor();
73 C.WaitFor();
74 disp('turn1 finished');
75
76 pause(0.2);
77 B.Power = 0;
78 C.Power = 0;
79 B.SendToNXT();
80 C.SendToNXT();
81 B.WaitFor();
82 C.WaitFor();
83
84 end
85
86 if distance <= 100
87
88 disp('turn2 < 100');
89
90 B.Power = 100;
91 C.Power = 0;
92 disp('turn 2');
93 B.TachoLimit = 720;
94 C.TachoLimit = 720;
95
96 B.SendToNXT();
97 C.SendToNXT();
98 B.WaitFor();
99 C.WaitFor();
100 disp('Finished turn 2');
101
102 pause(0.5);
103 B.Power = 0;
104 C.Power = 0;
105 B.SendToNXT();
106 C.SendToNXT();
107 B.WaitFor();
108 C.WaitFor();
109
110 disp('stopped turn2 and ready to go forward');
111
112 end
113
114 B.Stop('off');
115 C.Stop('off');
116 end
117
118 %B.Stop('off');
119 %C.Stop('off');
120 end
121
122 CloseSensor(SENSOR_4);
123 CloseSensor(SENSOR_1);

How to reshape a matrix horizontally using MATLAB

I have matrix A of the size(4,192). It consists of 12 matrices of the size(4,4) aligned horizontally. I want to get matrix B with the size(12,16). B must get as follows:
suppose
A=[y1,y2,y3,...,y12]
in which yn is a 4*4 matrix. Then,
B=[y1,y4,y7,y10;
y2,y5,y8,y11;
y3,y6,y9,y12]
Is there an efficient/quicker (using no loop) way to do this using MATLAB?
You can try the following code:
ys1 = 2; % size(1) from submatrix (for the following example, use ys1 = 4 for the actual problem)
ys2 = 2; % size(2) from submatrix (for the following example, use ys2 = 4 for the actual problem)
ns1 = 3; % size(1) of final matrix in terms of submatrix (3 rows)
ns2 = 4; % size(2) of final matrix in terms of submatrix (4 columns)
temp = reshape(A,ys1,ys2,ns1,ns2);
B = reshape(permute(temp,[1 3 2 4]),ys1*ns1,ys2*ns2);
Example:
A = [11 12 21 22 31 32 41 42 51 52 61 62 71 72 81 82 91 92 101 102 111 112 121 122;
13 14 23 24 33 34 43 44 53 54 63 64 73 74 83 84 93 94 103 104 113 114 123 124];
B =
11 12 41 42 71 72 101 102
13 14 43 44 73 74 103 104
21 22 51 52 81 82 111 112
23 24 53 54 83 84 113 114
31 32 61 62 91 92 121 122
33 34 63 64 93 94 123 124

Sliding window algorithm for activity recognition

I want to write a sliding window algorithm for use in activity recognition.
The training data is <1xN> so I'm thinking I just need to take (say window_size=3) the window_size of data and train that. I also later want to use this algorithm on a matrix
.
I'm new to matlab so i need any advice/directions on how to implement this correctly.
The short answer:
%# nx = length(x)
%# nwind = window_size
idx = bsxfun(#plus, (1:nwind)', 1+(0:(fix(nx/nwind)-1))*nwind)-1;
idx will be a matrix of size nwind-by-K where K is the number of sliding windows (ie each column contains the indices of one sliding window).
Note that in the code above, if the last window's length is less than the desired one, it is dropped. Also the sliding windows are non-overlapping.
An example to illustrate:
%# lets create a sin signal
t = linspace(0,1,200);
x = sin(2*pi*5*t);
%# compute indices
nx = length(x);
nwind = 8;
idx = bsxfun(#plus, (1:nwind)', 1+(0:(fix(nx/nwind)-1))*nwind)-1;
%'# loop over sliding windows
for k=1:size(idx,2)
slidingWindow = x( idx(:,k) );
%# do something with it ..
end
%# or more concisely as
slidingWindows = x(idx);
EDIT:
For overlapping windows, let:
noverlap = number of overlapping elements
then the above is simply changed to:
idx = bsxfun(#plus, (1:nwind)', 1+(0:(fix((nx-noverlap)/(nwind-noverlap))-1))*(nwind-noverlap))-1;
An example to show the result:
>> nx = 100; nwind = 10; noverlap = 2;
>> idx = bsxfun(#plus, (1:nwind)', 1+(0:(fix((nx-noverlap)/(nwind-noverlap))-1))*(nwind-noverlap))-1
idx =
1 9 17 25 33 41 49 57 65 73 81 89
2 10 18 26 34 42 50 58 66 74 82 90
3 11 19 27 35 43 51 59 67 75 83 91
4 12 20 28 36 44 52 60 68 76 84 92
5 13 21 29 37 45 53 61 69 77 85 93
6 14 22 30 38 46 54 62 70 78 86 94
7 15 23 31 39 47 55 63 71 79 87 95
8 16 24 32 40 48 56 64 72 80 88 96
9 17 25 33 41 49 57 65 73 81 89 97
10 18 26 34 42 50 58 66 74 82 90 98