Error using reshape Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.

4 visualizzazioni (ultimi 30 giorni)
I am trying to encode a video to bits then add probability of error then decoding it again but when I am decoding this error shows up while reversing the packets' step
This is the full code
clc
close all
clear all
obj=VideoReader('Stephan.avi');
Error using VideoReader/initReader
The filename specified was not found in the MATLAB path.

Error in audiovideo.internal.IVideoReader (line 136)
initReader(obj, fileName, currentTime);

Error in VideoReader (line 104)
obj@audiovideo.internal.IVideoReader(varargin{:});
a=read(obj);
frames=get(obj,'NumberOfFrames');
for i=1:frames
I(i).cdata=a(:,:,:,i);
end
s=size(I(1).cdata);
mov(1:frames) =struct('cdata', zeros(s(1),s(2), 3, 'uint8'),'colormap', []);
for Frame=1:296
%Red Components of the Frame
R=I(Frame).cdata(:,:,1);
%Green Components of the Frame
G=I(Frame).cdata(:,:,2);
%Blue Components of the Frame
B=I(Frame).cdata(:,:,3);
end
Rdouble = double(R);
Gdouble = double(G);
Bdouble = double(B);
Rbin = de2bi(Rdouble);
Gbin = de2bi(Gdouble);
Bbin = de2bi(Bdouble);
Rr=reshape(Rbin,1,prod(size(Rbin))); %we reshape to put the red as a stream of 1 row
Gr=reshape(Gbin,1,prod(size(Gbin))); %we reshape to put the green as a stream of 1 row
Br=reshape(Bbin,1,prod(size(Bbin))); %we reshape to put the blue as a stream of 1 row
pcksize=1024;
noofpckts= 101376*8/pcksize;
Rpck= reshape(Rbin,[noofpckts,pcksize]);
Gpck= reshape(Gbin,[noofpckts,pcksize]);
Bpck= reshape(Bbin,[noofpckts,pcksize]);
%trellis = poly2trellis(ConstraintLength,CodeGenerator)
trellis = poly2trellis(7,[135 135 147 163]);
for i=1:noofpckts
Rcoded= convenc(Rpck(i,:),trellis);
Gcoded= convenc(Gpck(i,:),trellis);
Bcoded= convenc(Bpck(i,:),trellis);
a = 0.0001;
b = 0.2;
p = rand(1, 1) .* (b-a) + a;
Rerror= bsc(Rcoded,p);
Gerror= bsc(Gcoded,p);
Berror= bsc(Bcoded,p);
tbdepth = 34;
Rdecoded = vitdec(Rerror,trellis,tbdepth,'trunc','hard');
Gdecoded = vitdec(Gerror,trellis,tbdepth,'trunc','hard');
Bdecoded = vitdec(Berror,trellis,tbdepth,'trunc','hard');
Rnpck= reshape(Rdecoded,[pcksize,noofpckts]);
Gnpck= reshape(Gdecoded,[pcksize,noofpckts]);
Bnpck= reshape(Bdecoded,[pcksize,noofpckts]);
Rr=reshape(Rnpck,8,prod(size(Rnpck)));
Gr=reshape(Gnpck,8,prod(size(Gnpck)));
Br=reshape(Bnpck,8,prod(size(Bnpck)))
Rn = bin2dec( Rr );
Gn= bin2dec( Gr );
Bn= bin2dec( Br );
Rd.ToDouble();
Gd.ToDouble();
Bd.ToDouble();
end
for i=1:296
mov(1,i).cdata(:,:,1) = Rd;
mov(1,i).cdata(:,:,2) = Gd;
mov(1,i).cdata(:,:,3) = Bd;
end
%writing the video
video=VideoWriter('channnelzft.avi','Uncompressed AVI');
open(video);
writeVideo(video,mov);
close(video);

Risposte (2)

Walter Roberson
Walter Roberson il 23 Mag 2023
Rbin = de2bi(Rdouble);
is equivalent to
Rbin = de2bi(Rdouble(:));
The size of the output will be numel(Rdouble) by max(ceil(log2(Rdouble+1), [], 'all') -- where the right side of that expression is the minimum number of bits needed to represent the largest value in binary. Which will not necessarily be 8 bits per entry, depending on the values -- for example if the red component for an image were all 0, then only 1 column of bits would be needed for Rbin
Rr=reshape(Rbin,1,prod(size(Rbin)))
You could code that more easily as Rr = reshape(Rbin, 1, []);
pcksize=1024;
noofpckts= 101376*8/pcksize;
Rpck= reshape(Rbin,[noofpckts,pcksize]);
That is assuming a fixed array size for the video, and is assuming that R, G, and B are all using the full 8 bits per component.
  1 Commento
Walter Roberson
Walter Roberson il 24 Mag 2023
Rdouble = [0 1 63]
Rdouble = 1×3
0 1 63
Gdouble = [83 91 126]
Gdouble = 1×3
83 91 126
Bdouble = [0 240 240]
Bdouble = 1×3
0 240 240
de2bi(Rdouble)
ans = 3×6
0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 1 1
de2bi(Gdouble)
ans = 3×7
1 1 0 0 1 0 1 1 1 0 1 1 0 1 0 1 1 1 1 1 1
de2bi(Bdouble)
ans = 3×8
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
Notice these have different numbers of columns depending on the largest value being represented.
Your existing code not only assumes a consistent number of columns are being produced: it also assumes that the image always has exactly the same number of elements, such as if it is always 288 by 352 .
You need to rewrite your code to ensure that de2bi() always returns the same number of columns, and you need to rewrite your code to account for different array sizes -- including accounting for the possibility that a full image might not happen to contain an integer number of packets.

Accedi per commentare.


Cris LaPierre
Cris LaPierre il 23 Mag 2023
The issue appears to be that you do not have the number of elements in your array as you think you do.
Your code was written expecting there to be 1024x792 elements. Not that R, G & B only have the cdata from the last frame.
A = 1:6;
% use [] to autosize the result
B = reshape(A,2,[])
B = 2×3
1 3 5 2 4 6
% your error
C = reshape(A,2,2)
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.
  4 Commenti
habiba
habiba il 23 Mag 2023
Thank you! bi2de() function works but I found that in the loop of frames only the last frame is visible afterwards as it was overwritten in each iteration by the new values so I tried putting the new values in a matrix so it won't be lost in the process.
Cris LaPierre
Cris LaPierre il 23 Mag 2023
The code you shared is still only capturing the final frame.
for frame = 1:5
R = frame;
end
R
R = 5
% Compare to this code
for frame = 1:5
R2(frame) = frame;
end
R2
R2 = 1×5
1 2 3 4 5

Accedi per commentare.

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by