This page will show informally ways to obtain 2D images and apply operations on them using mathematical and structural expressions.

Images are matrices whose elements correspond to pixels. To visualize the images, viewmat utility will be used.

```   load 'viewmat'
viewmat j./~i:10       NB. basic image: (x+iy),  x=y=_10..10
viewmat |@j./~i:10     NB. abs(x+iy)
viewmat (+&.*:)/~i:10  NB. sqrt(x^2+y^2) -- cone
(+&.*: -: |@j.)/~i:10  NB. complex magnitude
1```
 Complex plane x+iy Cone

## Encoding Values

There may be various ways to encode the value of a pixel:

•  any value convenient as immediate result of a mathematical expression and suitable as input for viewmat, which will map it's range over the hue spectrum boolean may represents a mask 0..1 may represent one color component or opacity mask 0..255 one color or gray integer an rgb color, such bytes are (red, green, blue): see Encoding RGB

For example,

•  Boolean Gray 0..255 Values 0..7 2|!/~i.32 viewgray 4*i.8 8 ((*:18) 8&|@|@- +&*:)"0/~i:20

### Encoding RGB

For use in viewmat for Windows integer values correspond to colors as follows:

•  Color Dec Hex Expression black 0 16b000000 0 red 255 16b0000ff 255 green 65280 16b00ff00 256#.2{.255 blue 16711680 16bff0000 256#.3{.255 white 16777215 16bffffff 256#.3#255

```   ]M=. 2 2\$16b000000 16b0000ff 16bff0000 16bffffff
0      255
16711680 16777215
hexmat M
000000 0000FF
FF0000 FFFFFF

viewrgb M
savemat_jviewmat_ 'rb.bmp'

(; hexmat) 2{."1 readbmp 'rb.bmp'
+-----------------+-------------+
|       0      255|000000 0000FF|
|16711680 16777215|FF0000 FFFFFF|
+-----------------+-------------+

viewrgb 2{."1 readbmp 'rb.bmp'```

Note: this order of RGB in integer does not correspond to regular practice, like for HTML and CSS, Photoshop, etc. There, red is #ff0000, i.e. first as in Red-Green-Blue.

## Operations

Now we will try some image operations.

Gradient is a matrix of 0..255 with a given shape.

```  m255=: <.@(255&*)@%
grad=: i. m255 [: <: */
vgrad=: {: #"0 (i. m255 <:)@{.
hgrad=: \$ (i. m255 <:)@{:```

These basic routines can produce further results

```   viewgray (0.5 <.@* vgrad + hgrad) 32 32  NB. diagonal
viewmat M=: 19 >+&.*:/~ i:20             NB. circle mask
viewrgb blue@vgrad@\$ M                   NB. vertical
viewrgb (red@(* hgrad@\$)@:-. + blue@(* vgrad@\$)) M```
 Diagonal Circle Mask Vertical Combination

### Color Combinations

Here is an interesting example of J engine capabilities of data representation. Gray transform to convert values 0..255 to gray RGB was defined as follows:

```   gray=: ((256&#.)@:(3&#))"0
hexmat gray 16*i.4 4
000000 101010 202020 303030
404040 505050 606060 707070
808080 909090 A0A0A0 B0B0B0
C0C0C0 D0D0D0 E0E0E0 F0F0F0

hexmat2 gray 16*i.4 4
000 111 222 333
444 555 666 777
888 999 aaa bbb
ccc ddd eee fff```

hexmat2 is a compact representation of (A)RGB scaled down by 16, i.e. 0..255 is mapped to 0..15.

Then viewgray=: (gray i.256)&viewmat was defined to provide gray "hue" for viewmat. In representation of viewgray the following was shown:

• (65793*i.256)&viewmat   NB. wow!

That's 256 256 256#.1 or

```   (256^i.3) +/ . * 1
65793```

Which in hex form has 1's in every byte:

```   hfd 65793
10101```

Now the color transforms are defined symmetrically

```   red  =: 16b000001&*
green=: 16b000100&*
blue =: 16b010000&*
gray =: 16b010101&*```

Now color can be generated separately and combined.

```   viewrgb  green 255*S1=. 2|!/~i.32   NB. boolean to 0..255 to green
viewgray S2=. (grad@\$ * -.) S1      NB. masked gray gradient ...
viewrgb  blue S2                    NB. ... gray to blue
viewrgb  (green 255*S1) + blue S2   NB. combination```

Masks allow selective application of operations on part of an image.

`mask=: 1 : '(] * ~:) + m.&*@:='`

For example, an image is captured from Plot

```pd 'save bmp test1.bmp'
viewrgb I=. readbmp 'test1.bmp'```

Masks will be created to replace particular colors determined with the Viewmat Spy.

```C1=: 256#.255 0 0
C2=: 256#.0 0 255
C3=: 256#.0 128 0
C4=: 256#.255 0 255```

Also a transparency tile to highlight masked area is defined.

```M=: 8 8 blow (,: |.) gray 160 220
T=: (\$I) tile M```

Two Gradients and a transparency tile are applied sequencially to areas masked by selected colors.

```G1=: red  vgrad \$I
G2=: blue hgrad \$I
viewrgb C3 T mask C2 G2 mask C1 G1 mask I```
 Original Multiple Masked Operation

### Blur

• ..

```   viewmat (+&*:)"0/~i:20
viewmat ((*:18) - +&*:)"0/~i:20```

### Edge

• ..

```   viewmat ((*:18) (20 > |@-) +&*:)"0/~i:20
viewmat ((*:18) (60 > |@-) +&*:)"0/~i:20```

### Antialias

• ..

`   viewmat ((*:18) |@- +&*:)"0/~i:20`

## Reshaping

These verbs transform the size of the image matrix.

```tile=: {.@[ \$ {:@[ \$"1 ,.@]
take=: (* \$) tile ]
blow=: {.@[ # {:@[ #"1 ]

T=. (2&|@>.&|)/~ (i:-.0:)8```
 40 40 tile T 4 4 take T 64 96 blow T

## Shapes

 (2&|@>.&|)/~ (i:-.0:)8 (2&|@>.&|)/~(,~ -@|.)@:i:4 mod max mod wave

### Utilities

```   load 'convert'
rgb=: 256 256 256&#:
gbr=: 256&#.

hexmat=:  [: ;:^:_1"1 [: <"1 hfd
hexmat2=: [: ;:^:_1"1 [: <"1 '0123456789abcdef'{~ 16 <.@%~ 256 #.^:_1 ]

gray =: 16b010101&*
viewgray=: (gray i.256)&viewmat

glwh=: 3 : 'wd''pmovex '',(+0 0,y-glqwh_jgl2_@\$@#)&.".wd''qformx'''```

## Resources

Contributed by OlegKobchenko

OlegKobchenko/Image Operations (last edited 2008-12-15 05:17:31 by DevonMcCormick)