Wednesday, March 30, 2016

Working-with-ROI-of-image-using-Matlab

Working with ROI of image using Matlab

M Saravana Mathan MCA
Project Associate
NIT Trichy

The part of the image, on which you have interest to work out, is called Region of Interest (ROI). In another words, selected subset of image is called ROI. In some contexts, you want to apply operations on ROI of image rather than the entire image. To achieve this, generally people extract the ROI from the image, store it in another variable and then apply operations on ROI. If you want to apply your operations on ROI without extracting from the image, it is bit difficult.

I am writing this article to explain the performing the operations on ROI without extracting from the image. In this context, the ROI part of image is affected rather than the entire image.

For example, you want to apply edge detection algorithm on the half of image (example: top of image), study the following example. Let consider the image as shown below.

Here ROI = "Half (Top) of the Image"

% read input image
img=imread('E:/images/input.jpg');

% convert color to gray image if input is color image
img=rgb2gray(img);
subplot(1,3,1);
imshow(img), title('Input image');

% calculating size of the image
[rows,cols]=size(img);
rows = rows/2;

% Apply Canny edge operation
edgeimg=edge(img(1:rows,1:cols),'Canny');
subplot(1,3,2);
imshow(edgeimg), title('Canny operation on Extracted ROI');

% calculating size of the ROI
[row,col]= size(edgeimg);

% edgeimg is in logical values (0,1). converting edgeimg into grayimage
edgeimg=uint8(edgeimg);
for i= 1:row 
     for j=1:col
         if(edgeimg(i,j)==1)
             edgeimg(i,j)=255;
         end
     end
end

% substituting Extracted ROI in Input image
img(1:rows,1:cols)=edgeimg;

subplot(1,3,3);
imshow(img), title('Input Image after substituing ROI');

output:




See also:

1. matlab-cropping-binary-image-algorithm

Objective of the Program: Program takes a black and white image as input. It removes the black portion and gives the white portion of the image.

1 comment:

  1. You can change the part

    for i= 1:row
    for j=1:col
    if(edgeimg(i,j)==1)
    edgeimg(i,j)=255;
    end
    end
    end

    for just edgeimg(edgeimg == 1) = 255; it is faster and even easier to write.

    Apart from that, the function size() returns double, so you should put rows = round(row/2), just in case.

    ReplyDelete

Python-environment-for-deep-learning-in-windows

Python is increasingly becoming a popular programming language for machine learning and deep learning. If you want to use python for train...