How i can convert any type of image to ASCII values and store these values into one-dimensional array?

13 vues (au cours des 30 derniers jours)
Hello matlab community.
I want to convert any type of image ( color or gray scale image ) to ASCII values and store these values into one-dimensional array for ex., if read my image as: A = imread(filename);, i want the final results(ASCII values ) appears in one-dimensional array(ex: B=[150,146,....), so that later on, I can reconstruct the original image.

Réponse acceptée

Image Analyst
Image Analyst le 4 Déc 2022
I don't understand why you want to do that. If you want the original image later, just make a copy of A before you alter it.
You can make a 1-D list B from A like this
B = A(:)
Not sure what you're going to do with that though if A is a color image. If you have a gray scale image it's just column 1, then column 2, then column 3, etc. until the last column of the image, but I'm not sure why you'd want this.
If you want to make an ASCII text file with the location and color value in a CSV file, see the attached demo.
  6 commentaires
Image Analyst
Image Analyst le 5 Déc 2022
SIZE_OF_IMAGE = size(YourImage);
where you of course replace "YourImage" with the actual name you used for your image variable.
Or you could do this:
[rows, columns, numberOfColorChannels] = size(YourImage)
output = reshape( permute(YourImage, [3 1 2]), 1, []);
reconstructed = permute(reshape(output, numberOfColorChannels, rows, columns), [2 3 1]);

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 4 Déc 2022
Modifié(e) : Walter Roberson le 4 Déc 2022
N = ndims(YourImage);
indices = cell(1,N);
idx = find(YourImage);
[indices{:}] = ind2sub(size(YourImage), idx);
values = YourImage(idx);
T = [values(:), indices{:}];
Now T is an array with multiple columns. The first column is a pixel value; the remaining columns are indices -- row, column at the very least; if the 4th column is present it is channel number.
This code does not record the location of any pixels that are zero.
  2 commentaires
Radhwan Jawad
Radhwan Jawad le 4 Déc 2022
And thank you very much for the answer
but, how i can reconstruct the original image?
Walter Roberson
Walter Roberson le 4 Déc 2022
reconstructed = accumarray(T(:,2:end), T(:,1));
This will not work exactly as desired if it happens that there are rows or columns along the right edge or top edge that are all black: for that case you would need to know what the original size of the image is.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by