Subtracting mean image intensity value from all individual pixels

7 views (last 30 days)
I have 3D imaging stacks that I'd like to investigate individual images from within the image stacks. I have found the mean intensity for each image and now would like to subtract this mean from all pixels within that image. I want to do this for all images within the stack and then show the image stack. I've tried using imsubtract, but I'm getting matrix mismatch errors. Here's the code in question.
% Request file selection.
[fileNameF pathNameF] = uigetfile('*C0*.tiff');
filePathF = fullfile(pathNameF, fileNameF);
disp(fileNameF);
filePathB = strrep(filePathF, 'C0', 'C1');
%Input wavelength, etc.
prompt = {'Calibration ratio:', 'Lower threshold', 'Upper threshold', 'Z step size [um]'};
dlg_title = 'Inputs';
num_lines = 1;
if exist('fbdefaults2.mat', 'file')
load('fbdefaults2.mat', 'def');
else
def = {'1','6', '254', '1'};
end
answer = inputdlg(prompt,dlg_title,num_lines,def);
fbCalibrationRatio=str2num(answer{1});
lowerThreshold=str2num(answer{2});
upperThreshold=str2num(answer{3});
zStepSize=str2num(answer{4});
def=answer;
save('fbdefaults2.mat', 'def');
info=imfinfo(filePathF);
slices = length(info);
height=info(1).Height;
width=info(1).Width;
fAttVecMean=zeros(1,slices);
bAttVecMean=zeros(1,slices);
newIfmean=zeros(1,slices);
for slice=1:slices
If = (imread(filePathF, slice))
Ib = (imread(filePathB, slice))
%Thresholding the images within the stack
mask = find((If < lowerThreshold) | (If > upperThreshold));
If(mask)=NaN;
%Ib(mask)=NaN;%Stringent condition.
mask = find((Ib < lowerThreshold) | (Ib > upperThreshold));
Ib(mask)=NaN;
%If(mask)=NaN;%Stringent condition.
fAttVal = nanmean(If(:));
bAttVal = nanmean(Ib(:));
fAttVecMean(1,slice)=fAttVal;
bAttVecMean(1,slice)=bAttVal;
IfDif(1,slice)=imsubtract(If,fAttVecMean);
end
Thanks for the assistance!
  1 Comment
Image Analyst
Image Analyst on 26 Sep 2014
Edited: Image Analyst on 26 Sep 2014
I fixed the formatting for you. As thanks, please read this: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup. By the way If is probably not a good name for a variable since it looks too much like if.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 26 Sep 2014
Edited: Image Analyst on 26 Sep 2014
There is absolutely no need to mess around with nans, find(), and nanmean(). Just simply do this:
[rows, columns, numSlices] = size(If);
IfDif = zeros(rows, columns, slices, class(If));
for slice=1:slices
If = (imread(filePathF, slice))
mask = If < lowerThreshold) | (If > upperThreshold);
fAttVecMean(slice) = mean(If(mask));
Ib = (imread(filePathB, slice))
mask = Ib < lowerThreshold) | (Ib > upperThreshold);
bAttVecMean(slice)= mean(Ib(mask));
% Insert the difference image into the current slice of the 3D image.
IfDif(:,:,slice)= If - fAttVecMean(slice);
end
And like I said earlier, call the image something, anything other than If - calling it "thisSlice" would be a conventional name that you could use.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!