This example requires about 13% as much time as misusing roifilt().
Is there any correlation kernel that keeps the roi of an image while removes the remained pixels? hi, I have already defined the roi of an image (I) by using roicolor, now I need to keep the pixels of I which are within roi and throw out the remaining pixels (the dimensions of output image should be equal to the dimensions of the roi array). I used roifilt2 but I do not know what correlation kernel (h) can be used for my purpose...
Prashant Kumar answered .
2025-11-20

% a single-channel image
inpict = imread('cameraman.tif');
% a logical mask
mask = imread('sources/standardmods/cman/cmantifmk.webp')>128;
% invert the mask and use a zero-valued kernel
%outpict = roifilt2(0,inpict,~mask);
% invert the mask and use a function handle which returns a
% zero-valued array of the same size and class as the image
outpict = roifilt2(inpict,~mask,@(x) 0*x);

% presuming the following: % mask is logical-class % image is not signed-integer class % mask and image have same number of channels outpict = inpict; outpict(~mask) = 0;