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

조회 수: 18 (최근 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
Radhwan Jawad
Radhwan Jawad 2022년 12월 4일
And thank you very much for the answer
but, how i can reconstruct the original image?
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