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 :)

 Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Mayo de 2011

0 votos

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

10 comentarios

Fiboehh
Fiboehh el 21 de Mayo de 2011
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 el 21 de Mayo de 2011
So show the output of this, placed right before the line that errors:
size(X)
Walter Roberson
Walter Roberson el 21 de Mayo de 2011
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 el 22 de Mayo de 2011
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 el 22 de Mayo de 2011
L = sqrt(length(P));
if floor(L)~=L
error('Bad vector length.')
else
image(reshape(P,L,L))
end
Walter Roberson
Walter Roberson el 22 de Mayo de 2011
The only thing I might do differently than Matt's answer is to use numel(P) instead of length(P)
Matt Fig
Matt Fig el 22 de Mayo de 2011
@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 el 22 de Mayo de 2011
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 el 22 de Mayo de 2011
Exactly as I thought... Good enough!
Walter Roberson
Walter Roberson el 22 de Mayo de 2011
Note that 512 does not have an integer square root. Fiboehh, what do you want to have happen if 512 is selected?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Images en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by