DPC(Defect Pixel Correction)——坏点检测

产生原因

image

校正方法

imageimage

代码

PINTO算法
readRAW

function rawData = readRaw(fileName, bitsNum, row, col)
% 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:     wtzhu_13@163.com
% 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, row*col, format);
% plot(I, '.');
z = reshape(I, row, col);
z = z';
rawData = z;
% imshow(z);
end

judgeDefectPixel

function correctP = judgeDefectPixel(aroundP, currentP, Th)
% judgeDefectPixel.m    correct the curren pixel
%   Input:
%       aroundP    	the pixel around the current pixel 
%       currentP    the value of current pixel
%       Th          the threshold of the defect pixel
%   Output:
%       correctP    the corrected value of the pixel
%   Instructions:
%       author:     wtzhu
%       e-mail:     wtzhu_13@163.com
% Last Modified by wtzhu v1.0 2021-07-16
% Note: 
    % get the median value of the around list
    medianV = median(aroundP);
    % get the difference between the around pixel and the current pixel
    diff = aroundP - ones(1, numel(aroundP)) * currentP;
    % if all difference bigger than 0 or all smaller than 0 and all abs of the diff are bigger than Th, that pixel is
    % a defect pixel and replace it with the median;
    if (nnz(diff > 0) ==  numel(aroundP)) || (nnz(diff < 0) ==  numel(aroundP))
        if length(find((abs(diff)>Th)==1)) == numel(aroundP)
            correctP = medianV;
        else
            correctP = currentP;
        end
    else
        correctP = currentP;
    end
end

expanRAW

function imgExpand = expandRaw(img, expandNum)
if mod(expandNum, 2) ~= 0
    disp('expandNum must be an even number!')
    return
end
[height, width] = size(img);
imgExpand = zeros(height+expandNum*2, width+expandNum*2);
imgExpand(expandNum+1:height+expandNum, expandNum+1:width+expandNum) = img(:,:);
imgExpand(1:expandNum, expandNum+1:width+expandNum) = img(1:expandNum,:);
imgExpand(height+expandNum+1:height+expandNum*2, expandNum+1:width+expandNum) = img(height-expandNum+1:height,:);
imgExpand(:,1:expandNum) = imgExpand(:, expandNum+1:2*expandNum);
imgExpand(:,width+expandNum+1:width+2*expandNum) = imgExpand(:, width+1:width+expandNum);
end

photo

clc;clear;close all;
tic;
% --------global velue---------
expandNum = 2;
Th = 30;

% --------raw parameters-------
filePath = 'images/HisiRAW_4208x3120_8bits_RGGB.raw';
bayerFormat = 'RGGB';
bayerBits = 8;
row = 4208;
col = 3120;
% -----------------------------

rawData = readRaw(filePath, bayerBits, row, col);
[height, width, channel] = size(rawData);

img_expand = expandRaw(rawData, expandNum);

disImg = zeros(height, width);
for i = expandNum+1 : 2 : height+expandNum
    for j = expandNum+1 : 2 : width+expandNum
        % R
        % get the pixel around the current R pixel
        around_R_pixel = [img_expand(i-2, j-2) img_expand(i-2, j) img_expand(i-2, j+2) img_expand(i, j-2) img_expand(i, j+2) img_expand(i+2, j-2) img_expand(i+2, j) img_expand(i+2, j+2)];
        disImg(i-expandNum, j-expandNum) = judgeDefectPixel(around_R_pixel, img_expand(i, j), Th);
        % Gr
        % get the pixel around the current Gr pixel
        around_Gr_pixel = [img_expand(i-1, j) img_expand(i-2, j+1) img_expand(i-1, j+2)  img_expand(i, j-1) img_expand(i, j+3) img_expand(i+1, j) img_expand(i+2, j+1) img_expand(i+1, j+2)];
        disImg(i-expandNum, j-expandNum+1) = judgeDefectPixel(around_Gr_pixel, img_expand(i, j+1), Th);
        % B
        % get the pixel around the current B pixel
        around_B_pixel = [img_expand(i-1, j-1) img_expand(i-1, j+1) img_expand(i-1, j+3) img_expand(i+1, j-1) img_expand(i+1, j+3) img_expand(i+3, j-1) img_expand(i+3, j+1) img_expand(i+3, j+3)];
        disImg(i-expandNum+1, j-expandNum+1) = judgeDefectPixel(around_B_pixel, img_expand(i+1, j+1), Th);
        % Gb
        % get the pixel around the current Gb pixel
        around_Gb_pixel = [img_expand(i, j-1) img_expand(i-1, j) img_expand(i, j+1) img_expand(i+1, j-2) img_expand(i+1, j+2) img_expand(i+2, j-1) img_expand(i+3, j) img_expand(i+2, j+1)];
        disImg(i-expandNum+1, j-expandNum) = judgeDefectPixel(around_Gb_pixel, img_expand(i+1, j), Th);
    end
end
figure();
imshow(rawData);title('org');
figure();
imshow(uint8(disImg));title('corrected');
disp(['cost time��',num2str(toc)])
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值