Getting Index exceeding matrix dimension for extraction the ROI

I took two image sequence,frame subtraction is applied and got the detected region by choosing the largest blob.Now I want to extract the ROI.My code works well if the detected window is bigger but doesn't work for smaller region.May b the subImage part has some error.Anybody can help me out. Thank you
<<
>>
if true
clc; close all;
rgbImage1 = imread('C:\Users\ASUS\Desktop\Project\Frame\Frame3.jpg');
rgbImage1 = imresize(rgbImage1,[512,512]);
[rows, columns, numberOfColorChannels] = size(rgbImage1);
subplot(2, 3, 1);
imshow(rgbImage1, []);
rgbImage2 = imread('C:\Users\ASUS\Desktop\Project\Frame\Frame4.jpg');
rgbImage2 = imresize(rgbImage2,[512,512]);
[rows, columns, numberOfColorChannels] = size(rgbImage2);
subplot(2, 3, 2);
imshow(rgbImage2, []);
diffImage = abs(double(rgbImage1) - double(rgbImage2));
thresholdValue = 60; mask = diffImage > thresholdValue; mask = any(mask, 3); mask = all(mask, 3);
subplot(2, 3, 3); imshow(mask, []);
BW2 = bwareafilt(mask,3);
subplot(2, 3, 4); imshow(BW2);title('Foreground'); rp = regionprops(mask, 'BoundingBox', 'Area'); area = [rp.Area].'; [~,ind] = max(area); bb = rp(ind).BoundingBox;
subplot(2, 3, 5);
imshow(rgbImage2); rectangle('Position', bb, 'EdgeColor', 'red');
subImage = rgbImage2(round(rp(ind).BoundingBox(2):rp(ind).BoundingBox(2)+rp(ind).BoundingBox(4)),round(rp(ind).BoundingBox(1):rp(ind).BoundingBox(1)+rp(ind).BoundingBox(3))); subplot(2, 3, 6); imshow(subImage); end

Answers (0)

Asked:

on 26 Nov 2017

Community Treasure Hunt

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

Start Hunting!