Skip to content

Commit

Permalink
add code of demosaic
Browse files Browse the repository at this point in the history
  • Loading branch information
wtzhu13 committed Jul 5, 2021
1 parent 36956f1 commit 633175c
Show file tree
Hide file tree
Showing 27 changed files with 393 additions and 0 deletions.
Binary file added Demosaic/images/RGBTest.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Demosaic/images/RGBTest_8bits_RGGB.raw

Large diffs are not rendered by default.

Binary file added Demosaic/images/colorTest.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
213 changes: 213 additions & 0 deletions Demosaic/images/colorTest_8bits_RGGB.raw

Large diffs are not rendered by default.

Binary file added Demosaic/sketchMap.xlsx
Binary file not shown.
58 changes: 58 additions & 0 deletions Demosaic/src/RGB2Raw.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function rawData = RGB2Raw(path, BayerFormat)
% RGB2Raw.m get rawData from HiRawImage
% Input:
% path the path of orgImage
% BayerFormat the format of raw, eg. 'RGGB','GRBG', 'GBRG'
% Output:
% rawData the matrix of raw image data
% Instructions:
% author: wtzhu
% e-mail: [email protected]
% Last Modified by wtzhu v1.0 2021-07-05
% Note:
orgImg = imread(path);
[height, width, channel] = size(orgImg);
if channel ~= 3
fprintf('please input RGB format');
return;
end
rawImg = zeros(height, width);

switch BayerFormat
case 'RGGB'
% R
rawImg(1:2:end, 1:2:end) = orgImg(1:2:end, 1:2:end, 1);
% B
rawImg(2:2:end, 2:2:end) = orgImg(2:2:end, 2:2:end, 3);
% G
rawImg(2:2:end, 1:2:end) = orgImg(2:2:end, 1:2:end, 2);
rawImg(1:2:end, 2:2:end) = orgImg(1:2:end, 2:2:end, 2);
case 'GRBG'
% R
rawImg(1:2:end, 2:2:end) = orgImg(1:2:end, 2:2:end, 1);
% B
rawImg(2:2:end, 1:2:end) = orgImg(2:2:end, 1:2:end, 3);
% G
rawImg(1:2:end, 1:2:end) = orgImg(1:2:end, 1:2:end, 2);
rawImg(2:2:end, 2:2:end) = orgImg(2:2:end, 2:2:end, 2);
case 'GBRG'
% R
rawImg(2:2:end, 1:2:end) = orgImg(2:2:end, 1:2:end, 1);
% B
rawImg(1:2:end, 2:2:end) = orgImg(1:2:end, 2:2:end, 3);
% G
rawImg(1:2:end, 1:2:end) = orgImg(1:2:end, 1:2:end, 2);
rawImg(2:2:end, 2:2:end) = orgImg(2:2:end, 2:2:end, 2);
end
rawData = rawImg;
% save rawData
nameList = strsplit(path, '.');
fileName = char(cellstr(nameList(1)));
disp(fileName);
rawName = sprintf('%s_8bits_%s.raw', fileName, BayerFormat);
fb = fopen(rawName, 'wb');
rawImg = rawImg';
rawList = rawImg(:);
fwrite(fb, rawList, 'uint8');
fclose(fb);
end
82 changes: 82 additions & 0 deletions Demosaic/src/demosaic.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
%% --------------------------------
%% author:wtzhu
%% date: 20210705
%% fuction: main file of Demosaic
%% note: add RGGB format only, other formats will be added later
%% --------------------------------
clc;clear;close all;

% ------------Raw Format----------------
filePath = 'images/RGBTest_8bits_RGGB.raw';
bayerFormat = 'RGGB';
width = 640;
height= 480;
bits = 8;
% --------------------------------------
bayerData = readRaw(filePath, bits, width, height);
figure();
imshow(bayerData);
title('raw image');

