# 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

Show comments

### Community Treasure Hunt

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

Start Hunting!