结构光编解码—正反格雷码解码代码

        本篇文章主要给出正反格雷码的解码代码,鉴于自身水平所限,如有错误,欢迎批评指正。(欢迎进Q群交流:874653199)

clc;
clear;

s1='.\graycode\02\';
s2='.bmp';

image_num=24;

vertical_bit=11;

tempImage=imread([s1 num2str(1) s2]);

[rows,cols]=size(tempImage);

for i=1:image_num
    filename=[s1 num2str(i) s2];
    srcImage(:,:,i)=imread(filename);
end

%% 计算mask
white=srcImage(:,:,1);
black=srcImage(:,:,2);

low=1;
high=255;

mask=ones(rows,cols);

indexB=find(black<low);
mask(indexB)=0;
indexW=find(white>high);
mask(indexW)=0;

vertical_image=srcImage(:,:,3:24);

vertical_decodeMat=decode(vertical_image,mask,vertical_bit);


figure
imshow(vertical_decodeMat,[])

%% 正反格雷码解码
function decodeMatrix=decode(srcImage,mask,bit)

[direct,indirect]=estimate_light(srcImage,mask);

[rows,cols]=size(srcImage(:,:,1));

graycodeMat=zeros(rows,cols,bit);

for i=1:bit    
graycodeMat(:,:,i)=robust_bit(srcImage(:,:,2*i-1),srcImage(:,:,2*i),direct,indirect);
end

binarys=zeros(rows,cols,bit);
binarys(:,:,1)=graycodeMat(:,:,1);
for i=2:bit    
 binarys(:,:,i)= xor(binarys(:,:,i-1),graycodeMat(:,:,i));
end

decodeMatrix=zeros(rows,cols);
for i=1:bit
    decodeMatrix=decodeMatrix+2^(bit-i)*binarys(:,:,i);
end
index=find(decodeMatrix<0);
decodeMatrix(index)=-10;
end

%% srcImage包含所有竖条纹格雷码图像或者横条纹,不应包含黑白图
function [direct,indirect]=estimate_light(srcImages,mask)

b_=0.5;
b1 = 1.0 / (1.0 - b_);
b2 = 2.0 / (1.0 - b_ * 1.0 * b_);

[rows,cols,num]=size(srcImages);

direct=zeros(rows,cols);
indirect=zeros(rows,cols);
  
for m=1:rows
    
   for n=1:cols 
       
    if(mask(m,n) ~=0)
      light_max=srcImages(m,n,1);
      light_min=light_max;
   
       for k=1:num
         if(srcImages(m,n,k)>light_max)
           light_max= srcImages(m,n,k); 
         end
         
         if(srcImages(m,n,k)<light_min)
           light_min= srcImages(m,n,k); 
         end
            
       end
       
    direct_light =round( b1 * (light_max - light_min) );
    global_light = round(b2 * (light_min - b_ * light_max) );
    
    if(global_light > 0)
     direct(m,n) =direct_light;
     indirect(m,n) =global_light;
    else
     direct(m,n) = light_max;
     indirect(m,n) =0;  
    end
    
    end
  
       
    end
        
end

end


%% 鲁棒分类,输入正反码图像以及直接分量和间接分量
function binMatrix=robust_bit(normal, inverted,direct_light,global_light)

[rows,cols]=size(normal);

binMatrix=zeros(size(normal));

m_=1;

for i=1:rows
    for j=1:cols
        
        if (direct_light(i,j) < m_)
        binMatrix(i,j)=-10;
        continue;
        end
        
        if (direct_light(i,j) > global_light(i,j)) 
            if(normal(i,j) > inverted(i,j))
                binMatrix(i,j) = 1;   
            else
                binMatrix(i,j) = 0; 
            end      
             continue;
        end
        
        if ((normal(i,j) <= direct_light(i,j)) && (inverted(i,j) > global_light(i,j))) 
            binMatrix(i,j) = 0;
            continue;
        end
        
        if ((normal(i,j) >= global_light(i,j)) && (inverted(i,j) <= direct_light(i,j))) 
            binMatrix(i,j)= 1;
            continue;
        end
        
        
    end
end


end

原始数据:

解码结果:

去噪及重建结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值