MATLAB Answers

Tia

could you fix these codes?

Asked by Tia
on 14 May 2013
    I=imread('filename'); 
    a1=I(1:8,1:8);
    a2=I(1:8,9:16);
    a3=I(1:8,17:24);
    a4=I(1:8,25:32);
    b1=I(9:16,1:8);
    b2=I(9:16,9:16);
    b3=I(9:16,17:24);
    b4=I(9:16,25:32);
c1=I(17:24,1:8);
c2=I(17:24,9:16);
c3=I(17:24,17:24);
c4=I(17:24,25:32);
d1=I(25:32,1:8);
d2=I(25:32,9:16);
d3=I(25:32,17:24);
d4=I(25:32,25:32);

could you tell me how to make it short? Thank you

  0 Comments

Products

No products are associated with this question.

3 Answers

Answer by Jan Simon
on 14 May 2013
Edited by Jan Simon
on 14 May 2013
 Accepted answer

Don't do this.

Hiding the index in the name of a variable is a bad programming pattern. It is much more efficient to use indices as indices:

img = reshape(a(1:32, 1:32), 8, 4, 8, 4);
img = permute(img, [1,3,2,4]);

Now you have img(:, :, 1, 1) instead of a1.

This is much faster, needs less memory, and allows to expand the method for millions of tiles easily.

  3 Comments

Tia
on 16 May 2013

wow..it's great.it's more efficient. thank you

Image Analyst
on 16 May 2013

You may not even need to do that. It's possible that you do not need to even store the tiles at all. Do you? Why do these tiles need to be stored rather than just operated on and thrown away (reused)? Just operate on one tile at a time if you can - why store forever?

Tia
on 15 Jul 2013

i try it. but it can't be back at 1D.. how do i get it? is it possible? or any ideas how to spilt it into 8x8pixels,but i want to manipulate each 8x8pixels,then store the tiles at all? thank you for your time


Answer by Iman Ansari
on 14 May 2013
Edited by Iman Ansari
on 14 May 2013

Hi. Use cell array:

I=imread('moon.tif');
C=mat2cell(I(1:32,1:32),8*ones(1,4),8*ones(1,4));
imshow(C{5},'InitialMagnification','fit') 

C{5} or C{1,2} is a2 in your code.

  3 Comments

Tia
on 14 May 2013

i'm still confused. actually if i have these codes:

I=imread('filename');
ul=I(1:256,1:256);
ur=I(1:256,257:512);
ll=I(257:512,1:256);
lr=I(257:512,257:512);
s=[ul,lr;ur,lr];
figure,imshow(s)

but if i have 32x32pixels,then i divide it into 8x8blocks.i should to write long codes

    I=imread('filename'); 
    a1=I(1:8,1:8);
    a2=I(1:8,9:16);
    a3=I(1:8,17:24);
    a4=I(1:8,25:32);
    b1=I(9:16,1:8);
    b2=I(9:16,9:16);
    b3=I(9:16,17:24);
    b4=I(9:16,25:32);
c1=I(17:24,1:8);
c2=I(17:24,9:16);
c3=I(17:24,17:24);
c4=I(17:24,25:32);
d1=I(25:32,1:8);
d2=I(25:32,9:16);
d3=I(25:32,17:24);
d4=I(25:32,25:32);
s=[a1,b1,c2,d3;a3,b2,c3,d4;...]

i want to use 'for'. how can i get it? thanks for your fast answer

Iman Ansari
on 14 May 2013

It's not good having variable named a1,a2,..., see this:

http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

I=imread('moon.tif');
C=mat2cell(I(1:32,1:32),8*ones(1,4),8*ones(1,4));
imshow(C{5},'InitialMagnification','fit')

C in this code is:

C={a1,a2,a3,a4;b1,b2,b3,b4;c1,c2,c3,c4;d1,d2,d3,d4}

and for s:

s=[C{1,1} C{2,1} C{3,2} C{4,3};C{1,3} C{2,2} C{3,3} C{4,4};...]
Tia
on 16 May 2013

thank you


Answer by David Sanchez
on 14 May 2013

You can try as well something like this

I=imread(your_32x32_image_name);
division = 8; % set according to yuor needs
L = size(I,1)/division;
subI = cell( L,L );
for row=1:L
    for col=1:L
        subI{row,col} = I( (row:row*division),(col:(col*division)) );
    end
end

  1 Comment

Tia
on 16 May 2013

thank you


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today