Overlay mask on to original image

23 次查看(过去 30 天)
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]);
end
Inew = subImage.*repmat(subImageMaskUnit8,[1,1,3]);
pngFileName = sprintf('Particle_%d.png', k);
fullFileName = fullfile(folder, pngFileName);
saveas(gcf, fullFileName);
end
  1 个评论
Guillaume
Guillaume 2017-6-28
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?

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2017-6-28

类别

Help CenterFile Exchange 中查找有关 Image Segmentation and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by