Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

You can try roipoly.

There is an example on SO here.

Here's an example:

img = imread('peppers.jpg');        % loads the image we want to use
[BW,xi,yi] = roipoly(img);          % create a polynomial surrounding region
BW = repmat(uint8(BW),[1,1,3]);     % create mask
selIMG = img.*BW;                   % apply mask to retain roi and set all else to 0
imview(selIMG)

se=strel('disk',3); 
erosion=imerode(selIMG,se); 
result_image=imsubtract(selIMG,erosion);
imview(result_image)

Edit

On erode: as the matlab doc explains, imerode picks the lowest value from the surrounding pixels (imdilate does the opposite). This means that the original treatment in my answer is inadequate for imerode, it would be better to set pixels outside the selection to max on the colorscale, and I provide an example here on how to do this "manually":

img = imread('peppers.jpg');                        % loads the image we want to use
[BW,xi,yi] = roipoly(img);                          % logical mask which contains pixels of interest
nBW = uint8(~BW);                                   % inverse of logical mask to pick surrounding pixels
surroundingMaxedOut = repmat(255*nBW,[1,1,3]);      % set surrounding pixels to max value
nBW = repmat(nBW,[1,1,3]);                          % make mask with 3 channels to pick surrounding pixels
BW = repmat(uint8(BW),[1,1,3]);                     % make mask with 3 channels to handle RGB
selIMG = img.*BW;                                   % pick the image region of interest
selIMG = selIMG + surroundingMaxedOut;              % final selection with surrounding pixels maxed out
imview(selIMG)                                      % inspect the selection


se=strel('disk',3); 
erosion=imerode(selIMG,se);                         % apply erosion 
finalIMG = img.*nBW + BW.*erosion;                  % insert eroded selection into the original image
imview(finalIMG)

As other answers show, matlab has routines that handle these operations implicitly and are more efficient not least in terms of memory management, however this example provides you with more control so you can see what is happening.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top