0

Matlab - Hello, I want to combine two binary images with same size (111x111), but first i want to divide the image into 3 x 3 matrix patch (37 sub matrix), with the two conditions:

1.If the 3 x 3 patches from image 2 matrix values is all white (1) then the result matrix = image 1 matrix , example:

 image 1 patch:           image 2 patch:             result:
 1 1 0                       1 1 1                    1 1 0   
 1 0 1                       1 1 1                    1 0 1
 1 1 1                       1 1 1                    1 1 1

2. Else, i want to keep the center value of 3 x 3 patches (index (2,2)) from image 1, but the other value from image 2

image 1 patch:             Image 2 patch :            result:
 0 0 0                       1 0 1                    1 0 1   
 0 0 0                       1 1 0                    1 0 0
 0 0 0                       1 0 1                    1 0 1

And do the whole image and combine the whole 3 x 3 patches into result image (111x111 again)

My Code so far (Using mat2cell):

clear;
clc;
I1 = imread('image1.bmp');
I2 = imread('image2.bmp');
TI1 = im2bw(I1); %Thresholding I1
TI2 = im2bw(I2); %Thresholding I2

%Mat2cell patch
cellTI1 = mat2cell(TI1, 3*ones(size(TI1,1)/3,1), 3*ones(size(TI1,2)/3,1))
cellTI2= mat2cell(TI2, 3*ones(size(TI2,1)/3,1), 3*ones(size(TI2,2)/3,1))

% Im Confused with the loop

result1 = ones(37,37);

for i=1:3
    for j=1:3
        for m=1:37
            for n=1:37
        if TI2{m,n} == [1 1 1;
                 1 1 1;
                 1 1 1]
             result1 = TI1(m,n);
        else
             result1 = [TI2{1,1}(1,1) TI2{1,1}(1,2) TI2{1,1}(1,3);
                        TI2{1,1}(2,1) TI1{1,1}(2,2) TI2{1,1}(3,2);
                        TI2{1,1}(3,1) TI2{1,1}(3,2) TI2{1,1}(3,3)];
        end
            end
        end

Sorry for my bad English, Thanks

stevz
  • 1
  • 1
    Perhaps you could use [blockproc](http://www.mathworks.com/help/images/ref/blockproc.html). – Rafael Monteiro Apr 08 '16 at 18:33
  • Thanks for the answer, but how how to retrieve a value (center pixel from 3 x 3) from a particular index using blockproc. – stevz Apr 09 '16 at 10:40
  • I've never used this function, just found it on Google. Did you read the docs? I've read it and I think you could do something like calling the function with `blockSize = [3 3]`, and your function (3rd parameter) could be something like `@(x) x.data(2,2)`. With this you should be able to access the central pixel from each 3x3 block. I'm basing on the docs. I suggest you read the full documentation to see if it suits your needs. – Rafael Monteiro Apr 09 '16 at 16:02
  • Where i can get the documentation blockproc function paramater like `@(x) x.data(2,2)` and other function to retrive values from the submatrix/block , Thanks – stevz Apr 09 '16 at 17:12
  • I've posted the documentation link on my first comment. – Rafael Monteiro Apr 09 '16 at 21:50
  • Thanks, but i think `mat2cell` more suitable for my project , i made a progress with `mat2cell`, but im tried figure how to repalce cell index (2,2) from image 1 with cell index (2,2) from image 2 using `mat2cell` – stevz Apr 10 '16 at 09:36

0 Answers0