rockinfresh
rockinfresh

Reputation: 2106

Partition an image into 8 rows via Matlab, not all partitions shown

I wish to ask if anybody out there knows how to partition an image into 8 different rows and 1 column? I have tried using mat2cell() and using the demo on their wiki as a reference, I tried partitioning the image into 8 rows, however not all image partition rows are displayed.

If you see the image below, 2, 4, 6, 8 is not displayed. I am also not sure why is it of 16 blocks.

enter image description here

Can somebody help me check my code? I am not really used to the MatLab syntax and language. I trying my best to understand now.

My code for splitting the blocks are as follows:

blockSizeR = 50; % Rows in block.
blockSizeC = 512; % Columns in block.
wholeBlockRows = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, wholeBlockRows), rem(rows, blockSizeR)];
wholeBlockCols = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, wholeBlockCols), rem(columns, blockSizeC)];

if numberOfColorBands > 1
% It's a color image.
ca = mat2cell(rgbImage, blockVectorR, blockVectorC, numberOfColorBands);
else
ca = mat2cell(rgbImage, blockVectorR, blockVectorC);
end
% Now display all the blocks.
plotIndex = 1;
numPlotsR = size(ca, 1);
numPlotsC = size(ca, 2);
for r = 1 : numPlotsR
for c = 1 : numPlotsC
    fprintf('plotindex = %d,   c=%d, r=%d\n', plotIndex, c, r);
    % Specify the location for display of the image.
    subplot(numPlotsR, 1, plotIndex);
    % Extract the numerical array out of the cell
    % just for tutorial purposes.
    rgbBlock = ca{r,c};
    imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
    [rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
    % Make the caption the block number.
    caption = sprintf('Block #%d of %d\n%d rows by %d columns', ...
        plotIndex, numPlotsR*numPlotsC, rowsB, columnsB);
    title(caption);
    drawnow;
    % Increment the subplot to the next location.
    plotIndex = plotIndex + 1;
    end
 end

I am new to MatLab, so is there is a simpler method to do this that I missed out, please do suggest or better still, if there are references that I can refer to. Many thanks (:

Upvotes: 0

Views: 751

Answers (2)

rockinfresh
rockinfresh

Reputation: 2106

I managed to solve already, the error lies in the for loop. I changed the for r = 1 : numPlotsR into r = 1 : (number of rows I want) for c = 1 : numPlotsC into c= 1: 1(as I only want one column), and used subplot(8,1,k) or (8,2,k) where k is the plot index. Just answering this in case anybody encounter such problem in future and want to use my code as a reference. Cheers!

Upvotes: 0

Frederick
Frederick

Reputation: 1301

If you know the dimensions of your matrix, you can do the math to figure out how to divide the number of rows into 4 equal parts:

e.g. If: size(rockinsMatrix) == [10 20] (a 10row x 20column) matrix, then you could split it into a set of 4 sub-matrices, two with 3 rows, and 2 with 2 columns.

If you want the matrices in a cell array then you can do that at that time.

Upvotes: 1

Related Questions