【问题标题】:Error in the function reshape() in matlabmatlab中函数reshape()出错
【发布时间】:2015-02-07 04:33:57
【问题描述】:

我有以下代码来读取 CIF 序列中的 Y 分量,这会引发此错误。

使用整形出错 要 RESHAPE,元素的数量不能改变。

foremanOne 中的错误(第 12 行) img_y = reshape(img_y, nColumn, nRow);

代码是

            clc;
            file = 'foreman.cif';
            nFrame = 10;
            [fid,message]= fopen(file,'rb');
            nRow = 288;
            nColumn = 352;

            for i = 1: nFrame
                %reading Y component 
                img_y = fread(fid, nRow * nColumn, 'uchar');
                img_y = reshape(img_y, nColumn, nRow);
                img_y = img_y';
                imshow(uint8(img_y));
            end

            fclose(fid);
            disp('OK');

可能出了什么问题?

【问题讨论】:

    标签: matlab video-processing


    【解决方案1】:

    在您的循环中,您不使用i,因此看起来您使用fread 打开的数组img_y 的尺寸为[288, 352, 10],而在重塑时您只提供图像的高度和宽度。 因此,我认为您只需要使用循环索引来索引img_y(我从 i 更改为 k ... i 不是一个好主意,因为它是虚数单位),如下所示:

               clc;
                file = 'foreman.cif';
                nFrame = 10;
                [fid,message]= fopen(file,'rb');
                nRow = 288;
                nColumn = 352;
    
             %// Use fread once outside the loop and convert right away everything to uint8.
              img_y = uint8(fread(fid, nRow * nColumn, 'uchar'));
    
                for k = 1:nFrame
                    %// reading Y component. I changed the name to avoid confusion
                    ImY = reshape(img_y(:,:,k), nColumn, nRow); %// Use index here
                    ImY = ImY';
                    imshow(ImY);
    
                pause(0.5) %// You might want to pause to see each image individually
                end
    
                fclose(fid);
                disp('OK');
    

    我还进行了一些其他更改以使代码更高效。

    希望有帮助!

    【讨论】:

    • 感谢您的接受!我刚刚注意到ImY 被重新调整为具有[nColumn,nRow] 的尺寸。由于在 MATLAB 中,行在索引期间排在第一位,因此您可能希望交换它们。
    • 我尝试执行代码,但错误依旧!!
    • 错误使用重塑要重塑元素的数量不能改变。 foremanOne 中的错误(第 31 行)ImY = reshape(img_y(:,:,k), nColumn, nRow);
    • 请提供更多信息,因为这里很难猜到。 “文件”的大小是多少?请在调用fopen 之后显示调用whos 的输出。谢谢
    • 名称大小字节类属性fid 1x1 8 双文件1x11 22 字符消息0x0 0 字符nFrame 1x1 8 双错误使用' ND 数组上的转置未定义。 foremanOne 中的错误(第 14 行)ImY = ImY';
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多