Projecting binary mask back onto image to read values

14 views (last 30 days)
I am trying to estimate average total signal of objects and average background value using the image processing tools. Whilst this is the first time I have attempted, I have read many examples and have applied the following method.
%1_Median Filter to remove some pixel noise
%2_Get background, my objects are typically <5 pixels
sb_open=imopen(im,strel('disk',100)); %Get features with radius>100
%3_Subtract original image from this
%4_Threshold and fill holes in binary image
level = graythresh(im)
z5 = imfill(z4_bw,'holes');
%5_dilate slightly so will cover all of objects when projected back to raw image
se = strel('disk',1);
I2 = imdilate(z5,se);
I have 4 questions.
1: If I am wanting to use this final processed binary image as a mask to project back onto the original image to obtain both the average intensities of the white area of the binary (i.e. the objects themselves), and then the black areas of the binary image to obtain the average background, is this a good approach?
2: How do I use this binary mask to obtain values corresponding to the white areas projected back to the original image
3: How do I use this binary mask to obtain the values corresponding to the black areas projected back to the original image?
4: The strel function for both imopen and dilate are manually entered. Are there any pointers to how this can be automated. I may have images where the same objects are represented by different numbe rof pixels (i.e. the magnification is changed upon image capture)
Thankyou for any assistance. Jason
Image Analyst
Image Analyst on 24 Oct 2014
Hard to say without seeing your image. Post it if you want specific help. An opening would give you a background if you had bright objects on a dark background, though a radius of 100 pixels is pretty large. It is not always appropriate to subtract a background from a grayscale image. In fact most times it is not, radiology and fluorescence being two exceptions that pop to mind. Possibly also topography. Most cases of reflective imaging would require a background division. If you don't know why, ask.

Sign in to comment.

Accepted Answer

Bruno Pop-Stefanov
Bruno Pop-Stefanov on 24 Oct 2014
Edited: Bruno Pop-Stefanov on 24 Oct 2014
The binary image you obtain can be used to do logical indexing. Logical indexing is accessing the elements of a matrix using a logical (i.e., binary) matrix.
Once you have the binary image I2 indicating the location of foreground pixels (with 1's), you can get a list of the values of the foreground pixels as:
foregroundValues = im(I2);
foregroundValues is a column vector containing the pixel values of the foreground.
Similarly, get the back the background values using the negation of I2:
backgroundValues = im(~I2);
You can then calculate the average pixel value in the foreground and the background:
meanForegroundValue = mean(foregroundValues);
meanBackgroundValue = mean(backgroundValues);
If you are not getting what you expect, display the mask in a figure window to make sure that your processing steps to separate the foreground and background works. For example, in the piece of code below I am overlaying a mask I drew by hand using the roipoly function with cameraman.tif, a grayscale image shipped with Image Processing Toolbox.
% Open the image file
im = imread('cameraman.tif');
% Draw a polygon by hand to define a mask
I2 = roipoly(im);
% Create an RGB image from im
imRGB(:,:,1) = im;
imRGB(:,:,2) = im;
imRGB(:,:,3) = im;
% Overlay the mask in the green channel
greenChannel = im;
greenChannel(I2) = 255;
imRGB(:,:,2) = greenChannel;
% Display the green mask over the black and white picture

More Answers (0)

Community Treasure Hunt

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

Start Hunting!