Problem 1199. ASCII art

Convert a grayscale image to ASCII

Inputs:

  • img: Grayscale image (a matrix of values between 0 and 1)
  • chars: Char array of valid ascii characters
  • bitmap: Grayscale images of each valid ascii character (3d matrix with size Height x Width x N, and with values between 0 and 1)

Output:

Bin the original image img into segments of size Height x Width, and find the best matching ascii character for each bin (minimizing the euclidean distance between each bin grayscale values and the matching character grayscale values).

Return the fitted ascii image: a matrix of characters in chars that, when displayed, approximates the image img.

                              _____                                      
                             QQÊÊÊÊþ¿_                                   
                            gÊÊÊÊÊÊÊÊÊþ                                  
                           qÊÊÊÊÊÊÊÑÑÊÊg_                              ``
                          _ÊÊÊþÎÊÊ ¾ºÜÑÑÑ®½g                        `````
                   gQQQQQØÊÊÊÑÊQQ¸ ¸»gÊÃ@Q©ÑmM                     ``````
                 _ØÊÊÊÊÊÊÊÊÊþ°ÑÊÊQ©Ñ ÊÊÊÊÊQ____g                   ` ````
               _gÊÊÊÊÊÊÊÊÊÊÊÊQQQÊÊÊ  ÎÊÊÊQÊþ¯¯¯¯                     ````
             gØÊÊÊÊÊÊÊÊÊÊÊÊÊÊþÊÊÊÊÑ×,_ÎÊίÊÊÊ                         ```
           _ØÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊQQÊQgÊÊ`¯°  ¸QÊ                       `````;
          gÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊgÊÊÊÊþ_¿__gʯ                       ` `;»»
         ÆÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊQQÊÊÊÊÊÊÊÊÊÊÊÊ_                       ````»»
          ÑÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊØÊÊÊÊÊÊÊÊ_ÿ_ÑÊÊÃ           ³³^            `»»
           ¯ÑÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÑÊÊÑþ           ·              ```»
             ÆÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÑÊ ÊÑÊþ           ·              ```»
             ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊʯÑÊÊÊÊ    Êþ                          `;»
            gÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ  qÊÃÊ `  QÊþ         ·         :`    ```
QgQ¿¸      gÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ   ÊÑ Ê ` qÑÑÊÿ        ·        ;``    `:`
ÑÑÑÑ©¸  ¸ qÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊʯ  qÊÎ Î ¨ ÑÑîÑʸ       ·      »»¨² ` ¸¸¸¸¸
QQQ¿ggQQÊQÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÃ »»qþþ Î » . ,¡Êþ_¿¿q»¿ jQ_`         _QQÿgQ
ÑÑÑÑÑÑÑÑÑÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÑÑÑÑÊQÑÊÑ_ÎÑQQQQjÊþþÊÑþQQÊÊÊÊg¿_  _g©ÑÑÑÊÊÑÑ
Ñ»©QQÿ©©QÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ°``¨qÊfQÊýÑQþ`©ÑÜÑÑÊþÊÑQÊÑÑÑÑÑÊÑÑ©©ÑÑÑÑÑÊÊÊÊÊ
Ñ©©ÑÑÑ©QÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÜλ¯ÊÑÑÿ.Ê ÎÎÑ»¯ÜÜÎÑÊþÎÎβ²²¨²¨²²»»»»ÎÎÎÎÎÎÎÑ
©`²Î¨ÎÎÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÑ»»»qÊQÿ »Ê ©Q©©Ñ»²©©ÑÊþQQQQQQQQQQQQ©©©©©©©©QQ
©©Q©©ÑÑÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ©©©©ÊÊÊ°QQÊ Q©Q©QQQ¿ÜÑÊþÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
©Ü©©^»²©ÑÑÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊѸ¨¸QÊMÿQQÑ Ñ»©»©Î»»Q¸ÜÊþQÑÑ©©ÑÑ©©©©ÜÑÑÑÑÑÑÑÑÑ
©»»»QQѩλÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ»»»ÎØѳ;Ñ»©©»¸¨²²²©©Ñ©Ñh¯Ê_©»QQÑ©ÑÑQÑQÑÑÑÑÑÑÑÑQ
©QQîQ©»Q;»ÊÊÑÊÊÊÊÊÊÊÊÊÊÊÊÑQQ¿_ÊÃÊÜ©QQÑQQQ©Î^QÎÑQQQ©ÑÊþ©©ÑÑ©Ñ©Ñ©ÑÑÑÑQQQQQQ
Ñ©ÜÑÎÎÜÎÎÜÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊQQÑÑQʽÑÑÑÑQQQQ©Q»Î뽩¯Ñ¨²ÑÊQÎÎÜÑÑÑÑÑÑÑÑQÑÑÑÑÑÑ
ÑÑÜÑÑÑÑÑÎQÊÊÊÊÊÊÑÊÊÊÊÊÊÊÑÑÑÑÑÑÊ#Ñ©ÑÑQQÑÑ©©©Ñ»½©©©©©QQÊѽÑÑÑÑÑÑQÑÑÜÑQÑÑÑÑÑ
ÑQQÑQ©Ñ©©ÑÊÊÊÊÊÊÑÊÑÊÊÊÊѽ»ÑÑÑÎ ¯©QQ©Ñ©©ÿ©©ÑÑ©ÿѽ½ÑѽÑÑq ýÿ©QÑ©Ñ©ÑÑ©©ÑÑÑ©Q
ÑÜj©Q©QÎQQÊÊÊÊÊÊ¿ÊÊÊÊÊÊÑ©©©©Q° »Q©©Î©Q©QQQ©ÑÑÑQΩÑÑ©Ñ©ÑÀ`ÑÑÑÑÑÑ©ÑÑÑÑÑÑÜQQ
QQÑ3ÑQQÑÑQÊÊÊÊÊÑ2QÊÊÊÊÊþQQ©©Ê ¸ÑÑ©©©Q©ÑQÑQ©ÑQÑÑ©QÑÑÑQQÑÑ¿³ÑÑÑÑ©ÑÑÑÑÑÑQÑQÑ
©ÑÑÑQÑQ©ÑQÊÊÊÊÊQÿÑÊÊÑÑÊÊÑÑQÑÊ ÎQ©ÑÑQÑÜÑÑQÑÑÜÑÑQQQÿQQÑ©ÑQÑ¿ÎQQÿQQÑQÊÑÑQÑQÑ

note: for an appropriate display the display font needs to be monospace. If a char image does not fit within the command window, you may try the following to display it on a figure:

 fontsize=6;
 figure;
 uicontrol('style','text','units','norm','position',[0,0,1,1],'string',str,...
 'fontweight','bold','horizontalalignment','left','fontname','monospaced',...
 'foregroundcolor','k','backgroundcolor','w','fontsize',fontsize);

and edit the value fontsize so that the image fits within the figure.

Solution Stats

66.67% Correct | 33.33% Incorrect
Last Solution submitted on Oct 07, 2023

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers10

Suggested Problems

More from this Author38

Problem Tags

Community Treasure Hunt

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

Start Hunting!