## Archive for the Category: ' MATLAB '

### Handbook of Monte Carlo Methods

This book is going to be published in 2011. It explains in detail the theory and practice of Monte Carlo methods. For example, it will explain how to generate billions of random numbers and ensure that the numbers are really random. For example, MATLAB uses the Mersenne Twister algorithm by default, but in fact, this method is not suitable for Monte Carlo methods because of the fact that when you get an extremely low number from this algorithm, the next number will more likely be a low number as well. So, this book will teach us how to generate the random numbers appropriate to Monte Carlo methods.

The official citation for the book is:

**D.P. Kroese, T. Taimre, Z.I. Botev (2011). Handbook of Monte Carlo Methods,
Wiley Series in Probability and Statistics, John Wiley and Sons, New York. **

Currently, the site contains all the MATLAB code for the book. Although we don’t have the book yet, this means that we can still work through the problems. The site is at:

http://www.maths.uq.edu.au/~kroese/montecarlohandbook/

The book is currently listed at $127.72, but it looks like this price is including a 12% discount already. I suppose that after the preorder is finished, the price will go up to $145. You can preorder the book at Amazon.

### MATLAB: converting rgb images into a map of indexed colors and those indexes

Here is a handy MATLAB function for converting a M*N*3 dimensional RGB image into an M*N dimensional array of indexes and a P*3 dimensional map. This is interesting because it’s lossless, unlike the MATLAB function rgb2ind, which has a low maximum number of colors.

`function [ ind, map ] = myrgb2ind( rgb )`

%MYRGB2IND Converts an RGB image into an index array and a map of colors

% make a map of all color values in rgb

% map = 3 columns, index of and the second is the color[rgb_width,rgb_height,channels] = size(rgb); % W*H*channels matrix

% don't use the rectangular shape of the image, just use one long column

[rgb_width,rgb_height,rgb_channels] = size(rgb);

index_all = reshape(rgb,rgb_width*rgb_height,rgb_channels);

% sort the image so all the colors are in order

index_all_sorted = sortrows(index_all);

% remove all colors that are duplicates

map = unique(index_all_sorted,'rows');

% assign index values of the map to each pixel position where the color occurred

% and the reshaped image is index_all;

[NULL,ind_reshaped] = ismember(index_all,map,'rows'); % ind_reshaped holds the row number of the corresponding color

ind = reshape(ind_reshaped, rgb_width,rgb_height); % reshape ind to the size of the image

end

And then for converting back:

`function [ rgb ] = myind2rgb( ind, map )`

%MYIND2RGB converts from lossless index to uint8 rgb

% determine the sizes, NULL can be ~ in 2009b and beyond

[NULL,rgb_channels] = size(map);

[rgb_width,rgb_height] = size(ind);

% reshape ind to the size of the image

ind_reshaped = reshape(ind, rgb_width, rgb_height);

% assign the colors via the index of each color in map

rgb_reshaped = map(ind_reshaped,:);

% get the picture to the right dimensions

rgb = reshape(rgb_reshaped, rgb_width, rgb_height, rgb_channels);

end

To test this I created the .m file program:

`close all; clear all`

%read in the image and show it

me_rgb = imread('../me.jpg');

imshow(me_rgb);

%convert from rgb into an index

[me_ind, me_map] = myrgb2ind(me_rgb);

% and back again

new_rgb = myind2rgb(me_ind,me_map);

%check that the two images are the same == lossless

assert (isequal(me_rgb, new_rgb), '@ta: matrix going in doesnt equal the one coming out');

%display the outputted image

figure

imshow(new_rgb);

Here, the assert function demonstrates that the rgb image coming out is the same as the one going in. If you tried this with the MATLAB function for rgb2ind and ind2rgb, you will not get the two to be equal unless there are less than 65536 colors (which is the max number of colors defined for rgb2ind).