1

I have an image size 128*128, I divided into 32*32 non-overlapping blocks, the problem is that I want to change the values of a specific or choosing block (the first or the 3rd.. for example) and getting a block with new values then replace it in my image. Do you have any idea how to get an image with ONE block modified ( NOT all of them)? Thank you This is an example with a small matrix

    %*********************
    A=magic(6)  ;      %%%%% matrix size 6*6
    B=Block(A,2,2) ;    % divide the matrix into 2*2 non-overlapping blocks
    subblock=B{3}  ;    % Choose the 3rd block
    new_block= 2*subblock; % change the block values by multipliying by 2

This is what I get

A = 35     1     6    26    19    24
     3    32     7    21    23    25
    31     9     2    22    27    20
     8    28    33    17    10    15
    30     5    34    12    14    16
     4    36    29    13    18    11

I extract the 3rd block

  sub_block=19    24
            23    25

Now I multiplied by 2 :

    new_block=  38    48
                46    50

This is my Block function:

    function A=Block(IM,p,q)  
     p=3;
     q=3;
     [m,n] = size(IM);
     IJ = zeros(p,q);
     z = 1;
     for m1 = 1:m/p   
     for n1 = 1:n/q  
     if m1*p <= m;
     if n1*q <= n;

      for i = (m1-1)*p+1:m1*p
         for j = (n1-1)*q+1:n1*q
             IJ(i-(m1-1)*p,j-(n1-1)*q) = IM(i,j);
           if (i-(m1-1)*p)==p&&(j-(n1-1)*q)==q;
               OUT = IJ;
              A{1,z} = OUT;
              z = z+1;
           end
         end
      end
    end
   end
end

end I want to replace these values in matrix A , but depending on block number. How can I do it?

Youssi
  • 63
  • 8
  • Is `block` from a toolbox or is it your own function? – Dan May 27 '15 at 10:46
  • possible duplicate of http://stackoverflow.com/questions/20336288/for-loop-to-split-matrix-to-equal-sized-sub-matrices and http://stackoverflow.com/questions/12554522/divide-a-matrix-into-submatrices-in-matlab – GameOfThrows May 27 '15 at 10:53
  • Actualy 'Block' is my own function – Youssi May 27 '15 at 10:54
  • @Youssi then you *MUST* post that function's code in your question! – Dan May 27 '15 at 10:59
  • @GameOfThrows I don't think it's a duplicate (of those 2 Qs) because the OP has already split the matrix into blocks, but is struggling to manipulate a block and then recombine all the blocks into a single matrix again – Dan May 27 '15 at 11:00
  • 1
    I edit the post, and I put the 'Block' function – Youssi May 27 '15 at 11:07

2 Answers2

1

Just type in the rows and cols you want to access, for your example

A(1:2,5:6)=A(1:2,5:6)*2

more generic, where n is the nth column block and m is the mth row block and c is the block width and r is the block height (in your example, n = 3, m = 1, r=c=2)

A(((m-1)*r+1):(m*r), ((n-1)*c+1):(n*c)) = any block of size(r,c)
rst
  • 2,510
  • 4
  • 21
  • 47
1

I don't know about your Block function, you don't actually need to convert to a cell matrix but if you do want to then I would do this:

A = magic(6);

[m,n] = size(A);
r=2; %// Desired number rows of blocks, m must be a multiple of r
c=2; %// Desired number cols of blocks, n must be a multiple of c

%// Create blocks (but as a 2D grid rather than a list)
B = mat2cell(A,r*ones(m/r,1), c*ones(n/c,1))

%// Manipulate a block
B(1,3) = {2*B{1,3}};

%// Convert back to a matrix
cell2mat(B)

I think that RobertStettler's answer is the better way to go though, if this is all you are trying to do

Community
  • 1
  • 1
Dan
  • 45,079
  • 17
  • 88
  • 157
  • yeap ^^ Exactly. Thank you – Youssi May 27 '15 at 11:12
  • You'd have to adapt it. The simplest would be just `B = mat2cell(A,r*ones(m/r,1), c*ones(n/c,1),[1,1,1])` assuming you want to always apply the same function to each channel. – Dan May 27 '15 at 11:15