Search code examples
matlabimage-processingforegroundroi

Region of interest extraction in MATLAB


I am writing a MATLAB code to implement a specific filter on a selected (from auto ROI) grayscale region of a forearm image which consists of veins. I also uploaded the forearm of a subject (after foreground has extracted).

Basically, I have NIR camera images of the forearm of different subjects with different orientations. I wrote the code that has extracted the foreground grayscale image of the arm, that gave me the white background with the forearm. I used Sobel edge to find edges. I also found the nonzero indices using the find function. I got the row and col indices. I need an idea on how to extract image inside (almost 10 pixels) of the edges detected on both sides of the forearm (black and white edged image-also uploaded).

Sobel-edge:

sobel-edge

Foreground image:

Foreground image

ROI image that I need to extract:

roi image that I need to extract

clear all
close all
clc

image= rgb2gray(imread('Subj1.jpg'));
image1=~im2bw(image,0.1);
image1=im2uint8(image1);
foreground=imadd(image1,image);
imshow(foreground);
edgesmooth=medfilt2(foreground);
sobeledge= edge(edgesmooth,'sobel');
sobeledge=im2uint8(sobeledge);
figure 

imshow(sobeledge);
[col,row]=find(sobeledge~=0);

Solution

  • Starting from the mask image that you make here:

    image1=~im2bw(image,0.1);
    

    but inverted, such that the mask is zero for the background and non-zero for the foreground:

    image1 = im2bw(image,0.1);
    

    you can use imdilate to expand it by a fixed distance:

    se = strel('disk',20); % This will extend by 20/2=10 pixels
    image2 = imdilate(image1,se);
    

    image2 will be like image1, but expanded by 10 pixels in all directions.

    imerode does the opposite, it shrinks regions.