-
Notifications
You must be signed in to change notification settings - Fork 4
/
bound_extension.m
72 lines (59 loc) · 3.15 KB
/
bound_extension.m
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
%===================================================================================
% MATLAB code for multi-level image thresholding segmentation using 2DNLMeKGSA.
% Author: Himanshu Mittal ([email protected]),
% Mukesh Saraswat ([email protected])
% Modified this file for the non-commercial purpose only.
% (https://quasar.ugent.be/bgoossen/download_nlmeans/)
%
% Developed in MATLAB R2015a
%
% Reference: "An optimum multi-level image thresholding segmentation using
% non-local means 2D histogram and exponential Kbest gravitational
% search algorithm." Engineering Applications of Artificial
% Intelligence, Volume 71, Pages 226-235, Elsevier, 2018.
% https://doi.org/10.1016/j.engappai.2018.03.001
%
% File purpose: Part of the file that computes the non-local means of an image.
%===================================================================================
function im_ext = bound_extension(im,By,Bx,type)
% im_ext = bound_extension(im,B,type);
%
% Extend an image for avoiding boundary artifacts,
%
% By, Bx: widths of the added stripes.
% type: 'mirror' Mirror extension
% 'mirror_nr': Mirror without repeating the last pixel
% 'circular': fft2-like
% 'zeros'
% Javier Portilla, Universidad de Granada, Jan 2004
[Ny,Nx,Nc] = size(im);
im_ext = zeros(Ny+2*By,Nx+2*Bx,Nc);
im_ext(By+1:Ny+By,Bx+1:Nx+Bx,:) = im;
if strcmp(type,'mirror'),
im_ext(1:By,:,:) = im_ext(2*By:-1:By+1,:,:);
im_ext(:,1:Bx,:) = im_ext(:,2*Bx:-1:Bx+1,:);
im_ext(Ny+1+By:Ny+2*By,:,:) = im_ext(Ny+By:-1:Ny+1,:,:);
im_ext(:,Nx+1+Bx:Nx+2*Bx,:) = im_ext(:,Nx+Bx:-1:Nx+1,:);
im_ext(1:By,1:Bx,:) = im_ext(2*By:-1:By+1,2*Bx:-1:Bx+1,:);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