figure an matrix by image

Hellow, i have reconstruct an image of 16x16 pixels and all the pixels are in a vector of 256 elements P(256) , is it necessary to change P to a dimension P(16,16) to show it in an image. I thought it would but i tried it and it gives a error. Hope for a positive answer :)

 채택된 답변

Walter Roberson
Walter Roberson 2011년 5월 9일

0 개 추천

image(reshape(P,16,16))
What error are you getting?

댓글 수: 10

Fiboehh
Fiboehh 2011년 5월 21일
I tried this and the error is:
figure
image(reshape(X,16,16))
??? Error using ==> reshape
To RESHAPE the number of elements must not change.
Is it always necessary to reshape, because all the images i produce in my ART code have the form: I(x,1) where x is mostly 256,512 or 1024 and maybe you know there is a standard solution?
Matt Fig
Matt Fig 2011년 5월 21일
So show the output of this, placed right before the line that errors:
size(X)
Walter Roberson
Walter Roberson 2011년 5월 21일
If you have an array which is 512 or 1024, then that contradicts your initial claim that "all the pixels are in a vector of 256 elements P(256)"
If you _do_ have 512 or 1024 elements, then what outcome do you want? Still a 16 x 16 pixel outcome? If so then how do you want multiple input elements to be mapped in to the 16 x 16 output ?
Fiboehh
Fiboehh 2011년 5월 22일
No, in my program the user can choice how many pixels. And i must visualize the result. So if they select a 1024 image, i must image the vector with 1024 elements (thats a 32x32 pixel image). So i though that i must make it 32x32 to use it in image. Because image(vector) doesn't do anything...
Also when the user choice a 256(16x16) image, i also have to image it... so i need it in general.
I hope you understand it now :)
But already thank you very much!
Matt Fig
Matt Fig 2011년 5월 22일
L = sqrt(length(P));
if floor(L)~=L
error('Bad vector length.')
else
image(reshape(P,L,L))
end
Walter Roberson
Walter Roberson 2011년 5월 22일
The only thing I might do differently than Matt's answer is to use numel(P) instead of length(P)
Matt Fig
Matt Fig 2011년 5월 22일
@Walter, just out of curiosity why choose NUMEL over LENGTH?
Are you still suspicious that Fiboehh hasn't supplied a vector?
Walter Roberson
Walter Roberson 2011년 5월 22일
numel() covers the case where a non-vector was supplied but length() does not. numel() is as easy to write and covers more cases, so one might as well use it unless one explicitly wishes the code to error out in the case where a non-vector was supplied (and if so then why not test for that condition to make it obvious that one did not simply "overlook" that possibility ?)
That and I don't want to have another go around with someone saying, "But that failed for me" only to have it turn out that they had supplied a non-vector.
Matt Fig
Matt Fig 2011년 5월 22일
Exactly as I thought... Good enough!
Walter Roberson
Walter Roberson 2011년 5월 22일
Note that 512 does not have an integer square root. Fiboehh, what do you want to have happen if 512 is selected?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by