1 |
2 |
joaocarlos |
% +----------------------------------------------------------------------------
|
2 |
|
|
% Universidade Federal da Bahia
|
3 |
|
|
% ------------------------------------------------------------------------------
|
4 |
|
|
% PROJECT: FPGA Median Filter
|
5 |
|
|
% ------------------------------------------------------------------------------
|
6 |
|
|
% FILE NAME : median_ref.m
|
7 |
|
|
% AUTHOR : João Carlos Bittencourt
|
8 |
|
|
% AUTHOR'S E-MAIL : joaocarlos@ieee.org
|
9 |
|
|
% -----------------------------------------------------------------------------
|
10 |
|
|
% RELEASE HISTORY
|
11 |
|
|
% VERSION DATE AUTHOR DESCRIPTION
|
12 |
|
|
% 1.0 2013-08-27 joao.nunes initial version
|
13 |
|
|
% -----------------------------------------------------------------------------
|
14 |
|
|
% KEYWORDS: median, filter, image processing
|
15 |
|
|
% -----------------------------------------------------------------------------
|
16 |
|
|
% PURPOSE: Reference model for Median Filter.
|
17 |
|
|
% -----------------------------------------------------------------------------
|
18 |
|
|
|
19 |
|
|
% This is only a simple verification reference model based on default Median Filter
|
20 |
|
|
|
21 |
|
|
% Clear environment
|
22 |
|
|
clc
|
23 |
|
|
clear
|
24 |
|
|
% Set-up vectors
|
25 |
|
|
% I have set the vector to size 12 in order to perform a basic testbench.
|
26 |
|
|
% result = zeros(227,227);
|
27 |
|
|
img_ref = imread('images/image22.jpg');
|
28 |
|
|
|
29 |
|
|
img_ref = rgb2gray(img_ref);
|
30 |
|
|
[width, height] = size(img_ref);
|
31 |
|
|
% Add one column with zeros for pipelining verification purpose.
|
32 |
|
|
% The hardware assumes that, in the first round in a row, previous values are zeros.
|
33 |
|
|
img = zeros(width+1,width+1);
|
34 |
|
|
for i = 1 : height,
|
35 |
|
|
img(i,2:width+1) = img_ref(i, 1:width);
|
36 |
|
|
end
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
% Default Median Algorithm
|
40 |
|
|
window_width = 3;
|
41 |
|
|
window_height = 3;
|
42 |
|
|
edgex = floor(window_width/2);
|
43 |
|
|
edgey = floor(window_height/2);
|
44 |
|
|
tic
|
45 |
|
|
for x = edgex : width - edgex,
|
46 |
|
|
for y = edgey : height - edgey,
|
47 |
|
|
temp = zeros(edgex,edgey);
|
48 |
|
|
for fx = 1 : window_width,
|
49 |
|
|
for fy = 1 : window_height,
|
50 |
|
|
temp(fx,fy) = img(x + fx - edgex, y + fy - edgey);
|
51 |
|
|
end
|
52 |
|
|
end
|
53 |
|
|
temp = reshape(temp.',1,[]);
|
54 |
|
|
srt = sort(temp); % remove comma to view step by step outputs
|
55 |
|
|
result(x,y) = srt(5);
|
56 |
|
|
end
|
57 |
|
|
end
|
58 |
|
|
toc
|
59 |
|
|
wtime = toc
|
60 |
|
|
fprintf ( 1, ' MY_PROGRAM took %f seconds to run.\n', wtime );
|
61 |
|
|
|
62 |
|
|
%result
|
63 |
|
|
imshow(mat2gray(result));
|
64 |
|
|
imwrite(mat2gray(result), 'images/image22_median.jpg', 'jpg');
|
65 |
|
|
|