% expand image inorder to make it easy to calculate edge pixels
bayerPadding = zeros(height + 2,width+2);
bayerPadding(2:height+1,2:width+1) = uint32(bayerData);
bayerPadding(1,:) = bayerPadding(3,:);
bayerPadding(height+2,:) = bayerPadding(height,:);
bayerPadding(:,1) = bayerPadding(:,3);
bayerPadding(:,width+2) = bayerPadding(:,width);

imDst = zeros(height+2, width+2, 3);
for ver = 2:height + 1
for hor = 2:width + 1
switch bayerFormat
case 'RGGB'
% G B -> R
if(0 == mod(ver, 2) && 0 == mod(hor, 2))
imDst(ver, hor, 1) = bayerPadding(ver, hor);
% G -> R
imDst(ver, hor, 2) = (bayerPadding(ver-1, hor) + bayerPadding(ver+1, hor) +...
bayerPadding(ver, hor-1) + bayerPadding(ver, hor+1))/4;
% B -> R
imDst(ver, hor, 3) = (bayerPadding(ver-1, hor-1) + bayerPadding(ver-1, hor+1) + ...
bayerPadding(ver+1, hor-1) + bayerPadding(ver+1, hor+1))/4;
% G R -> B
elseif (1 == mod(ver, 2) && 1 == mod(hor, 2))
imDst(ver, hor, 3) = bayerPadding(ver, hor);
% G -> B
imDst(ver, hor, 2) = (bayerPadding(ver-1, hor) + bayerPadding(ver+1, hor) +...
bayerPadding(ver, hor-1) + bayerPadding(ver, hor+1))/4;
% R -> B
imDst(ver, hor, 1) = (bayerPadding(ver-1, hor-1) + bayerPadding(ver-1, hor+1) + ...
bayerPadding(ver+1, hor-1) + bayerPadding(ver+1, hor+1))/4;
elseif(0 == mod(ver, 2) && 1 == mod(hor, 2))
imDst(ver, hor, 2) = bayerPadding(ver, hor);
% R -> Gr
imDst(ver, hor, 1) = (bayerPadding(ver, hor-1) + bayerPadding(ver, hor+1))/2;
% B -> Gr
imDst(ver, hor, 3) = (bayerPadding(ver-1, hor) + bayerPadding(ver+1, hor))/2;
elseif(1 == mod(ver, 2) && 0 == mod(hor, 2))
imDst(ver, hor, 2) = bayerPadding(ver, hor);
% B -> Gb
imDst(ver, hor, 3) = (bayerPadding(ver, hor-1) + bayerPadding(ver, hor+1))/2;
% R -> Gb
imDst(ver, hor, 1) = (bayerPadding(ver-1, hor) + bayerPadding(ver+1, hor))/2;
end
case 'GRBG'
continue;
case 'GBGR'
continue;
case 'BGGR'
continue;
end
end
end
imDst = uint8(imDst(2:height+1,2:width+1,:));
figure,imshow(imDst);title('demosaic image');

orgImage = imread('images/RGBTest.jpg');
figure, imshow(orgImage);title('org image');




39 changes: 39 additions & 0 deletions Demosaic/src/readRaw.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function rawData = readRaw(fileName, bitsNum, width, height)
% readRaw.m get rawData from HiRawImage
% Input:
% fileName the path of HiRawImage
% bitsNum the number of bits of raw image
% row the row of the raw image
% col the column of the raw image
% Output:
% rawData the matrix of raw image data
% Instructions:
% author: wtzhu
% e-mail: [email protected]
% Last Modified by wtzhu v1.0 2021-06-29
% Note:

% get fileID
fin = fopen(fileName, 'r');
% format precision
switch bitsNum
case 8
disp('bits: 8');
format = sprintf('uint8=>uint8');
case 10
disp('bits: 10');
format = sprintf('uint16=>uint16');
case 12
disp('bits: 12');
format = sprintf('uint16=>uint16');
case 16
disp('bits: 16');
format = sprintf('uint16=>uint16');
end
I = fread(fin, width*height, format);
% plot(I, '.');
z = reshape(I, width, height);
z = z';
rawData = z;
% imshow(z);
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.

0 comments on commit 633175c

Please sign in to comment.