将ASCII值转换为图像

时间:2014-11-26 03:33:37

标签: image matlab image-processing

所以,这是一个令人困惑的问题,我不完全确定我理解。我要做的是获取ASCII值并在.txt文件中创建灰度艺术。我的两个输入是图片和我必须制作的字母串......很漂亮。所以这就是我的函数头所需要的:

Function Name: image2ascii
Inputs  (2): - (char) the file name of an image to analyze
               - (char) a string of characters to use to make ASCII art
 Output Files (1): - a .txt file that resembles the original image

我获得了一套指导原则:

1) Convert the input image to grayscale by averaging the values of the
      red, blue, and green layers. Do not truncate the values!
   2) For each 8x4 block in the grayscale image, compute the average
      intensity (I) of all 32 pixels and round it to the nearest integer.
   3) Map these values to characters using the following scheme:
        0 <= I <=  31 : input_string(1)
       32 <= I <=  63 : input_string(2)
       64 <= I <=  95 : input_string(3)
       96 <= I <= 127 : input_string(4)
      128 <= I <= 159 : input_string(5)
      160 <= I <= 191 : input_string(6)
      192 <= I <= 223 : input_string(7)
      224 <= I <= 255 : input_string(8)
   4) Print these characters to a .txt file with the same filename as the
      input image. There should be a newline character at the end of each
      line.

我做了第一名!所以我到了某个地方。我不确定8x4块。我在考虑做一个子情节.... 至于3号......我想我会为那些做一个if语句,但我有点困惑。 4号......一旦我理解了为其他一切做些什么,我就会明白这一点。

Notes:
   - The input image will always be evenly divisible into 8x4 blocks
   - The input string will always have 8 characters

测试用例:

image2ascii('abe.jpg', '@8XYv:. ');
 => output file named 'abe.txt' identical to 'abe_soln.txt'

Ol'Abe:http://tinypic.com/r/wb4g42/8

http://oi62.tinypic.com/wb4g42.jpg

我会在底部发布解决方案......因为它很大

这是我到目前为止所做的:

function image2ascii(pic, str)
%// Reads in the image
Img = imread(pic);
%// Extracts the color layers
Red = Img(:,:,1);
Green = Img(:,:,2);
Blue = Img(:,:,3);
%// Finds the size
[r,c,l] = size(Img);
%// Grayscales the image
gray = uint8((double(Red)+ double(Green)+ double(Blue))./3);
Red = gray;
Green = gray;
Blue = gray;
%// Creates a grayscaled image
Grayimg = cat(3,Red,Green,Blue);

%//Makes it into 8x4 blocks
imgrows = r ./ 8;
imgcols = c ./ 4;

end

abe_soln txt:

