MATLAB【七】———— matlab 高斯核使用,超像素图像模拟,矩阵转图像,深度相机模型实践实现
生活随笔
收集整理的這篇文章主要介紹了
MATLAB【七】———— matlab 高斯核使用,超像素图像模拟,矩阵转图像,深度相机模型实践实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
深度模型,圖片轉稀疏矩陣,稀疏矩陣轉圖片?
%% mat to 2array
temp_speckle = ref_speckle;
[row_index,col_index,v]=find(temp_speckle);
obj_matrix =[row_index,col_index];
[obj_matrix_height,obj_matrix_width] = size(obj_matrix);%% depth camera model
plane_matrix = ones(rows,cols);
% s = B*f*(diff_dis-d0)/(diff_dis*d0);row_index = round(row_index-B*f*(diff_dis-d0)/(diff_dis*d0)); %% 2array to mat obj_image = zeros(1280,800);obj_matrix =[row_index,col_index];for i_obj_image = 1:obj_matrix_heightif(obj_matrix(i_obj_image)~=0)a = obj_matrix(:,1);b = obj_matrix(:,2);obj_image(a,b) = 1;endend
?
clc;
close all;
clear %% %%----平面--1280*800 single plane fittingd0=800;%標定距離mm
B=45;%基線長度mm
f=1000;%焦距,像素
min_distance=350;%mmM=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*diafor n=1:times*diaif (cx-n)^2+(cy-m)^2>=(R+1)*Rkernel_mask(m,n)=0;endend
endmax_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:Lcenter_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
endref_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:Lcenter_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));
% endif(((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
endobj_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:mif(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 ?
總結
以上是生活随笔為你收集整理的MATLAB【七】———— matlab 高斯核使用,超像素图像模拟,矩阵转图像,深度相机模型实践实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 个性现实签名
- 下一篇: 基础数据结构【一】————数组