why my program shows an error Index exceeds matrix dimensions.

1 view (last 30 days)
clear all;
close all;
%imtool close all;
clc;
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
rgbImage = imread('pic3.jpg');
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image
figure;
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Display the original color image.
subplot(2, 2, 2);
imshow(redChannel, []);
title('Red Channel', 'FontSize', fontSize);
subplot(2, 2, 3);
imshow(greenChannel, []);
title('Green Channel', 'FontSize', fontSize);
subplot(2, 2, 4);
imshow(blueChannel, []);
title('Blue Channel', 'FontSize', fontSize);
% Extract just the segments and not the numbers.
binaryImage = ~(redChannel < 200) & (blueChannel < 200);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% subplot(2, 2, 1);
figure;
imshow(binaryImage, [0 .5]);
title('binary image')
axis on;
drawnow;
hold on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Now define locations to check.
row1 = 35;
row2 = 110;
row3 = 175;
row4 = 260;
row5 = 330;
col1 = 70;
col2 = 175;
col3 = 275;
% Plot boxes around there to check.
title('Checking Image Inside Red Boxes', 'FontSize', fontSize);
boxWidth = 30;
% First check top segment.
row = row1;
col = col2;
figure;
boxX = [col col+boxWidth col+boxWidth col col];
boxY = [row row row + boxWidth row + boxWidth row];
plot(boxX, boxY, 'ro-');
title('box X v/s box Y')
%*************************************************************
%imageInsideBox=zeros(1, boxWidth);
imageInsideBox = binaryImage(row:row + boxWidth, col:col+boxWidth)
% Determine if there are any pixels set inside that box.
segmentState(1) = any(imageInsideBox(:))
% Now check upper left segment.
row = row2;
col = col1;
boxX = [col col+boxWidth col+boxWidth col col];
boxY = [row row row + boxWidth, row + boxWidth, row];
plot(boxX, boxY, 'ro-');
imageInsideBox = binaryImage(row:row + boxWidth, col:col+boxWidth)
% Determine if there are any pixels set inside that box.
segmentState(2) = any(imageInsideBox(:))
% Now check lower right segment.
row = row4;
col = col3;
boxX = [col col+boxWidth col+boxWidth col col];
boxY = [row row row + boxWidth, row + boxWidth, row];
plot(boxX, boxY, 'ro-');
imageInsideBox = binaryImage(row:row + boxWidth, col:col+boxWidth)
% Determine if there are any pixels set inside that box.
segmentState(6) = any(imageInsideBox(:))

Answers (1)

Walter Roberson
Walter Roberson on 23 Jan 2016
imageInsideBox = binaryImage(row:row + boxWidth, col:col+boxWidth)
is incorrect. It needs to be
imageInsideBox = binaryImage(row:row + boxWidth-1, col:col+boxWidth-1)

Community Treasure Hunt

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

Start Hunting!