i've an image of size 185x256 and i've divided the image into discrete blocks having block rows 16 and block columns 16. when i access the blocks in a loop it says index exceeds matrix dimension...

the code is as follows:
in the following image it has 11 rows and 16 columns grayImage http://imageshack.us/photo/my-images/17/tanklb.jpg/
grayImage=rgb2gray(Imread('tank.jpg'));
whos grayImage
[rows columns numberOfBands]=size(grayImage);
blockSizeR = 16;% Rows in block.
blockSizeC = 16; % Columns in block.
wholeBlockRows = floor(rows / blockSizeR);
wholeBlockCols = floor(columns / blockSizeC);
fprintf('\nSize of whole block is %d %d\n\n',wholeBlockRows,wholeBlockCols);
blockNumber=1;
for row = 1 : blockSizeR : rows
for col = 1 : blockSizeC : columns
row1 = row;
row2 = row1 + blockSizeR - 1;
row2 = min(rows, row2);
% Determine starting and ending columns.
col1 = col;
col2 = col1 + blockSizeC - 1;
col2 = min(columns, col2); % Don't let it go outside the image.
% Extract out the block into a single subimage.
oneBlock = grayImage(row1:row2, col1:col2);
glcm=graycomatrix(oneBlock,'NumLevels',16);
disp(glcm);
subplot(16,16,blockNumber);
imshow(oneBlock);
caption2 = sprintf('Block #%d\n of %d', blockNumber,256);
title(caption2, 'FontSize', fontSize);
%drawnow;
blockNumber = blockNumber + 1;
end
end
i has total of 11 rows and 16 columns i.e 185x256 when i calculate the GLCM
i got the error as
Index exceeds number of subplots.
can u tell me how to solve this error...
which line of code should i add to the above code so that it can work well...
i've to only deal with the size 185x256..i dont want to use the function imresize...

 采纳的回答

Just add
clear
at the very top of the code. Also these issues:
Line 26: change (oneBlocl) to (oneBlock)
Line 30: define the variable (blocks)
Line 31: define (fontSize) in the title argument
perhaps you want to add the command
clc
inside the loops to clean the screen while running (makes run a little bit faster)
After I've made the above minor fixes, it worked fine.

10 个评论

