How to get pixel value around objects in greyscale image?
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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)
... of course, there's nothing that says that the annular regions around two blobs don't overlap.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!