Tuesday, November 24, 2015

Matlab: Enhanced Mosaic Operation

Summary

若干图片还是要求具有同样的尺寸,并含有相同的波段数量。

Code Specification

代码前端设定这些内容:
  1. N=2;定义输出列数
  2. color=[255, 255, 255];定义输出背景颜色
  3. blkimg.width=0.04;定义插入图片之间水平方向间距,归一化为图片的width
  4. blkimg.height=0.04;定义插入图片之间垂直方向间距,归一化为图片的height
  5. edgimg.width=0;定义图片与边缘之间水平方向间距,归一化为图片的width
  6. edgimg.height=0;定义图片与边缘之间垂直方向间距,归一化为图片的height
如果blkimg.width、blkimg.height、edgimg.width、edgimg.height四变量全部赋值为0,此时结果将与下面代码结果一致,不留出任何空白,见Fig. 1。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
% Created by LI Xu
% Version 1.0
% 25 October, 2014

% Modified by LI Xu
% Version 1.1
% November 24, 2015
% Add Source and destination directory

% Mosaicing Images with the same
% rows, columns and banks.
% No blank edges.

clear;
clc;

% Set the number of columns for Output M*N
N=2;

% Source Directory
SouDir='./input';
% Destination Directory
DesDir='./output';

% Images
files=dir([SouDir, '/*.png']);
num_files=length(files);
M=ceil(num_files/N);

% Image Info
img=imread([SouDir, '/', files(1).name]);
rows=size(img, 1);
cols=size(img, 2);
bands=size(img, 3);

cellstack=cell(M, 1);
N_flag=1;
temp_img=zeros(rows, cols*N, bands)+255;
temp_img=uint8(temp_img);
for ii=1:num_files
    imgfile=files(ii).name;
    in_img=imread([SouDir, '/', imgfile]);
    
    % Position
    if N_flag > N
        N_flag=1;
        temp_img=zeros(rows, cols*N, bands)+255;
        temp_img=uint8(temp_img);
    end
    cellrow=ceil(ii./N);
    row_position=1:rows;
    col_position=1+cols*(N_flag-1):cols*N_flag;
    temp_img(row_position, col_position, :)=in_img;
    %imshow(temp_img);
    cellstack{cellrow, 1}=temp_img;
    
    N_flag=N_flag+1;
end


% Output Image
otImage=zeros(rows*M, cols*N, bands);
for row=1:M
    row_range=1+rows*(row-1):rows*row;
    otImage(row_range, :, :)=cellstack{row, 1};
end

otImage=uint8(otImage);
imshow(otImage);
pause(2);

otname=['NoEdge_R', num2str(M), 'C', num2str(N), '.png'];
otpath=[DesDir, '/', otname];
imwrite(otImage, otpath);
close all
disp('END');

Fig. 1
其他时候可以是这样的:
Fig. 2
Fig. 3
MatMosaic1.2.rar.灰度与彩色图片混搭.

References

No comments:

Post a Comment