I'm trying to apply an algorithm only to a specific region of an image. I tried imfreehand
, but not able, at least for me, to do that using this function.
So, is there some way when running my code for the operations to be applied only to some specific region of an image in MATLAB
?
Thanks.
Using a mask defined by any of the "imroi" functions - imfreehand and imellipse included, you can use roifilt2 to filter just the roi using a given filter or function.
First, define the area:
imshow(I); %display your image
h = imfreehand; % now pick the region
BW = createmask(h); %makes BW mask
Then, use roifilt2 in one of the following ways -
Define a filter and apply it:
H = fspecial('unsharp');
I2 = roifilt2(H,I,BW);`
Apply a given function to the roi:
I2 = roifilt2(I, BW, 'histeq');
Apply a given function to the roi, specifying parameters:
fh = @(I)(histeq(I,5)); %define function
I2 = roifilt2(I, BW, fh);
The last is equivalent to calling I2 = hist(I,5); but only works on the defined roi.
ETA:
If you want to call multiple functions on the roi, it may be easiest to define your own function, which takes an image input (and optionally, other parameters), applies the appropriate filters/functions to the image, and outputs a final image - you would then call "myfunc" in the same way as "histeq" above.