A question about different image size generation using Matlab

Hello everyone!
I need your help! Can anyone please tell me why, the Y1 becomes empty and its size changes when I input a true color image ?
X1 = double(imread('true_color_image'));
Y1 = padarray(X1(end-1:end,end-1:end),[1 1],'replicate','pre')
size(X1)
size(Y1)
Y1 =
0 0 0
0 0 0
0 0 0
ans =
120 120 3
ans =
3 3
While when I use input data, as shown below, Y1 does not change:
X2 = [1 2 3; 4 5 6; 7 8 9]
Y2 = padarray(X2(end-1:end,end-1:end),[1 1],'replicate','pre')
size(X2)
size(Y2)
X2=
1 2 3
4 5 6
7 8 9
Y2=
5 5 6
5 5 6
8 8 9
ans =
3 3
ans =
3 3
What can I do so that the size of Y1 does not change (i.e. remains same as X1) ?
Thank you!

5 件のコメント

Image Analyst
Image Analyst 2016 年 7 月 5 日
If X1 is really a true color image like the filename suggests, then why are you only indexing it with only 2 indexes instead of 3? Do you realize what that actually does when you do that (I'd guess not)?
Gobert
Gobert 2016 年 7 月 5 日
編集済み: Gobert 2016 年 7 月 5 日
What you have just suggested does not lead to any answers to my question.@ Image Analyst
X1 = double(imread('true_color_image'));
Y1 = padarray(X1(end-1:end,end-1:end,1:end),[1 1],'replicate','pre');
size(X1)
size(Y1)
ans =
120 120 3
ans =
3 3 3
Image Analyst
Image Analyst 2016 年 7 月 5 日
What does this say
[rows, columns, numberOfColorChannels] = size(X1)
Gobert
Gobert 2016 年 7 月 5 日
編集済み: Gobert 2016 年 7 月 5 日
@ Image Analyst . It simply says this:
X1 = double(imread('true_color_image'));
[rows, columns, numberOfColorChannels] = size(X1)
rows =
120
columns =
120
numberOfColorChannels =
3
Image Analyst
Image Analyst 2016 年 7 月 5 日
Looks like you figured it out now since you've accepted an answer.

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

 採用された回答

Guillaume
Guillaume 2016 年 7 月 5 日

0 投票

X1(end-1:end,end-1:end)
Since you're using 2D indexing to index a 3D matrix, this only return values from the 1st plane (the red colour plane). This is equivalent to
X1(end-1:end,end-1:end, 1)
If you want a 3D matrix as output, you must pass a 3D matrix as input, so:
Y1 = padarray(X1(end-1:end,end-1:end, :), [1 1], 'replicate', 'pre')
Note that the above will work on greyscale images as wel, since the : will match the only colour plane.

3 件のコメント

Gobert
Gobert 2016 年 7 月 5 日
編集済み: Gobert 2016 年 7 月 5 日
@ Guillaume , even if I consider the 3rd dimension, I still don't get the expected Y1 size - and, that is my question, I would like to know why or what I can do to get it fixed: See this example with your correction:
X1 = double(imread('true_color_image'));
Y1 = padarray(X1(end-1:end,end-1:end, :), [1 1], 'replicate', 'pre');
size(X1)
size(Y1)
ans =
120 120 3
ans =
3 3 3
Guillaume
Guillaume 2016 年 7 月 5 日
Well, end-1:end only select the last 2 columns/rows. So padding it by 1 is always going to give you 3 columns/rows.
Matlab is doing exactly what you're asking. What you're asking is obviously not what you really mean. You haven't say what it is though.
If you want to pad the whole array:
Y1 = padarray(X1, [1 1], 'replicate', 'pre');
Gobert
Gobert 2016 年 7 月 5 日
編集済み: Gobert 2016 年 7 月 5 日
Thank you very much, Guillaume .
I got it:
X1 = double(imread('true_color_image'));
Y1 = padarray(X1(2:end,2:end,:), [1 1], 'replicate', 'pre');
size(X1)
size(Y1)
ans =
120 120 3
ans =
120 120 3

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2016 年 7 月 5 日

コメント済み:

2016 年 7 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by