no sir it does not work
u've to change the code so that it stops when it encounter 177...because mygrayImage has the dimension 185x256 i.e it does not have complete 16 rows of block rather less than that....
it works well for blockNumber176 but when it encounter blockNumber177 it needs to be stop because that block is not in my image but it takes as such and shows error
index exceed matrix dimension...
when apply the function imresize it works well but this distort my image i've to deal with the size 185x256
i tried alot to solve this problem but unable to do so.. can u help me in doing so
What subplot(11,16,blockNumber) ? Your code only shows subplot(16,16,blockNumber)
If you have altered your code to use wholeBlockRows then remember that only counts whole rows, and does not take into account that there might be a partial row (as there is in this situation.)
It worked, pammy. I copied the code above and added/corrected what I mentioned, downloaded the image (tank.jpg) - it was tankpl.jpg and I renamed it, and the code worked as far as I'm concerned, I mean it finished running with no errors, and with an output of an image divided into multiple rows and columns.
Anyway, here is the code that worked with my Matlab (2009a):
clc
clear
grayImage=rgb2gray(Imread('tank.jpg'));
whos grayImage; % not needed
[rows columns numberOfBands]=size(grayImage);
blockSizeR = 16;% Rows in block.
blockSizeC = 16; % Columns in block.
wholeBlockRows = floor(rows / blockSizeR);
wholeBlockCols = floor(columns / blockSizeC);
fprintf('\nSize of whole block is %d %d\n\n',wholeBlockRows,wholeBlockCols);
blockNumber=1;
for row = 1 : blockSizeR : rows
for col = 1 : blockSizeC : columns
clc
row1 = row;
row2 = row1 + blockSizeR - 1;
row2 = min(rows, row2);
% Determine starting and ending columns.
col1 = col;
col2 = col1 + blockSizeC - 1;
col2 = min(columns, col2); % Don't let it go outside the image.
% Extract out the block into a single subimage.
oneBlock = grayImage(row1:row2, col1:col2);
glcm=graycomatrix(oneBlock,'NumLevels',16);
disp(glcm);
subplot(16,16,blockNumber);
imshow(oneBlock);
caption2 = sprintf('Block #%d\n of %d', blockNumber);%,blocks);
title(caption2, 'FontSize', 3);% fontSize);
drawnow;
blockNumber = blockNumber + 1;
end
end
The variables were col=241 and row=177 when it finished running. The rest is for you to find out :)
Regards.
i've one more query sir in my other part of the code...
here also the image configuration is same as i told u earlier....
i've used the formula for row start and col start for a corresponding blockNumber
as follows:
rowstart1=mod(((floor((blockNumber1-1)/16))*16+1),256);
colstart1=mod(((mod(blockNumber1,16)-1)*16+1),256);
oneBlock1=grayImage(rowstart1:rowstart1+15,colstart1:colstart1+15);
same problem it encounter here also for blockNumber 177 it shows an error
index exceeds matrix dimension
At line
oneBlock1=grayImage(rowstart1:rowstart1+15,colstart1:colstart1+15);
can u help me in correcting this?
You left out the min() calls that your previous code had, so when you start working on the partial blocks, you run into problems.
Sure Pammy, it is your code and I only tried to slightly modify it for you. Please do not hesitate if you needed further modifications. Regards.
But you used a smart technique in the original code, why not extending it if you want to change the block numbers?
Anyway, I've modified it a little bit further as below (works for any blockSizeR and blockSizeC) as:
... % the above lines are the same.
disp(glcm);
if (blockNumber>blockSizeC*blockSizeR);return;end
subplot(blockSizeC,blockSizeR,blockNumber);
imshow(oneBlock);
... % the rest of the code is the same.
notice the new (if) statement. I've tried the new modifications at
blockSizeR=10 and blockSizeC=16;
blockSizeR=20 and blockSizeC=20;
blockSizeR=11 and blockSizeC=11;
and all worked fine.

请先登录,再进行评论。

更多回答(1 个)

Your code does not define "rows" or "columns"

6 个评论

can u tell me how it could be done by writing in terms of code sir.
[rows columns numberOfBands]=size(grayImage);
this i specified above... again i got the same error.. help me in rectifying my error...
At the command prompt give the command
dbstop if error
and then run your program. When it stops with the error, please show us which line it is stopping on, and a copy of the error message, and please show the values of row, col, and blockNumber
it works for upto blockNumber176. after the blockNumber176 i.e from block number177,178,179, and so on..it does not work...
for block number177 the rowstart is 177 and the column start is 1
sir because my image is 185x256 thats why it shows error
error in subplot(11,16,blockNumber);
when i use the function imresize and set the size to [256,256] it works well but this i dont want..
i want it to deal with the exact size of the image i.e185x256
What subplot(11,16,blockNumber) ? Your code only shows subplot(16,16,blockNumber)
If you have altered your code to use wholeBlockRows then remember that only counts whole rows, and does not take into account that there might be a partial row (as there is in this situation.)
sir subplot(16,16,blockNumber) when i m resizing my image and
subplot(11,16,blockNumber) when i m not resizing it...
@Ahmed A. Selman gave me a solution...his solution works well only when each row has 16 columns but what if each rows has less than 16 columns
that is;
as my grayImage is 185x256 then it has wholeBlockRows 12 and wholeBlockCols 16, the solution given to me works well for this but when the image size 185x190 then it does not work
tell me the solution of it sir....
subplotrows = ceil(rows / blockSizeR);
subplotcols = ceil(columns / blockSizeC);
then use
subplot(subplotrows, subplotcols);

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by