MATLAB Answers

Overlay mask on to original image

218 views (last 30 days)
Joe Perkins
Joe Perkins on 28 Jun 2017
Answered: Image Analyst on 28 Jun 2017
Hi - Coming to the end of writing a script to first segment particles and then save each individually. Ideally I would only like the particle pixels (without background in boundary box) so I am attempting to create masks of each particle, overlay this, make each binary ROI (value of 0) 'transparent' so that the particle is on show with white (value of 1) surroundings - I have tried modifying other similar examples on here but none of them seem to work and the saved image is just the cropped image mask or a big white box ... any advice would be greatly appreciated.
%I = original image
%binaryImage3 = logical mask created earlier on
folder = 'c:/Users/ezxjp1/Documents/MATLAB/ParticleImages'
for k = 1 : numberOfParticles
% Find the bounding box of each particle.
thisParticleBoundingBox = particleMeasurements(k).BoundingBox;
% Extract out this particle into it's own image.
subImage = imcrop(I, thisParticleBoundingBox);
% Create binary mask of subplot and overlay
subImageMask = imcrop(binaryImage3, thisParticleBoundingBox);
subImageMaskUnit8 = im2uint8(subImageMask);
% Get size of original image
[rowssubImage, colssubImage, numberOfColorChannelssubImage] = size(subImage);
[rowssubImageMaskUnit8, colssubImageMaskUnit8, numberOfColorChannelssubImageMaskUnit8] = size(subImageMaskUnit8);
% If size of mask does not match original, resize mask
if rowssubImageMaskUnit8 ~= rowssubImage || colssubImage ~= colssubImageMaskUnit8
subImageMaskUnit8 = imresize(subImageMaskUnit8, [rowssubImage colssubImage]);
Inew = subImage.*repmat(subImageMaskUnit8,[1,1,3]);
pngFileName = sprintf('Particle_%d.png', k);
fullFileName = fullfile(folder, pngFileName);
saveas(gcf, fullFileName);

  1 Comment

Guillaume on 28 Jun 2017
I'm not really clear what it is you're trying to do. Presumably, your particleMeasurements come from regionprops, isn't what you want the Image property returned by regionprops?

Sign in to comment.