Index in position 2 exceeds array bounds. (reason for error)

img=imread('D:/lena1.png');
[height, width]=size(img);
for a=1:8:height
disp(height);
for b=1:8:width
imgdct_8(a:a+7,b:b+7)=dct2(img(a:a+7,b:b+7));
end
end
for i=1:8
for j=1:8
qtz_mtrx(i,j)=1+((i+j)* 15);
end
end

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 4 月 15 日

0 投票

[height, width]=size(img);
You are probably thinking that code is the same as
temp = size(img) ;
height = temp(1);
width = temp(2);
But it is not the same. Instead it means
temp = size(img);
height = temp(1);
width = prod(temp(2:end));
This matters because lena1.png is a color image, so your width ends up being 3 times what you expect.

2 件のコメント

lakshmi amruthavalli P
lakshmi amruthavalli P 2022 年 4 月 15 日
thank you so much
lakshmi amruthavalli P
lakshmi amruthavalli P 2022 年 4 月 15 日
what change should i do in the code... suggest me

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by