MATLAB【二】————图像做减法,批量文本处理,子图显示

clear;
clc;
close all;
 
name_string = [
 "1.5ms\100\"
];

length = strlength(name_string);
[m,n] =size(length);
 %%----------------------------- 
 for num=1:m
    str = name_string(num,1);         
    figure('color', [1, 1, 1], 'position', [0, 0, 1800,800]);  % 为区分边界,将底色改为灰色       
   %%----------------读入图像-------------     
    img_path_A = fullfile('D:\data\youtu0712\正对\下午第二组\',str,'raw\1speckle.bmp');
    [A1, map_a] = imread(img_path_A);   
    info_A = imfinfo(img_path_A);
    A1 = double(A1);
    A = imrotate(A1,-90);
     
    img_path_B = fullfile('D:\data\youtu0712\正对\下午第二组\',str,'raw\1ir.bmp');
    [B1, map_b] = imread(img_path_B);   
    info_B = imfinfo(img_path_B);
    B1 = double(B1);
    B = imrotate(B1,-90);
    %% 
  %%----------------数据处理-------------     
%     w_a = info_A.Width;
%     h_a = info_A.Height;
    w_a = info_A.Height;
    h_a = info_A.Width;
    % 创建与图象大小相对应的网格
    [x_a,y_a] = meshgrid(1:w_a,1:h_a);
    z_a =x_a - y_a + y_a -x_a;
    i_a = 1;
    j_a = 1;
    % 用图象灰度值填充高度值
    while (i_a - 1) * w_a + j_a <= w_a * h_a
        z_a(i_a,j_a) = A(i_a,j_a);
        j_a = j_a + 1;
        if j_a > w_a
            j_a = 1;
            i_a = i_a + 1;
        end 
    end
    subplot(131)
    title('sp')
    % 绘制三维图象
    meshc(x_a,y_a,z_a);
    surf(x_a,y_a,z_a,'FaceColor','interp','EdgeColor','none','FaceLighting','phong') 
    view(0,90)
    colormap
    colorbar
    %%----------------数据处理-------------        
%     w_b = info_B.Width;
%     h_b = info_B.Height;
    w_b = info_B.Height;
    h_b = info_B.Width;
    [x_b,y_b] = meshgrid(1:w_b,1:h_b);
    z_b = x_b - y_b + y_b - x_b;
    i_b = 1;
    j_b = 1;
    % 用图象灰度值填充高度值
    while (i_b - 1) * w_b + j_b <= w_b * h_b
        z_b(i_b,j_b) = B(i_b,j_b);
        j_b = j_b + 1;
        if j_b > w_b
            j_b = 1;
            i_b = i_b + 1;
        end 
    end
    subplot(132); 
    title('ir');
   
    meshc(x_b,y_b,z_b);
    surf(x_b,y_b,z_b,'FaceColor','interp','EdgeColor','none','FaceLighting','phong'); 
    view(0,90)
%     colormap
    colormap(parula)
    colorbar
    %% 
%%----------------数据处理-------------  
%%----------------读入图像,做减法,找出差异-------------    
%      C = imsubtract(A,B);%A-B
%     C = bsxfun(@minus,A,B);
     C = A - B;   
%     C = B - A ; 
    w_c = 800;
    h_c = 1280;
    % 创建与图象大小相对应的网格
    [x_c,y_c] = meshgrid(1:w_c,1:h_c);
    z_c = x_c - y_c + y_c - x_c;
    i_c = 1;
    j_c= 1;
    
    over_expourse_count = 0;
    while (i_c - 1) * w_c + j_c <= w_c * h_c
        z_c(i_c,j_c) = C(i_c,j_c)+128;
        if(z_c(i_c,j_c)>255)
            over_expourse_count=over_expourse_count+1;           
            z_c(i_c,j_c)=255;
        else
             z_c(i_c,j_c)=0;
        end
        j_c = j_c + 1;
        if j_c > w_c
            j_c = 1;
            i_c = i_c + 1;
        end 
    end   
    title(['The overexposure num is =' num2str(over_expourse_count)])
    subplot(133)     
    title('深度差') 
    
    surf(x_c,y_c,z_c,'FaceColor','interp','EdgeColor','none','FaceLighting','phong') 
    view(0,90)
    colormap( subplot(133),gray(2)) 
%     colormap( subplot(133),parula(5))
    colorbar;



 %% 
%     %%----------------去除空白区域-------------
    set(gcf, 'InvertHardCopy', 'off'); % 让设置的背景色有效
    sub_row = 1; % 子图行数
    sub_col = 3; % 子图列数
    for i_row = 1 : sub_row
        for j_col = 1 : sub_col
            order = (i_row-1)*sub_col+j_col; % 子图的顺序
            subplot(sub_row, sub_col, order);
            RemoveSubplotWhiteArea(gca, sub_row, sub_col, i_row, j_col); % 去除空白部分
        end
    end
%      saveas(figure(num), ['substract\sp_substract_ir_', char(str),'.bmp'])
    str_to_char =char(str);
    str_for_path = replace(str_to_char,'\','a');   
    %str_for_path = replace(str_to_char,'_','b');   
    saveas(figure(num), ['substract2\_',str_for_path,'.bmp'])
     
 end
 

调用函数

function [] = RemoveSubplotWhiteArea(gca, sub_row, sub_col, current_row, current_col)
% 设置OuterPosition
sub_axes_x = current_col*1/sub_col - 1/sub_col;
sub_axes_y = 1-current_row*1/sub_row; % y是从上往下的
sub_axes_w = 1/sub_col;
sub_axes_h = 1/sub_row;
set(gca, 'OuterPosition', [sub_axes_x, sub_axes_y, sub_axes_w, sub_axes_h]); % 重设OuterPosition
 
% TightInset的位置
inset_vectior = get(gca, 'TightInset');
inset_x = inset_vectior(1);
inset_y = inset_vectior(2);
inset_w = inset_vectior(3);
inset_h = inset_vectior(4);
 
% OuterPosition的位置
outer_vector = get(gca, 'OuterPosition');
pos_new_x = outer_vector(1) + inset_x; % 将Position的原点移到到TightInset的原点
pos_new_y = outer_vector(2) + inset_y;
pos_new_w = outer_vector(3) - inset_w - inset_x; % 重设Position的宽
pos_new_h = outer_vector(4) - inset_h - inset_y; % 重设Position的高
 
% 重设Position
set(gca, 'Position', [pos_new_x, pos_new_y, pos_new_w, pos_new_h]);    %(此程序摘自博客 http://blog.csdn.net/shanchuan2012/article/details/53980288 )

%%%%%%%function 调用方法
%     set(gcf, 'InvertHardCopy', 'off'); % 让设置的背景色有效
%     sub_row = 1; % 子图行数
%     sub_col = 3; % 子图列数
%     for i_row = 1 : sub_row
%         for j_col = 1 : sub_col
%             order = (i_row-1)*sub_col+j_col; % 子图的顺序
%             subplot(sub_row, sub_col, order);
%             %plot(x,y,'r.');
%             RemoveSubplotWhiteArea(gca, sub_row, sub_col, i_row, j_col); % 去除空白部分
%         end
%     end

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大江东去浪淘尽千古风流人物

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值