matlab 高斯核使用,超像素图像模拟,矩阵转图像,深度相机模型实践实现 ———— MATLAB(七)

北城余情 提交于 2020-10-14 11:16:47
clc;
close all;
clear 

%% %%----平面--1280*800 single plane fitting

d0=800;%标定距离mm
B=45;%基线长度mm
f=1000;%焦距,像素
min_distance=350;%mm

M=1280;
N=800;
MAX=1023;
max_value=800;

d1 =1000;
dep_val=800;
img_depth=zeros(M,N)*dep_val;

%% 
figure
s = surf(img_depth);
s.EdgeColor = [0 1 0];
title('深度值')

% load('center_coordinate245_8359');
load('center_coordinate270_vescl');
figure
plot(center_coordinate(:,1),center_coordinate(:,2),'.')
axis equal
set(gca,'FontSize',12)
set(gca,'ydir','reverse')%设左上角为(0,0)点
axis([0 800 0 1280])
title('点阵图')
% s=B*f*(d1-d0)/(d1*d0);

%% 生成一个高斯函数,函数与散斑点的灰度分布具有有较高的一致性,即可以很好的仿真当前的
dia = 7;%散斑直径
sigma = 2;
gausFilter = fspecial('gaussian', [dia,dia], sigma);
% figure
times=19;%奇数
kernel=imresize(gausFilter,times);%%imresize 使用双三次插值。 B = imresize(A,scale) 返回图像 B,它是将 A 的长宽大小缩放 scale 倍之后的图像。
cx=round(dia*times/2);
cy=round(dia*times/2);
R=floor(dia*times/2);

kernel_mask=ones(size(kernel));
for m=1:times*dia
   for n=1:times*dia
       if (cx-n)^2+(cy-m)^2>=(R+1)*R
           kernel_mask(m,n)=0;
       end
   end
end

max_kernel = max(max(kernel));
kernel=kernel.*kernel_mask*(max_value/max_kernel);

surf(kernel);
%% 

IR_speckle=zeros(M*times,N*times);
L=length(center_coordinate);

for i=1:L
    center_x=round(center_coordinate(i,1)*times);
    center_y=round(center_coordinate(i,2)*times);
    
    if(((center_y-R)>0) && ((center_y+R) < M*times) && ((center_x-R)>0) && ((center_x+R) < N*times) ) 
       IR_speckle(center_y-R:center_y+R,center_x-R:center_x+R)=IR_speckle(center_y-R:center_y+R,center_x-R:center_x+R)+kernel;
    end     
end

ref_speckle=uint8(imresize(IR_speckle,1/times));
ref_speckle(ref_speckle>MAX)=MAX;
% figure
% imshow(ref_speckle,[])
% title('40cm散斑图')
ref_speckle =imrotate(ref_speckle,-90);
figure, imshow(ref_speckle, 'border', 'tight') 
save_path = 'D:\matlab\vescl\gauss\';  
imwrite(ref_speckle,[save_path,'ref.bmp']);


%% get object image
Dep=imresize(img_depth,times);
% surf(Dep);
Obj_spe=zeros(M*times,N*times);
for i=1:L
    center_x=center_coordinate(i,1)*times;%先不取整
    center_y=round(center_coordinate(i,2)*times);
%     if((center_x>M*times)||(center_y>N*times))
%         continue;
%     else
%         d1 = Dep(round(center_x),center_y);
        center_x=round(center_x-times*B*f*(d1-d0)/(d1*d0));      
%     end
    if(((center_y-R)>0) && ((center_y+R) < M*times) && ((center_x-R)>0) && ((center_x+R) < N*times) ) 
       Obj_spe(center_y-R:center_y+R,center_x-R:center_x+R)=Obj_spe(center_y-R:center_y+R,center_x-R:center_x+R)+kernel;
    end        
end

obj_speckle=uint8(imresize(Obj_spe,1/times));
obj_speckle(obj_speckle>MAX)=MAX;
obj_speckle =imrotate(obj_speckle,-90);
figure, imshow(obj_speckle, 'border', 'tight') 
save_path = 'D:\matlab\vescl\gauss\';  
imwrite(obj_speckle,[save_path,'object.bmp']);

%% 

将矩阵转换成图片,plot使用示例,数组操作剔除为0的元素

close all;
clear
clc
% origi= load('坐标270.txt');
% figure
% plot(origi(:,1),origi(:,2),'.');
% axis equal 

% Locate=find(a>122) %a是存储数据的数组名,find是找到大于122的数的位置 
% a(Locate)=[]; %删除数组a中大于122的元素

origin_data = load('center_coordinate270_vescl.mat');
x = origin_data.center_coordinate(:,1);
y = origin_data.center_coordinate(:,2);
origin_data_origin =[x,y];
% figure
% plot(x,y,'--gs',...
%         'LineWidth',1,...
%         'MarkerSize',2,...
%         'MarkerEdgeColor','b',...
%         'MarkerFaceColor',[0.5,0.5,0.5])
% axis equal    

Locate=find(x>800);
x(Locate) = [];
y(Locate) = [];
origin_data = [x,y];
% figure
% plot(x,y,'.');    
% axis equal  
[m,n] = size(origin_data);   

for num = 0:5
   % transposition x and y 
    coeff =0.02*num;
    gauss = normrnd(0,coeff,[m,n] );
    gauss_round = roundn(gauss,-4);
    simulation_data = origin_data + gauss_round;
    simulation_data_x = simulation_data(:,1);
    simulation_data_y = simulation_data(:,2);
    simulation_data = [simulation_data_x,simulation_data_y];    

    %%   
    t = uint16(simulation_data_x);
    simulation_data_x = uint16(simulation_data_y);
    simulation_data_y = t;
    rotate_data = [simulation_data_x,simulation_data_y] ;

    %% matrix to  mat 
    origin_image = zeros(1280,800);
     for i =1:m
          if(simulation_data_x(i)~=0)
              a = simulation_data_x(i);
              b = simulation_data_y(i);
              origin_image(a,b) = 1;             
           end                      
     end  
   
    origin_image = imrotate(origin_image,-90);   %%%imrotate>0 anticlock 
    origin_image = im2uint16(origin_image);
    figure, imshow(origin_image, 'border', 'tight') 
    save_path = 'D:\matlab\vescl\gauss\';
   
    frame = getframe(gcf);
    result = frame2im(frame);
    result = im2uint16(result);
    imwrite(origin_image,[save_path,num2str(num),'.png']);  
end

%% matrix to  mat 
    origin_image = zeros(1280,800);
     for i =1:m
          if(simulation_data_x(i)~=0)
              a = simulation_data_x(i);
              b = simulation_data_y(i);
              origin_image(a,b) = 1;             
           end                      
     end  







标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!