IMAGE SEPARATION INTO BLOCKS

HI , i have an 256*256 image .. i want to separate it into blocks of 4*4 of that original image .. for that i tried with this code .
[img_x,img_y]=size(img); block_size=4; slide_len=1; n=1; for ix=block_size/2:slide_len:img_x-block_size/2 for jy=block_size/2:slide_len:img_y-block_size/2 current_block=img((ix-block_size/2+1):(ix+block_size/2),(jy-block_size/2+1):(jy+block_size/2));
end
end
but i could not get the full blocks also when i cheked with original image pixel values differs .. can i use this codes or any in built command can be used .. thanks to all in advance for reading this patiently

2 Comments

Hi you can use this command
c=mat2cell(a,[x y...],[x y...])
where in that x y .. should match with the no. of rows and columns,,,
chek for help in matlab, u wil get more detail
Regards,
Muthu.
i get like this when i use this command
Columns 1 through 9
[4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8]
[4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8]
[4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8]
[4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8]
[4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8]
[4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8]
[4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8]
[4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8] [4x4 uint8]

Sign in to comment.

 Accepted Answer

Dear as in obove code division is n't correct .. follow this code
let say your image is I of 256 x 256. Make your your grid which is 4 x 4 this time should divide image dimensions.
if size(I,3)>1, I = rgb2gray(I); end % to make sure image is grayscale
I = imresize(I, [256 256]); % you can keep it the size you want but should be divisible by 4 x 4.
img_blocks = mat2cell(I,size(I,1)/4*ones(1,4), size(I,2)/4*ones(1,4));
Then you get img_blocks of size 4x4. For first block you can get
part1 = img_blocks{1,1};

6 Comments

clc;
clear all;
[file path]=uigetfile('Input\*.jpg');
filename=strcat(path,file);
I=imread(filename);
img_blocks = mat2cell(I,size(I,1)/4*ones(1,4), size(I,2)/4*ones(1,4));
part1 = img_blocks{1,1}
i run this file .. i get lik this but i want to get separate 4*4 block ....please i am very new to mat lab .. can you give me code clearly.. thank you
Make sure that your image I should be able to divided into 4 x 4 parts. Let say if your image size is 245 x 245 then that could give you error. For save side you always resize your image to desired dimensions. I also assume your image is grayscale image.
Code :
I=imread(filename);
I = imresize( I, [256 256]);
img_blocks = mat2cell(I,size(I,1)/4*ones(1,4), size(I,2)/4*ones(1,4));
thank you for your concern .. i have clarification again ..my image file is 256*256 only... if i run its comin like follows..
[64x64 uint8] [64x64 uint8] [64x64 uint8] [64x64 uint8]
[64x64 uint8] [64x64 uint8] [64x64 uint8] [64x64 uint8]
[64x64 uint8] [64x64 uint8] [64x64 uint8] [64x64 uint8]
[64x64 uint8] [64x64 uint8] [64x64 uint8] [64x64 uint8]
this is the code what you have given to me
clc;
clear all;
[file path]=uigetfile('Input\*.jpg');
filename=strcat(path,file);
I=imread(filename);
img_blocks = mat2cell(I,size(I,1)/4*ones(1,4), size(I,2)/4*ones(1,4));
img_blocks
what i need is the pixel 0f 4*4 should appear in the command window one by one follows.. i am going to proceess these block into my further operation
:- ) yes dear you got 4x 4 blocks in img_blocks. Let say you want to excess first block then
img_blocks{1, 1} and if you want to get the last one it should be img_blocks{4,4}.
Take your time to understand the output. This is exactly what you are required :-)
thank you ..
s = [64 64];
S = size(I);
out = permute(reshape(I,s(1),S(1)/s(1),s(2),[]),[1 3 2 4]);

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Supported Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!