在matlab中实现demosaic功能

时间:2016-11-01 18:02:40

标签: matlab image-processing

我有一个矩阵文件,这是Bayer过滤器更改的照片。当我使用RGGB传感器对齐使用去马赛克功能时,它会变成原始图片。 我尝试通过将每个不可用像素颜色周围的相同颜色像素的平均值作为其值来实现去马赛克功能,但它给出的答案不是原始照片。你能告诉我我做错了吗?

clear;
clc;

I = struct2cell(load('Bayer.mat'));
J = I{1,1};
[M N] = size(J);
T = uint8(zeros(M,N,3));

for i = 2:M-1
    for j = 2:N-1
        if mod(i,2) ~= mod(j,2) %G
            T(i,j,1)=round((J(i,j-1)+J(i,j+1))/2);
            T(i,j,2)=round(J(i,j));
            T(i,j,3)=round((J(i-1,j)+J(i+1,j))/2);
        elseif mod(i,2) == 1 %R
            T(i,j,1)=round(J(i,j));
            T(i,j,2)=round((J(i-1,j)+J(i+1,j)+J(i,j-1)+J(i,j+1))/4);
            T(i,j,3)=round((J(i-1,j-1)+J(i+1,j-1)+J(i+1,j-1)+J(i-1,j+1))/4);
        else %B
            T(i,j,1)=round((J(i-1,j-1)+J(i+1,j-1)+J(i+1,j-1)+J(i-1,j+1))/4);
            T(i,j,2)=round((J(i-1,j)+J(i+1,j)+J(i,j-1)+J(i,j+1))/4);
            T(i,j,3)=round(J(i,j));
        end
    end
end
%K = demosaic(J,'rggb');
imwrite(J,'im0.jpg');
imwrite(T,'im05.jpg');

1 个答案:

答案 0 :(得分:3)

处理绿色通道时出错。您应该分别考虑左下角和右上角绿色,因为它的邻居会针对每种情况进行更改。以下是代码:

=IFERROR(INDEX(A1:F1;1;MATCH("*JOB-20*";A1:F1;0)+1);"")

此外,如果您的图片I = imread('lena.bmp'); [M,N,L] = size(I); J = zeros(M,N); R = I(:,:,1); G = I(:,:,2); B = I(:,:,3); J(1:2:M,1:2:N) = R(1:2:M,1:2:N); J(2:2:M,2:2:N) = B(2:2:M,2:2:N); J(J==0) = G(J==0); T = zeros(M,N,3); figure,imshow(uint8(J)); %% Reconstruct Bayer Filtered Image here for i = 2:M-1 for j = 2:N-1 if mod(i,2) == 0 && mod(j,2) == 1 %G T(i,j,1)=round((J(i-1,j)+J(i+1,j))/2); T(i,j,2)=round(J(i,j)); T(i,j,3)=round((J(i,j-1)+J(i,j+1))/2); elseif mod(i,2) == 1 && mod(j,2) == 0 T(i,j,1)=round((J(i,j-1)+J(i,j+1))/2); T(i,j,2)=round(J(i,j)); T(i,j,3)=round((J(i-1,j)+J(i+1,j))/2); elseif mod(i,2) == 1 %R T(i,j,1)=round(J(i,j)); T(i,j,2)=round((J(i-1,j)+J(i+1,j)+J(i,j-1)+J(i,j+1))/4); T(i,j,3)=round((J(i-1,j-1)+J(i+1,j-1)+J(i+1,j-1)+J(i-1,j+1))/4); else %B T(i,j,1)=round((J(i-1,j-1)+J(i+1,j-1)+J(i+1,j-1)+J(i-1,j+1))/4); T(i,j,2)=round((J(i-1,j)+J(i+1,j)+J(i,j-1)+J(i,j+1))/4); T(i,j,3)=round(J(i,j)); end end end 定义为J,则应将其设为uint8,因为如果像素总和超过255,double将会溢出。