% 此文件用于将蓝色底的寸照的背面变成白色的
clear; close all;
I=imread('D:\BaiduNetdiskDownload\jiajia_20180405.jpg');
r_layer = double(I(:,:,1));
g_layer = double(I(:,:,2));
b_layer = double(I(:,:,3));
close all;

which_layer = g_layer; % 要根据原图片的背景颜色修改修改此处。如果原背景是蓝色，则要将r_layer改成b_layer
threshold = 220;       % 要根据原图片实际情况修改这个阈值

[Left,Top] = size(which_layer);
index = zeros(Left,Top);
setToWhite = ones(Left,Top);

index = which_layer > threshold;
j_left = Top;
flag_right = 1;

for i = 1:Left
    
    for j = 1:Top
        if index(i,j) == 0
            j_left = j;
            break;
        end
    end
    
    for j = Top:(-1):1
        if index(i,j) == 0
            flag_right = j;
            break;
        end
    end
    
    if j_left <= flag_right
        setToWhite(i,j_left:flag_right) = 0;
    end
end
setToWhite = logical(setToWhite);

r_layer(setToWhite) = 255;
g_layer(setToWhite) = 255;
b_layer(setToWhite) = 255;
I_change = I;
I_change(:,:,1) = uint8(r_layer);
I_change(:,:,2) = uint8(g_layer);
I_change(:,:,3) = uint8(b_layer);
figure,imshow(I);
figure,imshow(I_change);

b_layer_change = b_layer;
b_layer_change(setToWhite) = 255;
figure(4), imshow(uint8(b_layer_change));
imwrite(I_change,'new.jpg');
