how to call a function inside cell array

10 views (last 30 days)
Dear Team ,
My work is as follows , I've to divide an RGB image (256 x 384) into 6 equal sized(each block size 128 x 128) non-overlapping blocks and apply functions like mean, median on R plane , G plane and B plane separately for each block and compare it with another similar block partitioned image.
My doubts:
1. I used cell array for block partition of input image using below code.
img = imresize(I1, [256 384]);
blockSize = 128;
C = mat2cell(img,blockSize*ones(1,size(img,1)/blockSize),blockSize*ones(1,size(img,2)/blockSize),3);
Now i 've find the mean and median of R plane, G plane and B plane for each block. I used the below code
D = cell(2,3);
for i =1:2
for j = 1:3
for k = 1:3
D{i,j} = mean2(C{k}(:,:,1)); /*In this step - I've to find mean, median in one go for each cell - is it possible to call a function here ? I also tried using cellfun(@(x) mean2(x(:)),C) but i dont know how to modify this step for each plane of an RGB image */
end
end
end
/* I'm not getting my output here in expected format :(
My expected output in cell array format
Block 1 = [Rmean Gmean Bmean Rmedian Gmedian Bmedian]
Block 2 = [Rmean Gmean Bmean Rmedian Gmedian Bmedian] ... ...
Block 6
2. How to compare blocks of first image against the blocks of second image (one to one comparison) Block 1 (Image 1) --> Block 1 (Image 2)
Block 2 (Image 1) --> Block 2 (Image 2) ....
Block 6 (Image 1) --> Block 6 (Image 2)
Could you suggest me an algorithm for block matching so that i could take it up as a reference for my work . My guide has asked me to go about one to one block matching but i'm bit puzzled about cell array comparison for image retrieval.How does this comparison help in retrieving relevant images?. Request your suggestion and guidance on this.
Regards, Malini

Accepted Answer

Image Analyst
Image Analyst on 11 Mar 2013
I think your approach is way more confusing that it needs to be. First of all there is no reason (the badly named) D should be a cell array rather than just a simple numerical array. Secondly you could get the means and medians each in one call to blockproc() - no need for for loops, though with only 9 iterations they don't take much time.
  1 Comment
Malini
Malini on 13 Mar 2013
Dear ImageAnalyst
Thank you for suggesting Blockproc(). I used below code for finding the features of each block. I'm able to find mean2 and std2 for each block but i'm facing error when i try finding median,mode, maximum pixel and minimum pixel in each block. I used below code
myFcn = @(blk) [mean2(blk) std2(blk) median(blk) mode(blk) max(blk) min(blk)];
I = cellfun(myFcn, C, 'UniformOutput',false);
figure
subplot(231), imshow( cellfun(@(c)c(1),I) ), title('mean')
.....
Error in ==> CAT arguments dimensions are not consistent.
I understand the error as the output of mean is a single value(i.e.,Mean of Rmean,Gmean and Bmean) and that of median is an array here because of which i'm facing this error. please let me know how to use median(median) in blockproc function so that i could get a single value for median , mode, max and min. Kindly request you to guide me.
Regards,
Malini

Sign in to comment.

More Answers (1)

Matt J
Matt J on 11 Mar 2013
Edited: Matt J on 11 Mar 2013
It looks like it would be much easier if you also split the R,G,B, layers into separate cells. For example, using FEX:mat2tiles
C=mat2tiles(img,[128,128,3]);
Means = cellfun(@(c) mean(c(:)), C);
Medians = cellfun(@(c) median(c(:)), C);
Instead of having separate variables Block1...Block6, it would also be better if you combined all the block data into a 2x3x6 array,
BlockData=cat(3,Means,Medians);
If you do this for several images, you can compare the BlockData arrays as you would any other arrays, e.g.,
BlockDataImage1 >= BlockDataImage2
BlockDataImage1 ~= BlockDataImage2
etc...
  2 Comments
Malini
Malini on 12 Mar 2013
Matt
Thank you for your suggestion. I've obtained the features for each block . Could you kindly help me out with the second query in my above question.
Also could you please let me know the reason for choosing mat2tiles over mat2cell as the output results and memory taken appear same for both.The execution time for mat2tiles is 0.04 sec and that of mat2cell is 1.02 sec. (Is there any other reason for choosing mat2tiles?)
Matt J
Matt J on 13 Mar 2013
Edited: Matt J on 13 Mar 2013
Hi Malini
Is there any other reason for choosing mat2tiles
The only reason for using mat2tiles is that it has an easier interface when all the blocks are of the same size. My good-natured subjective opinion is that the call to mat2cell in your code is really long and ugly whereas mine is really easy and concise :-) But yes, you could apply my same suggestion using mat2cell instead.
If speed is critical for you, however, as you seem to be implying, I would not recommend cutting things into cells at all, nor would I recommend blockproc. I would just apply your triple for loop directly to 'img'
e=0:127;
Means=zeros(2,3,3); Medians = Means;
for i=[1,129]
for j=[1,129,257]
for k=1:3
t=img(i+e,j+e,k);
Means(i,j,k)=mean(t(:));
Medians(i,j,k)=median(t(:));
end
end
end
Could you kindly help me out with the second query in my above question.
As far as I know, I covered everything about that in the second part of my Answer.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!