Friday, December 4, 2015

Matlab: Embedding Images

Summary

代码操作一张图片嵌入另一图片之特定位置。

Code Specification

对于需要嵌入的图片,首先操作它缩放一定比例,scale定义缩放比例。在原始图片Fig. 1嵌入另一图片,得到Fig. 2,。
Fig. 1
Fig. 2
 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
% Created by LI Xu
% Version 1.0
% 12 April, 2015

% Improved by LI Xu
% Version 1.1
% December 4, 2015
% Set the input/output directories
% for processing

clear;
clc;
clf;

% Front Picture
frontpath='legend.png';
front=imread(frontpath);
% Scale Factor
scale=0.18;
front=imresize(front, scale);
frontrow=size(front, 1);
frontcol=size(front, 2);
% Source Directory
SouDir='./input';
% Destination Directory
DesDir='./output';

% Set the start location of row and column
hratio=0.57;
vratio=0.8;

% Background Picture
files=dir([SouDir, '/*.png']);
for ii=1:length(files)
    
    filename=files(ii).name;
    filepath=[SouDir, '/', filename];
    back=imread(filepath);
    row=size(back, 1);
    col=size(back, 2);
    
    strow=round(row*vratio);
    stcol=round(col*hratio);
    
    
    resimg=back;
    resimg(strow:strow+frontrow-1, stcol:stcol+frontcol-1, :)=front;
    
    imshow(resimg);
    pause(2);
    
    otpath=[DesDir, '/', filename];
    % Save
    imwrite(resimg, otpath);
    close all;
    disp([num2str(ii), '.', filename]);
    
end

disp('***********************************************');

No comments:

Post a Comment