-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreen.m
More file actions
56 lines (43 loc) · 1.53 KB
/
green.m
File metadata and controls
56 lines (43 loc) · 1.53 KB
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
clear all;
clc;
% reading input image locations
disp('Make sure the dimensions for both the images are the same');
fg_img = input('Enter the full path to the foreground image: ', 's');
bg_img = input('Enter the full path to the background image: ', 's');
[filepath, name, ext] = fileparts(fg_img);
% path to the output image
output_img = strcat(filepath, '\', 'output.png');
% reading the images
fg = imread(fg_img);
bg = imread(bg_img);
% extracting image dimensions
size_img = size(fg);
w = size_img(1);
h = size_img(2);
% green pixel threshold
threshold = 235;
% initializing output array
output = zeros(w, h, 3, 'uint8');
for i = 1:1:w
for j = 1:1:h
% accessing the green pixel of fg image
% 2 corresponds to green matrix
g_pixel = fg(i, j, 2);
if g_pixel >= threshold
% if current pixel green value >= threshold
% then select pixel from background
output(i, j, 1) = bg(i, j, 1); % 1 corresponds to red matrix
output(i, j, 2) = bg(i, j, 2); % 2 corresponds to green matrix
output(i, j, 3) = bg(i, j, 3); % 3 corresponds to blue matrix
else
% then select pixel from foreground
output(i, j, 1) = fg(i, j, 1);
output(i, j, 2) = fg(i, j, 2);
output(i, j, 3) = fg(i, j, 3);
end
end
end
imwrite(output, output_img);
clc;
disp(strcat('File - output.png', ' saved.'));
imshow(output_img);