您的位置:首页 > 编程语言 > MATLAB

Matlab——彩色图像的拼接

2018-04-03 22:37 453 查看

Matlab——彩色图像的拼接

      有时候我们向老板或上级汇报近期的实验结果(图像),为了提升可视化的效果,我们可以将图片拼接后发给老板一张大图,简单粗暴!!!我做了一个简化版本,具体效果如下:


23张待拼接的原图


拼接后的图stack_1重点来了,如何使用呢?


      只需要将待整理的图像放到CompareResults文件夹,并保证图像的标号类型是“xxxx”,这里从0001开始。如果图像编号没有处理为该类型,运行会报错,怎么办呢?调用函数number2sring即可。完整代码下载地址:Matlab彩色图像拼接或者链接地址:https://download.csdn.net/download/zichen_ziqi/10325792Matlab代码如下(1个主函数+2个调用函数+1个辅助函数):主函数concatI
4000
mageDemo:close all;clear all;clc;
%% 输入与输出文件夹
folder_path = 'CompareResults\';
output_img_name = 'MergeResults\';
%% 开始拼接
for idx = 1
input_path = folder_path;
output_path = strcat(output_img_name,'stack-',num2str(idx),'.bmp');

%为了找出文件名的最小与最大编号
[min,max] = find_min_max(input_path);

%将彩色图像连接起来:ceil(n/4) x 4,不足用0补齐
concat_write_color( input_path, '%04d.bmp', min, max, output_path , ceil( size( dir(folder_path), 1 ) / 4 ), 4,1);
end调用函数concat_write_color:%% in order to compare smoothly we contact the images
function concat_write_color(inputPathName,img_format,img_start,img_end,concactBmpPath,rowNum,colNum,label_flag)
if(~exist('label_flag','var'))
label_flag = 0;
end

rowPadding = 20;
colPadding = 15;

label_flag = 1;
if label_flag
mkdir('tmp');
h = figure(1);
set(h,'visible','off');
for idx = img_start:img_end
file_name = sprintf(img_format,idx);
origin_img_path = fullfile(inputPathName,file_name);
if ~exist( origin_img_path,'file')
break;
end

[img] = imread( origin_img_path );

imshow(img);
xlabel(file_name);
print(h,'-dbmp',fullfile('tmp',file_name));
resize_img = imresize(imread(fullfile('tmp',file_name)),[300,400]);
% imwrite(resize_img,curcolormap,fullfile('tmp',file_name));
imwrite(resize_img,fullfile('tmp',file_name));
end
inputPathName = 'tmp';
end

%%
[rowLen ,colLen,channel] = size(imread(fullfile(inputPathName,sprintf(img_format,img_start))));
rowLen = uint32(rowLen);
colLen = uint32(colLen);
channel = uint32(channel);
concatImg = zeros(uint32((rowLen + rowPadding )*(rowNum-1)),uint32((colLen + colPadding)*colNum),uint32(channel));

for idx = img_start : img_end
file_path = fullfile(inputPathName,sprintf(img_format,idx));
if ~exist( file_path,'file')
break;
end
img = imread( file_path );
rowStart =uint32( floor((idx - 1)/colNum)*(rowLen + rowPadding)) - uint32( floor((img_start - 1)/colNum)*(rowLen + rowPadding)) ;
colStart = uint32(mod(idx - 1 ,colNum)*(colLen + colPadding));

concatImg(rowStart + 1:rowStart + rowLen ,colStart + 1:colStart + colLen,:) = img;
end
concatImg = uint8(concatImg);

imwrite(concatImg,concactBmpPath);
rmdir('tmp','s');
end调用函数find_min_max:function [min,max] = find_min_max(input_folder, pattern, pa)
if(~exist('pattern','var'))
pattern = '\d{4}';

end
file_names = dir(input_folder);
min = 99999999999;
max = -1;

for idx = 3:size(file_names,1)
num = str2num(cell2mat(regexp(file_names(idx).name, pattern, 'match')));
if num > max
max = num;
end
if num < min
min = num;
end
end
end图像重命名函数number2string:
function numstr=number2string(num)
num=num2str(num);
numzeros='0000';  %可根据需求更改
numstr=[numzeros(length(num):end) num];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: