Your Image1 has been produced by im2double() so the range of Image1 is 0 to 1. But your second set of code has
Green = Image1 (i, j, 2) + 20;
so Green is going to be in the range (20+0) to (20+1), which is always going to be greater than the 0 to 1 entries already in Image1, so your "if" is always going to be true.
Note that your second block of code, involving "Green" is going to completely overwrite the output of the first block of code. Your first block might as well not be there.
Your first block could be rewritten without loops as
mask = Image1(:,:,2) > Image1(:,:,1) & Image1(:,:,2) > Image1(:,:,3);
Image2 = Image1 .* mask(:,:,[1 1 1]);
and your second block could be rewritten without loops as
mask = Image1(:,:,2) + 20 > Image1(:,:,1) & Image1(:,:,2) + 20 > Image1(:,:,3);
Image2 = Image1 .* mask(:,:,[1 1 1]);
Best Answer