MATLAB Examples

This example reduces the number of image colors.

## Original (24-bit RGB) image

```rgb = imread('peppers.png'); ```

Display image using native 24-bit full color RGB format.

```imshow(rgb) title('Original image: 24-bit RGB') ```

## Reduce to 8000 (max) colors

```[im,map] = rgb2ind(rgb,8000,'nodither'); imshow(im,map) title('Reduced to 8000 (max) colors') numColors = length(map) ```
```numColors = 5329 ```

Sometimes you can get fewer colors, depending on the image data.

## Reduce to 4000 colors

```[im,map] = rgb2ind(rgb,4000,'nodither'); imshow(im,map) title('Reduced to 4000 colors') numColors = length(map) ```
```numColors = 4000 ```

## Reduce to 2000 colors

```[im,map] = rgb2ind(rgb,2000,'nodither'); imshow(im,map) title('Reduced to 2000 colors') numColors = length(map) ```
```numColors = 2000 ```

## Reduce to 1000 colors

```[im,map] = rgb2ind(rgb,1000,'nodither'); imshow(im,map) title('Reduced to 1000 colors') numColors = length(map) ```
```numColors = 1000 ```

Obviously with fewer colors we get what we wanted. No need to keep checking with subsequent iterations.

## Reduce to 500 colors

```[im,map] = rgb2ind(rgb,500,'nodither'); imshow(im,map) title('Reduced to 500 colors') ```

## Reduce to 200 colors

```[im,map] = rgb2ind(rgb,200,'nodither'); imshow(im,map) title('Reduced to 200 colors') ```

## Reduce to 100 colors

```[im,map] = rgb2ind(rgb,100,'nodither'); imshow(im,map) title('Reduced to 200 colors') ```

## Reduce to 50 colors

```[im,map] = rgb2ind(rgb,50,'nodither'); imshow(im,map) title('Reduced to 50 colors') ```

Notice false color contouring becomes very pronounced.

Sometimes digital photographs appear more visually acceptable when dithering is used for the color reduction. This is actually the default mode of the rgb2ind function. Up to this point dithering was disabled.

## Use 50 colors dithered (default)

```[im,map] = rgb2ind(rgb,50); imshow(im,map) title('Only 50 colors dithered (default)') ```