Question

In Matlab I've got a big matrix (512x512x100). In order to analyze it I have used blockproc. Due to the fact that blockproc only accepts 2D matrices I have transformed my original matrix into a 2D matrix using mat2cell and some other consequent steps. The blockproc function search for the pixel with the highest value in each sub-matrix and returns its coordinates referenced to that very sub-matrix.

After a couple of steps I've got the coordinates of those maximum pixels referenced to the first mat2cell transformation. Now I would like to convert those coordinates to global coordinates, that is, absolute coordinates in the large original 3D matrix.

I've try some things with this example:

d=rand(4,4,4)

d(:,:,1) =

0.0451    0.8044    0.0784    0.7859
0.8911    0.3481    0.4636    0.9806
0.4887    0.5677    0.0999    0.9488
0.7822    0.0467    0.5569    0.2256

d(:,:,2) =

0.8131    0.8880    0.8066    0.8103
0.8240    0.3358    0.8422    0.2552
0.8364    0.2759    0.3753    0.7741
0.9853    0.8297    0.3745    0.5936

d(:,:,3) =

0.1200    0.1841    0.3897    0.0894
0.0747    0.7485    0.4866    0.4722
0.8387    0.9523    0.0166    0.5013
0.8210    0.3107    0.6935    0.1286

d(:,:,4) =

0.6424    0.7322    0.4631    0.1684
0.5523    0.0953    0.0168    0.6231
0.1073    0.5530    0.8504    0.9304
0.3482    0.7804    0.7657    0.2496

After doing the mat2cell I've got:

cell(:,:,1) =

[2x2x2 double]    [2x2x2 double]
[2x2x2 double]    [2x2x2 double]

cell(:,:,2) =

[2x2x2 double]    [2x2x2 double]
[2x2x2 double]    [2x2x2 double]

These are the resulting coordinates. Each row of the matrix from below represents the coordinates of the maximum pixel in each "sub-cell":

max_px =

 2     1
 2     3
 2     2
 1     2
 2     2
 1     2
 2     4
 1     4

by doing "cell{1}(2,1)" you get:

ans =

0.8911

wich is the value of the maximum pixel in the first 3D sub-batrix (first "sub-cell")

I guess the is a pretty straight forward way to convert those relative coordinates to global, but I don't know how. Thank you in advance.

Was it helpful?

Solution

You don't really have to use blockproc. I don't know why anyone ever does...

  dcell=mat2cell(d,...);

  mask=cell2mat(  cellfun(@(c) eq(c,max(c(:))) , dcell, 'uni',0) ) ;

  [imax,jmax,kmax]=ind2sub(size(d), find(mask(:)));

This assumes that there is a unique max in each sub-array, which you also appear to have assumed.

OTHER TIPS

This uses the max_px that you already have. The 4th and 5th line just convert max_px to more natural 3D coordinates, so maybe those could be omitted, and the code shortened, if you can get it in that form to begin with.

dcell=mat2cell(d,...);   


[outer_dims{1:3}]=size(dcell); outer_dims=[outer_dims{:}],
[inner_dims{1:3}]=size(dcell{1}); inner_dims=[inner_dims{:}],

idx=sub2ind(inner_dims,max_px(:,1),max_px(:,2));

[ii,jj,kk]= ind2sub(inner_dims,idx); %get 3-inner max coodinates


[X,Y,Z]=ndgrid(1:outer_dims(1),1:outer_dims(2), 1:outer_dims(3));

dims6=[inner_dims; outer_dims];

idx6=sub2ind(dims6(:).', ii,X(:),jj,Y(:),kk,Z(:));

 [imax,jmax,kmax]  = ind2sub(size(d),idx6);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top