how can I apply an algorithm only to a specific region of interest (ROI) ?
Show older comments
hey, i have chosen a region of interest (roi) and i want to apply an algorithm (filtering and morphological operations) only in the specific area not in the whole image. i tried to make a function that contains all the commands and then apply the function to the image but it didn't work.any ideas how to do that?
Answers (1)
Image Analyst
on 10 Feb 2014
0 votes
It's usually done by masking. Do whatever you need to do to get a binary image, then mask it with the ROI image and label and call regionprops. For examples, see the demos I've attached.
12 Comments
maria
on 10 Feb 2014
Image Analyst
on 10 Feb 2014
OK that'll work. If you don't have a binary image, like you're just finding the mean gray level or something, then you can just mask the gray level image. But beware if you're analyzing outside the mask, like if you do mean2(yourImage) and it was masked, it will include zeros. You'd have to do mean(yourImage(maskImage)).
maria
on 11 Feb 2014
Image Analyst
on 11 Feb 2014
After masking you have an image with black outside the mask, and is unchanged inside the mask. Your algorithm will work on that image and has to know how to handle black areas, like ignore them or include them. If you include them you may get edge artifacts. For example, like I showed in the demos if you do
maskedImage = grayImage;
maskedImage(~mask) = 0; % Blacken outside mask.
Then, if your algorithm is to get the mean of the masked area,
mean1 = mean2(maskedImage) % Mean including black
mean2 = mean(grayImage(mask)); % Mean of only pixels inside the mask
These will give you two different means because the first one included the blackened pixels while the second one did not.
maria
on 12 Feb 2014
Image Analyst
on 12 Feb 2014
THat's because it's not a 2D image in that case. You need to do
B = (255-grayImage) .* uint8(mask);
maria
on 13 Feb 2014
Image Analyst
on 13 Feb 2014
I don't know what that means. B is a 2D gray level image and nowhere there did I use grayImage(mask). And you can use B in histeq() (though I don't know why anyone would ever want to use that functions), and in graythresh. Please explain.
maria
on 13 Feb 2014
Image Analyst
on 14 Feb 2014
Edited: Image Analyst
on 14 Feb 2014
Just do everything to the original image and mask it when you're done. Attach your m-file if you need more help.
newImage = ProcessImage(originalImage); % Do whatever you want here...
% Now mask it
newImage(~mask) = 0; % Set everything outside mask to 0.
maria
on 16 Feb 2014
Image Analyst
on 16 Feb 2014
OK, then can you mark the answer as Accepted to close it out?
Categories
Find more on ROI-Based Processing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!