MATLAB【二】————图像做减法,批量文本处理,子图显示
生活随笔
收集整理的這篇文章主要介紹了
MATLAB【二】————图像做减法,批量文本处理,子图显示
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
clear;
clc;
close all;name_string = ["1.5ms\100\"
];length = strlength(name_string);
[m,n] =size(length);%%----------------------------- for num=1:mstr = name_string(num,1); figure('color', [1, 1, 1], 'position', [0, 0, 1800,800]); % 為區(qū)分邊界,將底色改為灰色 %%----------------讀入圖像------------- 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);%% %%----------------數(shù)據(jù)處理-------------
% w_a = info_A.Width;
% h_a = info_A.Height;w_a = info_A.Height;h_a = info_A.Width;% 創(chuàng)建與圖象大小相對應(yīng)的網(wǎng)格[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_az_a(i_a,j_a) = A(i_a,j_a);j_a = j_a + 1;if j_a > w_aj_a = 1;i_a = i_a + 1;end endsubplot(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)colormapcolorbar%%----------------數(shù)據(jù)處理-------------
% 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_bz_b(i_b,j_b) = B(i_b,j_b);j_b = j_b + 1;if j_b > w_bj_b = 1;i_b = i_b + 1;end endsubplot(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)
% colormapcolormap(parula)colorbar%%
%%----------------數(shù)據(jù)處理-------------
%%----------------讀入圖像,做減法,找出差異-------------
% C = imsubtract(A,B);%A-B
% C = bsxfun(@minus,A,B);C = A - B;
% C = B - A ; w_c = 800;h_c = 1280;% 創(chuàng)建與圖象大小相對應(yīng)的網(wǎng)格[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_cz_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;elsez_c(i_c,j_c)=0;endj_c = j_c + 1;if j_c > w_cj_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;%%
% %%----------------去除空白區(qū)域-------------set(gcf, 'InvertHardCopy', 'off'); % 讓設(shè)置的背景色有效sub_row = 1; % 子圖行數(shù)sub_col = 3; % 子圖列數(shù)for i_row = 1 : sub_rowfor j_col = 1 : sub_colorder = (i_row-1)*sub_col+j_col; % 子圖的順序subplot(sub_row, sub_col, order);RemoveSubplotWhiteArea(gca, sub_row, sub_col, i_row, j_col); % 去除空白部分endend
% 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
調(diào)用函數(shù)
function [] = RemoveSubplotWhiteArea(gca, sub_row, sub_col, current_row, current_col)
% 設(shè)置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]); % 重設(shè)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的原點(diǎn)移到到TightInset的原點(diǎn)
pos_new_y = outer_vector(2) + inset_y;
pos_new_w = outer_vector(3) - inset_w - inset_x; % 重設(shè)Position的寬
pos_new_h = outer_vector(4) - inset_h - inset_y; % 重設(shè)Position的高% 重設(shè)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 調(diào)用方法
% set(gcf, 'InvertHardCopy', 'off'); % 讓設(shè)置的背景色有效
% sub_row = 1; % 子圖行數(shù)
% sub_col = 3; % 子圖列數(shù)
% 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
?
總結(jié)
以上是生活随笔為你收集整理的MATLAB【二】————图像做减法,批量文本处理,子图显示的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: k吗
- 下一篇: 哪些电影院能直播国庆70周年盛况