MATLAB编码问题

发布于 2024-09-27 05:11:36 字数 1436 浏览 2 评论 0原文

大家好,当我尝试触发下面的功能时,我收到了此错误消息。有人可以帮我吗?谢谢!

>> changeYuv('tilt.yuv',352,288,1:40,40);
??? Index exceeds matrix dimensions.
    Error in ==> changeYuv at 32
            j=histogram(imgYuv(:,:,1,k+1));

>> [x,y,z,a]=size(imgYuv)
x =
   288
y =
   352
z =
     3
a =
    40

源代码:

function [imgYuv, S]= changeYuv(fileName, width, height, idxFrame, nFrames) 
% load RGB movie [0, 255] from YUV 4:2:0 file

fileId = fopen(fileName, 'r');

subSampleMat = [1, 1; 1, 1];
nrFrame = length(idxFrame);

for f = 1 : 1 : nrFrame
    % search fileId position
    sizeFrame = 1.5 * width * height;
    fseek(fileId, (idxFrame(f) - 1) * sizeFrame, 'bof');

    % read Y component
    buf = fread(fileId, width * height, 'uchar');
    imgYuv(:, :, 1,f) = reshape(buf, width, height).';

    % read U component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    imgYuv(:, :, 2,f) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample

    % read V component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    imgYuv(:, :, 3,f) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample

    %histogram difference of Y component
    for k=1:(nFrames-1)
        h=histogram(imgYuv(:,:,1,k));
        j=histogram(imgYuv(:,:,1,k+1));
        X=abs(h-j)/256;
        S(k)=sum(X);
    end
end
fclose(fileId);

Hey guys, I got this error message when I tried to trigger the function below. Can anybody help me out? Thanks!

>> changeYuv('tilt.yuv',352,288,1:40,40);
??? Index exceeds matrix dimensions.
    Error in ==> changeYuv at 32
            j=histogram(imgYuv(:,:,1,k+1));

>> [x,y,z,a]=size(imgYuv)
x =
   288
y =
   352
z =
     3
a =
    40

The source code:

function [imgYuv, S]= changeYuv(fileName, width, height, idxFrame, nFrames) 
% load RGB movie [0, 255] from YUV 4:2:0 file

fileId = fopen(fileName, 'r');

subSampleMat = [1, 1; 1, 1];
nrFrame = length(idxFrame);

for f = 1 : 1 : nrFrame
    % search fileId position
    sizeFrame = 1.5 * width * height;
    fseek(fileId, (idxFrame(f) - 1) * sizeFrame, 'bof');

    % read Y component
    buf = fread(fileId, width * height, 'uchar');
    imgYuv(:, :, 1,f) = reshape(buf, width, height).';

    % read U component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    imgYuv(:, :, 2,f) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample

    % read V component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    imgYuv(:, :, 3,f) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample

    %histogram difference of Y component
    for k=1:(nFrames-1)
        h=histogram(imgYuv(:,:,1,k));
        j=histogram(imgYuv(:,:,1,k+1));
        X=abs(h-j)/256;
        S(k)=sum(X);
    end
end
fclose(fileId);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放我走吧 2024-10-04 05:11:36

在外循环的每次迭代中,您似乎都会在第四维中从空开始将 imgYuv 增长 1。但您的内部循环始终从 1 循环到 nFrames-1。因此,在我看来,您正在尝试访问超出 imgYuv 范围的内容。

与此无关的一点是,像这样增长数组通常非常慢。最好在开始之前初始化 imgYuv,即 imgYuv = Zeros([height,width,3,nFrames])

On every iteration of the outer loop, you appear to be growing imgYuv by one in the 4th dimension, starting from empty. But your inner loop always loops from 1 to nFrames-1. Therefore, it would seem to me like you're trying to access beyond the extent of imgYuv.

On an unrelated note, growing an array like this is typically very slow. You're much better off initialising imgYuv before you start, i.e. imgYuv = zeros([height,width,3,nFrames]).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文