How to get pixel value around objects in greyscale image?

2 views (last 30 days)
Hi!
I'm new to Matlab and stuck with one of my projects.
I am performing image analysis from grey scale images where the pixel intensity represents a specific concentration of element. In the image, I have ovoid objects that have a grey value between 0-40. My goal is to get the average grey value of pixels surrounding the ovoid objects (let's say 4 pixels width around the ovoid objects in any directions).
Here is a picture of what my original image looks like:
I thought to convert the image into binary and use the regionprops command to find objects but then, I'm stuck to:
  • apply the mask of objects to the grey scale image
  • isolate pixels surrounding the objects (with a 4 pixel width from the object edges) and get their values
At then end, I'm not sure that I'm trying to apply the best approach to reach my initial goal.
Here is the code I have written:
[FileName,PathName] = uigetfile('.tif','Choose tiff file to import...');
image = imread(strcat(PathName,FileName));
figure;imshow (image); title('original Image');
bw = im2bw(image,0.195);
filtim = medfilt2(bw);
filtim = ~filtim;
filtim = 1-filtim;
filtim = (filtim == 0);
bw2 = imclearborder(filtim);
bw3 = bwareaopen(bw2, 15);
figure;imshow (bw3); title('Segmented image');
s = regionprops(bw3, image, {'Centroid'});
If any body could help, it would be greatly appreciated.
Cheers
Guillaume

Accepted Answer

DGM
DGM on 4 May 2023
Edited: DGM on 4 May 2023
I have no idea what's going on with the filtering, but considering this
filtim = ~filtim % invert the mask
filtim = 1-filtim % then invert it again
filtim = (filtim == 0) % then invert it again
I'm going to assume that it doesn't matter.
Consider the example:
% read the image
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/152389/image.jpeg');
% binarize the thing
mask = ~im2bw(inpict,0.195); %#ok<IM2BW>
% clean up the mask
mask = imclearborder(mask);
mask = bwareaopen(mask,20);
% create annular mask around regions
localmask = ~mask & imdilate(mask,strel('disk',4,0));
imshow(localmask,'border','tight'); % show it
% get mean of pixels surrounding each blob
S = regionprops(localmask,inpict,'meanintensity');
localmean = vertcat(S.MeanIntensity)
localmean = 56×1
163.3000 147.3158 162.3574 157.1309 165.5685 127.4662 167.3613 161.2959 150.0758 139.5014
... of course, there's nothing that says that the annular regions around two blobs don't overlap.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!