Ben
Ben

Reputation: 966

Matlab execute function each cell Matrix with matrix and cell postion as arguments

I need to apply a function to each pixel of each image in a set of ~400 images. The function I wrote (called customf) requires 3 arguments: the matrix, and the position of the cell in the matrix (m and n). The position of the pixel is needed in order to compute the LBP (local binary pattern) which requires the values of surrounding pixels. customf(matrix, m, n) returns an integer d so that 0 < d < 256, and I would like to store each value of d in a matrix of the same size as my image.

given that the set is quite large, I'd like my code to be as efficient as possible, but I don't understand how to use cellfun or arrayfun in such a way.

or is it a better solution? (using nested for might be inefficient?)

thanks!

Upvotes: 2

Views: 150

Answers (1)

Shai
Shai

Reputation: 114786

Can you write customf in a different way? Instead of working on the entire image for each pixel (m, n), why don't you give it only a local patch required for computing the LBP of the patch's central pixel?

For example, if customf needs to look at pixels -/+ k away from m, n to compute the response d at m, n, then you may have

k = 5;
localF = @( patch ) customf( patch, k+1, k+1 ); % assuming patch is of size (2k+1)x(2k+1)
% apply LBP to an image
D = nlfilter( image, [2*k+1 2*k+1], localF );

Note that nlfilter zero-pads image in order to obtain D with the same size as image.

Upvotes: 1

Related Questions