YYvvvvvvvvvvvvYX888XXXXXXX8888888@@@@@8XYvv:::::::::::::::::
vvvvvvvvvvvvX8@@@@@@@@@@@@@@@@@@@@@@@@@@@8Yv::::::::::::::::
vvvvvvvvvvv8@@@@@@@@8@@@@@@@@@@@@@@@@@@@@@@X:::..:.:::::::::
vvvvvvvvY8@@@@@@8XXX88@@@@@@@@@@@@@@@@@@@@@@Y::......:::..:.
vvvvvvX@8@@@@@@@8888888@@@@@@@@@@@@@@@@@@@@@8XXXXYYvv::.....
vvvvv8@@@@@@@@8888@@@@@@@@@@@@@@@@@@@8888@@@@@@@@@@88XXYv...
vvvvX@@@@@@@@@@@@@@@@@@@@@@@@@@@@@8Xv:::..vX8@@@@@@@8888Xv:.
vvvv@@@@@@@@@@@@@8888@@@@@@@8888XYv:..      .v8@@8888888XYv:
vvv8@@@@@@@@@8Yv::::v:::::::::....          ..vX88XY8@8XYv::
vvv@@@@@@@XY:.....                          ...vXYYX@@@X:...
vvX@@@@@8v.                                ....:vvY88888v...
vY@@@@88v.                               .......vXX88XXXY...
vX@@@88Y.                             .......::.vYX8XX8XY. .
v8@@8XXv...                     .. ..........::::vXYX88X8:  
v@@XXXXv...                              ....::::v8Y8XX888: 
v8888XYv...                              ...:::vvYXXX@8X8@X.
vY8@8YXY:..                             ..:::vvYYYX888@8@@X:
vv8@@X8X:...             .      ..:vvvYYXX88XYvYYYX@@@@@88XX
v8@@@88X::...:vYYYYXXXXYYv:..:::vYX@@@@@@@@@@88XYYX8@@8XXYXX
vY8@8@@Xv:::YXX88@@@@@@@@XXv..vY8@@@@@@8888@@@88YYX8@8YXXYYY
vX@@@@@8Y:.:Y8@@@@@@@@@@8XY: .vv88@@@88888888888XYY888X88YYY
Y@XYY88XY:.vX8@8888@8X@8XX::   .YXXYvv::vvYXYYYYYvvY8@@8XYYY
YXvvvYvYY:.vYY:.:::vvvv::...   .vYvv:::::::::v:::vvY8@@8XYvv
vYvvvY88Y:...........::.        vYYv::vv::::::..:vvX888XXvv:
vv:::Y88Y::.   ........         :vYYv::...:....::vY@@8XYvvv.
vvvv:vX8Yv:..    ....    ..     .vYXv:........:vvY8@@8Yv::: 
vvvvvvv888Y::.......    ..      .vXYYv:....::vYYXX@@@Xvv::  
vvvv:::v88@Xv::::..   .v:  ..::vYXXYYYXv:.:vYXX88@@@8YYv:.  
vvvvv:.::X@@XYYvv:...vYv.:XX888@@@@@XvvYv:vvXXXX88@Yv:..    
vvvvvv:::v@@@XYvvv::vXv   ..:YXY888XYv::vYvYXYXXX88Y:       
vvvvvv:::::X88XYYY:vX:       .v.:YYvvvvvvvYYXYXXXX8Y..      
vvvvvv:::::YXXXXYYvv........::..vvvvvYYXYYYY8YYXXXXv:..     
vvvvvv::::vXXXXYYY::Y8Y:::vvvvvYYYYXXX88XXvY888XX8Xv:.      
vvvvvvv:::YY88XXYYvvXv.:vvvvvvvvYYXX8XYYYvvX888888XXv.      
YYvvvvvvv::YX88X8XXXXYvv:vY88X8X888XXXX8XX88888888vv.       
Yvvvvvvv::::YX8888X8@@8@88v:::vYYYYX8@88@888@88888Y.        
YYYvvvvv:::::Y8@@@@@@888@8XYYYXXXXX88@@8@@@@@@@@XXv.        
YYYvvvvvv::::vX@@@@@@@@@@@8XX888X8@@@@@@@@@@@@@8Yv:.        
YYYvvvvvvv::::v8@@@@@@@@@@@X@@888@@@@@@@@@@@@@Yv:YY.        
YYYvvvvvvvvv:::vY8@@@@@@@@@@@@@@@@@@@@@@@@@8v:.  .@Y.       
YYYYvvvvvvvv:::v v@@@@@@@@@@@@@@@@@@@@@@@@8Y.    X@@8:      
YYYYvvvvvvv::v8Y Y@8@@@@@@@@@@@@@@@@@XYv::.     Y@@@@@Y.    
YYYYvvvvvvv:Y@@.  :XX8@@@@@@@@@@@@XY:.         :@@@@@@@8v. .
YYYvvvvvvvvX@@X     vYYXYX88@@8Xv..::::..     .@@@@@@@@@@8Yv
YYvvvvvvvv8@@@v               ...:Y8@@@@@@8XYY@@@@@8@@@@@@@@
YYvvvvvvv8@@@@v   :YXXYvvvvvvYX88@@@@@8XXXX8@@@@@@888888888@
YYYYYX8@@@@@@@Y Y@@@@@@@@@@@@@@@@@88XYYYYYYYY8XYYX8888888888
YYX8@@@@@@@@@@@@@@@@@XXYYXX88XXXXXXYYXvvYYYvY8@XvvX888888888
@@@@@@@@@@@@@@@X8@@@8XYYXX88XXXYYYXXYXYvYY8@8XXvY8@@88888888
@@@@@@@@@@@@@@Y   vXXXYX8@@@8888XXXXXY88@XYYvvvY@@@@@@888888

1 个答案:

答案 0 :(得分:2)

很酷的问题!

鉴于到目前为止你知道多少MATLAB知识,我们不能使用任何花哨的图像处理功能......即使我很乐意使用它们。我们别无选择,只能用循环来做这件事。

你所拥有的第一部分是正确的。您在图像中读取,提取所有平面,然后将所有平面平均在一起。接下来你需要做的是打开一个文件,在那里你要编写你的ASCII艺术作品。我们将此文件命名为与输入图像的名称相同。打开此文件后,我们需要提取8 x 4块(不同),计算平均强度和圆形。一旦我们这样做,我们使用强度来确定我们需要的输入字符串中的哪个字符。

我们注意到每个有效范围是32的倍数。因此,我们只需将强度除以32即可。这将告诉我们需要进入哪个指数。请注意,当你除以32时,我们只需要除以整数,因为这会告诉我们我们所处的范围的多个。因此,你需要取{{ 1}}。一旦你这样做,我们将得到一个从0到7的值。从1 - 8开始,当MATLAB从1开始索引数组时,我们只需添加1.我们可以使用这个索引索引到floor来提取写入文件的正确字符。

我们使用fprintf打印单个字符,即index_string中的字符到文件中。在每一行之后,我们需要输入一个手动回车来转到下一行。我们重复此操作,直到我们到达您图像的最后一个块,然后我们关闭文件。

没有进一步的麻烦:

input_string

使用示例图像,这是我从文本文件中获得的输出:

function [] = image2ascii(pic, input_string)

%// Reads in the image
Img = imread(pic);
%// Extracts the color layers
Red = Img(:,:,1);
Green = Img(:,:,2);
Blue = Img(:,:,3);

%// Grayscales the image
gray = uint8((double(Red)+ double(Green)+ double(Blue))/3);

%// Open up a file for reading.  Call it the same as the image name
%// but as a text file
fid = fopen([pic(1:end-4) '.txt'], 'w');

%// For each 8 x 4 block we have
for r = 1 : 8 : size(Img,1)
    for c = 1 : 4 : size(Img,2)

        %// Extract the block
        block = gray(r:r+7,c:c+3);

        %// Find the average intensity and round
        meanBlock = round(mean(block(:)));

        %// Figure out which character to output
        index = floor(meanBlock/32) + 1;        
        fprintf(fid,'%c', input_string(index));
    end
    %// Go to the next row
    fprintf(fid,'\n');
end

%// Close the file
fclose(fid);