0

I am trying to write a matlab code which copies one segment of an image into another with a particular range matrices. My code works as required. The only problem I am having is that I want to assign 255 value to copied part of image so that the image appears on white background rather than black background

a=imread('/Applications/MATLAB_R2015a.app/toolbox/images/imdata/cameraman.tif');
    a=double(a);
    b=zeros(256,256);
    for i =0:1:255
        for j=0:1:255
        if((i>=97 && i<=150)&&(j>=34 && j<=81))
        b(j,i)=a(j,i);
    %    else
    %         b(j,i)=255;
        end
        end
    end
    imshow(a,[]);
    figure,imshow(b,[]);
    imageSegmenter(b);
Ahmed Rik
  • 58
  • 8

1 Answers1

1

Instead of initializing your matrix to zeros simply initialize it to 255.

b = 255 + zeros(256, 256);

As a side-note, MATLAB uses 1-based indexing so you should change your for loop indices to reflect that:

for i = 1:size(b,2)
    for j = 1:size(b, 1)
        % Do stuff
    end
end

Better yet, you can completely remove the for loop.

b = 255 + zeros(256, 256);
b(34:81, 97:150) = a;
Suever
  • 64,497
  • 14
  • 82
  • 101
  • When I am trying to remove for loop and in place of hard coded values I am trying to take values into variables x1, x2, y1, y2 and put in I am getting error. b(x1:x2,y1:y2) = a; – Ahmed Rik May 22 '16 at 17:54