2015-02-07 47 views
0

我有以下代碼來讀取CIF序列中的Y組件,這是拋出此錯誤。錯誤在函數重塑()在matlab

使用重整的錯誤 要重新設置元素的數量不得更改。

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'); 

什麼可能出現了問題?

回答

0

在你的循環,你不使用i所以看起來你fread打開陣列img_y是尺寸[288, 352, 10]而當你重塑你只提供圖像的高度和寬度。 因此,我認爲你只需要與循環指數指數img_y(我改變從我到k ...我不是一個好主意,因爲它的虛數單位),像這樣:

  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'); 

我做了一些其他更改以及使代碼更有效一些。

希望有幫助!

+0

感謝您的接受!我只注意到'ImY'被重新塑造成尺寸爲[nColumn,nRow]。由於在MATLAB中,行在索引過程中首先出現,所以您可能需要交換它們。 – 2015-02-07 05:26:21

+0

我試圖執行代碼,但錯誤仍然是相同的! – Prashanth 2015-02-08 06:06:30

+0

使用重整的錯誤 重新設置元素的數量不得更改。 foremanOne中的錯誤(第31行) ImY = reshape(img_y(:,:,k),nColumn,nRow); – Prashanth 2015-02-08 06:06:52