# Basic manipulation of images in MATLAB

The objective of this part is to be familiarized with basic read/write operations on images in MATLAB, and learn the type of images supported as well as some basic operations over them.

## Basics

Images have a size in pixels, that we usually express as `NxM`

. In MATLAB, every image is nothing but a matrix. Each element of the matrix is the gray tone for images in B/W, or, if it is a color image, it can be represented by three matrixes, where each matrix represents one of the three primary colors (red, green, blue or RGB).

To read an image from disk, we can use `imread`

:

```
>> f = imread('chestxray.jpg')
>> f = imread('D:\imagenes\chestxray.jpg')
>> f = imread('/home/user/chestxray.jpg')
```

If the image is B/W, MATLAB will create a matrix `f`

, where each element will have a value of gray:

$f=\left[\begin{array}{cccc}f(1,1)& f(1,2)& \cdots & f(1,N)\\ f(2,1)& f(2,2)& \cdots & f(2,N)\\ \vdots & \vdots & & \vdots \\ f(M,1)& f(M,2)& \cdots & f(M,N)\end{array}\right]$

Now we can check the size of the image:

```
>>size(f)
ans =
1024 1024
```

We can save the size in a variable:

`>>[M, N] = size(f)`

We could also save the number of colors of the image with the same command:

`>>[M, N, c] = size(f)`

And if we want detailed information about the image:

```
>>whos f
Name Size Bytes Class
f 249x500 373500 uint8 array
Grand total is 373500 elements using 373500 bytes
```

## Displaying images

We may want to display our image or apply effects to it. We can do that with `imshow()`

:

`imshow(f,G)`

`f`

is the image and `G`

is the number of intensity levels to show. If `G`

is not specified, 256 is used. The syntax is:

`imshow(f,[low high])`

For example:

`imshow(f)`

`imshow(f,[0,50])`

If we want to expand the dynamic range, we can leave the brackets empty. That will use the minimum intensity value of the image as the low limit, and the maximum value as the high limit:

`imshow(f)`

`imshow(f,[])`

Another interesting function is `pixval`

, which affects the last loaded image. When you run it in the console, you can see the value of the intensity when the mouse hovers a particular pixel. It can also measure the euclidean distance between two points.

Finally, when a new image is loaded, MATLAB overwrites the window of the previously loaded image. To avoid this, and open a new image in an independent window, do:

`>> figure, imshow(f)`

## Saving images

To save an image:

`>> imwrite(f, 'filename')`

If `'filename'`

doesn't have an extension that MATLAB recognizes, use:

`>> imwrite(f, 'filename', 'tif')`

We'll be working mainly with JPEG files, in which case you can also specify the quality:

`>> imwrite(f, 'filename.jpg', 'quality', q)`

Where `q`

is a number between 0 and 100 (0 = biggest compression, worse quality, 100 = smallest compression, best quality).

To get information about an image:

`>> imfinfo filename`

would show:

```
>> imfinfo test.jpg
ans =
Filename: 'test.jpg'
FileModDate: '08-feb-2005 17:18:13'
FileSize: 6125
Format: 'jpg'
FormatVersion: ''
Width: 600
Height: 494
BitDepth: 8
ColorType: 'grayscale'
FormatSignature: ''
NumberOfSamples: 1
CodingMethod: 'Huffman'
CodingProcess: 'Sequential'
Comment: {}
```

## Image types

`double`

- Double precision, numbers in floating point that vary in a range of -10
^{308}to 10^{308}(8 bytes per element) `uint8`

- 8 bit integers in the range of [0,255] (1 byte per element)
`uint16`

- Integers of 16 bits in the range of [0, 65535] (2 bytes per element)
`uint32`

- Integers of 32 bits in the range of [0, 4294967295] (4 bytes per element)
`int8`

- Integers of 8 bits in the range of [-128, 127] (1 byte per element)
`int16`

- Integers of 16 bits in the range of [-32768, 32767] (2 bytes per element)
`int32`

- Integers of 32 bits in the range of [-2147483648,2147483647] (4 bytes per element)
`single`

- Floating point number of simple precision, with values in the range of -10
^{38}to 10^{38}(4 bytes per element) `char`

- Characters (2 bytes per element)
`logical`

- The values are 0 or 1 (1 byte per element)
- Intensity images
- A matrix whose values have been scaled to represent intensity. They could be
`uint8`

or`uint16`

. If they are`double`

, the values are scaled in the range [0,1]. - Binary images
- Images whose values are only 0 or 1. They are represented in MATLAB using
`logical`

values. To convert an array of zeros and ones into a`logical`

array in MATLAB, we type:

To check if an array is`>> B = logical(A)`

`logical`

:`>> isLogical(A)`

## Converting images

Command | Converts to | Valid entry type |
---|---|---|

`im2uint8` |
`uint8` |
`logical` , `uint8` , `uint16` and `double` |

`im2uint16` |
`uint16` |
`logical` , `uint8` , `uint16` and `double` |

`mat2gray` |
`double` ([0,1]) |
`double` |

`im2double` |
`double` |
`logical` , `uint8` , `uint16` and `double` |

`im2double` |
`logical` |
`uint8` , `uint16` and `double` |

For example:

```
>> f = [0 0.5; 0.75 1.5]
f =
0 0.5000
0.7500 1.5000
>> g = im2uint8(f)
g =
0 128
191 255
```

```
>> g = [0 0.3; 0.7 0.9]
g =
0 0.3000
0.7000 0.9000
>> gb = im2bw(g, 0.6)
gb =
0 0
1 1
```

You can practice these concepts using `snippet01`

.