MATLAB— RGB图像 ==> Bayer图像 (bggr, gbrg, grbg, rggb)
通过 MATLAB 将 RGB图像 转换成 Bayer图像(四种Bayer pattern:BGGR,GBRG,GRBG,RGGB)
clc;clear;close all
I = imread('lena.jpg');
I_bayer = make_bayer(I,'bggr');
figure;imshow(I_bayer);title('Bayer image');
function [Ibayer] = make_bayer(Irgb,F) % 根据输入的Bayer格式选择相应的函数
if strcmp(F,'bggr')
Ibayer = BGGR(Irgb);
elseif strcmp(F,'gbrg')
Ibayer = GBRG(Irgb);
elseif strcmp(F,'grbg')
Ibayer =GRBG(Irgb);
else strcmp(F,'rggb')
Ibayer = RGGB(Irgb);
end
function[Ibayer] = BGGR(Irgb)
Irgb = double(Irgb)/255;
Ibayer = zero(size(Irgb,1),size(Irgb,2));
Ibayer(1:2:end,1:2:end) = Irgb(1:2:end,1:2:end,3); % copy blue(B)
Ibayer(1:2:end,2:2:end) = Irgb(1:2:end,2:2:end,2); % copy green(G)
Ibayer(2:2:end,1:2:end) = Irgb(2:2:end,1:2:end,2); % copy green(G)
Ibayer(2:2:end,2:2:end) = Irgb(2:2:end,2:2:end,1); % copy red(R)
return
end
function[Ibayer] = GBRG(Irgb)
Irgb = double(Irgb)/255;
Ibayer = zero(size(Irgb,1),size(Irgb,2));
Ibayer(1:2:end,1:2:end) = Irgb(1:2:end,1:2:end,2); % copy green(G)
Ibayer(1:2:end,2:2:end) = Irgb(1:2:end,2:2:end,3); % copy blue(B)
Ibayer(2:2:end,1:2:end) = Irgb(2:2:end,1:2:end,1); % copy red(R)
Ibayer(2:2:end,2:2:end) = Irgb(2:2:end,2:2:end,2); % copy green(G)
return
end
function[Ibayer] = GRBG(Irgb)
Irgb = double(Irgb)/255;
Ibayer = zero(size(Irgb,1),size(Irgb,2));
Ibayer(1:2:end,1:2:end) = Irgb(1:2:end,1:2:end,2); % copy green(G)
Ibayer(1:2:end,2:2:end) = Irgb(1:2:end,2:2:end,1); % copy red(R)
Ibayer(2:2:end,1:2:end) = Irgb(2:2:end,1:2:end,3); % copy blue(B)
Ibayer(2:2:end,2:2:end) = Irgb(2:2:end,2:2:end,2); % copy green(G)
return
end
function[Ibayer] = RGGB(Irgb)
Irgb = double(Irgb)/255;
Ibayer = zero(size(Irgb,1),size(Irgb,2));
Ibayer(1:2:end,1:2:end) = Irgb(1:2:end,1:2:end,1); % copy red(R)
Ibayer(1:2:end,2:2:end) = Irgb(1:2:end,2:2:end,2); % copy green(G)
Ibayer(2:2:end,1:2:end) = Irgb(2:2:end,1:2:end,2); % copy green(G)
Ibayer(2:2:end,2:2:end) = Irgb(2:2:end,2:2:end,3); % copy blue(B)
return
end