How i can convert any type of image to ASCII values and store these values into one-dimensional array?
5 ビュー (過去 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.
0 件のコメント
採用された回答
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
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
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
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.
参考
カテゴリ
Help Center および File Exchange で Convert Image Type についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!