Overlay mask on to original image

조회 수: 18 (최근 30일)
Joe Perkins
Joe Perkins 2017년 6월 28일
답변: Image Analyst 2017년 6월 28일
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 Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by