第一部分:彩色图像的拼接
有时候我们向老板或上级汇报近期的实验结果(图像),为了提升可视化的效果,我们可以将图片拼接后发给老板一张大图,简单粗暴!!!我做了一个简化版本,具体效果如下:
23张待拼接的原图
拼接后的图stack_1
重点来了,如何使用呢?
只需要将待整理的图像放到CompareResults文件夹,并保证图像的标号类型是“xxxx”,这里从0001开始。如果图像编号没有处理为该类型,运行会报错,怎么办呢?调用函数number2sring即可。完整代码下载地址:Matlab彩色图像拼接或者链接地址:https://download.csdn.net/download/zichen_ziqi/10325792
Matlab代码如下(1个主函数+2个调用函数+1个辅助函数):
主函数concatImageDemo:
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];
第二部分:彩色图像的裁剪
假如现有一张超大分辨率的高清图,需要对其进行分割,由于大图分割的效率很低,我们可以将其进行裁剪,如剪裁为256x256尺寸的小图,再进行分割。具体效果如下:
裁剪效果为:
matlab代码:
clc;clear all;close all;
%% 输出路径
outputPath = '图像_256x256\';
if ~exist(outputPath)
mkdir(outputPath);
end
%% 读入图片,图片大小为:12134x21069?
[filename, filepath] = uigetfile({'*.jpg;*.ppm; jpeg *.;*.bmp;*.png;*.gif'},'Choose Input Image');
if isequal(filename,0) || isequal(filepath,0)
disp('User pressed cancel')
return
else
fullfp = fullfile(filepath, filename);
end
Img = imread(fullfp);
%% 图像划分子图的过程(子图的尺寸:256x256,格式为.png)
[m,n,dim] = size(Img);
ImageNew = Img;
rowNum = m - floor(m/256)*256;
colNum = n - floor(n/256)*256;
%% 行裁边
if (rowNum~=0)
if(mod(rowNum,2)==0)
ImageNew = ImageNew((rowNum/2+1):(m-rowNum/2),:,:);
end
if(mod(rowNum,2)~=0)
ImageNew = ImageNew((floor(rowNum/2)+1):(m-ceil(rowNum/2)),:,:);
end
end
%% 列裁边
if (colNum~=0)
if(mod(colNum,2)==0)
ImageNew = ImageNew(:,(colNum/2+1):(n-colNum/2),:);
end
if(mod(colNum,2)~=0)
ImageNew = ImageNew(:,(floor(colNum/2)+1):(n-ceil(colNum/2)),:);
end
end
%% 划分过程
[mm,nn,dimNew] = size(ImageNew);
% 原图划分:行floor(m/256),列floor(n/256);
rowNewNum = floor(m/256);
colNewNum = floor(n/256);
num = 0;
for ii = 1:rowNewNum
for jj = 1:colNewNum
fprintf('\n正在处理第%d张图片,请稍后......\n',num+1);
subImage = ImageNew((256*(ii-1)+1):256*ii,(256*(jj-1)+1):256*jj,:);
num = num + 1;
% imshow(subImage);
imwrite(subImage,strcat(outputPath,number2string(num),'.png'));
end
end
fprintf('\n划分完成!');