how can I apply an algorithm only to a specific region of interest (ROI) ?

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)

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

i can't get a binary image first. my image is quite inhomogeneous and i m gonna have problems (for example with thresholding value)so first i have to define my ROI and then apply my code. i'm using imfreehand to specify the region where i'm interested and in the end the drawn region is untouched inside the region and all black outside the region as in the second demo
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)).
i don't exactly get what you mean. i mask the grayscale image using the createmask command and then select the specific ROI but how should i use the mean value of my native picture and how does it help me to apply the algorithm only to ROI ?
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.
lets say that i want to find the negative of my image (resolution 2672* 4008). if i do Β=255 - grayImage(mask); it returns a matrix of size (167321*1) :/
THat's because it's not a 2D image in that case. You need to do
B = (255-grayImage) .* uint8(mask);
but that works only in the specifc case. as long as grayImage(mask) is a vector not a matrix i can't use it in commands like for example histeq(), graythresh() etc
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.
i have an initial grayscale image where i need to specify a region of interest and after that, i have to find its negative image and apply some filters and commands like histeq or imadjust only to the roi. in order to find the negative i did what you said above and it worked. then i tried histeq(negativeImage(mask)) and imtophat(negativeImage(mask),se) in order to enhance the contrast of the roi and to fix uneven illumination of the roi respectively. but it didn't work as long as negativeImage(mask) is a vector not a 2D image.
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.
ok i figured it out, thanks a lot :)
OK, then can you mark the answer as Accepted to close it out?

Sign in to comment.

Asked:

on 10 Feb 2014

Commented:

on 16 Feb 2014

Community Treasure Hunt

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

Start Hunting!