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

10 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2022-12-4
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 个评论
Image Analyst
Image Analyst 2022-12-5
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]);

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2022-12-4
编辑:Walter Roberson 2022-12-4
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 个评论
Walter Roberson
Walter Roberson 2022-12-4
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.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by