0

I have a programming challenge out of reach of my very stammering Matlab expertise. I have a list of images and list of ROI mask in a cell array, and I want to extract mean pixel value of each ROI individually, My code goes like this :

files=dir('*.tif');
for f=1:length(files)
image=im2double(imread(files(f).name));       
pixel_value(:,f)=cellfun(@(x) extractROIpixel(x,image), mask);    
% apply extractROIpixel on each cell of the mask array
end

function [ mean_pixel_value ] = extractROIpixel( mask, img )
      mask=im2double(mask);
      mask(mask == 0) = NaN;
      mask_area=mask.*img;
      mean_pixel_value=mean(mask_area(~isnan(mask_area)));  
end

This works, but very slow (<5min), I have 400 images to process with a 200 long cell array of masks (200 ROIs). I'm sure it is due to poor design, as Matlab is widely used for this kind of image processing tasks, but I can't figure out another affordable way to do it, thanks in advance.

Vinith
  • 1,264
  • 14
  • 25

1 Answers1

0

What about:

files=dir('*.tif');
for f=1:length(files)
   image=im2double(imread(files(f).name));
   masked=cellfun(@(x) image.*x, mask);
   stats{f}=cellfun(@(x) regionprops(x, 'MeanIntensity'), masked);
end

I did not benchmark the two approaches, so I don't know if it is more efficient.

Cape Code
  • 3,584
  • 3
  • 24
  • 45
  • Thanks for your fast answer, but it can not work, as I want to apply the "masked" function to each cell of my mask array, within the same image : in other terms, I have to apply function throughout mask array keeping the image constant and iterate for all my images. Hence my "for" loop on the image list. – user2319639 Nov 18 '13 at 18:18
  • I see. I edited it accordingly. Again, I don't know if `regionprops` is an efficient thing. – Cape Code Nov 18 '13 at 18:24
  • Thanks for pointing my attention to the regionprops method, which I did not know of... But it actually takes twice as much time than my extractROIpixel function. – user2319639 Nov 19 '13 at 12:42
  • Ok, good to know. It's pretty convenient, but apparently not very lean. – Cape Code Nov 19 '13 at 16:13