Split RGB Image into blocks (24-bit)

Eg a 630x538 24-bit image
Would be 630x538x3.
How would I:
Chnage dimensions of image so that exactly divisible
Split into blocks of e.g 3x3x3 or 8x8x3 or 16x16x3
And then access each block in turn?

Answers (2)

Image Analyst
Image Analyst on 21 Nov 2020
Try blockproc(). I'm attaching some demos that you can adapt as needed.

8 Comments

Cheers, however the problem with blockproc as I’ve read - unless I’m mistaken - is that it’s multithread therefore there is no order of accessing blocks (I want to be able to manipulate these blocks that I divide into in some kind of sequential order)
I don't know why you need some arbitrary, random order to process the blocks in, but if you do, you'll just have to store the block coordinates in an coordinates array [row1, row2, col1, col2] and then get each block by extracting the region from that array:
% Go down every row of coords getting the coordinates of the rectangle to process.
for row = 1 : size(coords, 1)
row1 = coords(row, 1);
row2 = coords(row, 2);
col1 = coords(row, 3);
col2 = coords(row, 4);
subImage = originalImage(row1:row2, col1:col2, :);
% Now do something with this subImage
end
May I explain to you where I’m heading with this? May we get in touch somehow Srf.13@icloud.com
I need to keep a track on the block position/ block number that I’m working on. On each block I will do some kind of operation. This will determine which block should be accessed next. I’m not necessarily going from block 1 to block2 to block3.... it might be block 1, block 17, block 28 ... following the principle of a knights tour (reference to a flag matrix with the number of blocks)
As my operation should have some kind of reversible outcome I need to be able to backtrack on the order of which blocks are accessed
Yep, my code should work for that. Is there any reason why you cannot put the starting and ending rows and columns of your blocks into an N-by-4 array like that?
May you please get me started. I'm on a complete burn out. Once I'm 'ignited' I can run free. May you please contact me via email.
%% open the image
I = imread('Original.jpg') ;
[n,m] = size(I) ;
%% split into 3 planes
redChannel = I(:, :, 1);
greenChannel = I(:, :, 2);
blueChannel = I(:, :, 3);
%% output functions (S should be a copy of the image, but in blocks)
funr = S(:,:,1);
fung = S(:,:,2);
funb = S(:,:,3);
blockSize = [64 64];
@(block_struct) (block_struct.data) * ones(size(block_struct.data));
%% blocks
blockyImageR = blockproc(redChannel, blockSize, funr)
blockyImageR = blockproc(redChannel, blockSize, fung)
blockyImageR = blockproc(redChannel, blockSize, funb)
Image Analyst
Image Analyst on 22 Nov 2020
Edited: Image Analyst on 22 Nov 2020
If you don't want to scan and process your image in the normal raster-scan style of blockproc(), then exactly what order to you want to do it in? Again, why can't you make up that list of coordinates in advance? Even if the upper left of the block was some kind of knights tour, you could do it. Surely you must know the route or a recipe for building it. If you don't, then what's wrong with a raster scan?
I don't do private consulting via email. I don't have the time and no one could afford me.

Sign in to comment.

Rik
Rik on 21 Nov 2020
The better solution would be to use blockproc, but you can also use mat2cell and use a loop.

2 Comments

Hi Rik, I actually want to use blockproc however I need to keep a track on the block position/ block number that I’m working on. On each block I will do some kind of operation. This will determine which block should be accessed next. I’m not necessarily going from block 1 to block2 to block3.... it might be block 1, block 17, block 28 ... following the principle of a knights tour (reference to a flag matrix with the number of blocks)
If you want a specific order (i.e. the blocks don't have an independent outcome) you can't use blockproc. Otherwise I don't see why splitting into cells will not suit your needs.

Sign in to comment.

Products

Release

R2020b

Asked:

on 21 Nov 2020

Edited:

on 22 Nov 2020

Community Treasure Hunt

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

Start Hunting!