-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_color_img_manipulation.m
129 lines (98 loc) · 3.31 KB
/
main_color_img_manipulation.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
% This is a MATLAB script for the
% CLPS1520 lecture on color images
% Other m-files required: none
% Subfunctions: none
% MAT-files required: none
% Author: Thomas Serre
% Data source: Images can be downloaded at https://www.dropbox.com/sh/2pga3leyhklri20/pSAtZKoAW1
% Brown University
% CLPS Department
% email: Thomas_Serre@Brown.edu
% Website: http://serre-lab.clps.brown.edu
% February 2014;
%%
% Load the image in the ../Data folder named 'Landscape-image.jpg'
% the image is then stored in a variable called img
% Look at the help for the 'imread' function
img = imread('../Data/Landscape-image.jpg');
%% Display the image on screen
imagesc(img); axis image; axis off;
% you should also check out the help for imshow and image
%% Convert images to gray values
gray_img = rgb2gray(img);
imagesc(gray_img); axis image; axis off; colormap gray;
colorbar;
%%
% let's do a little arithmetic using images
boat1 = imread('../Data/boat1.jpg');
boat2 = imread('../Data/boat2.jpg');
% let's resize boat2 so that it is the same size as boat1
[h, w, n] = size(boat1);
boat2 = imresize(boat2, [h w]);
% let's convert images to double
boat1 = double(boat1)/255;
boat2 = double(boat2)/255;
% sum between images
img = (boat1+boat2)/2;
figure(1)
imagesc(boat1); axis image; axis off;
figure(2)
imagesc(boat2); axis image; axis off;
figure(3)
imagesc(img); axis image; axis off;
%% product of scalar between image and scalara = 1;
img = gray_img*1;
figure(3)
imshow(img); axis image; axis off; colormap gray; colorbar;
%% weighted sum between images
a = .8;
img = (1-a)*boat1+a*boat2;
figure(1)
imagesc(boat1); axis image; axis off;
figure(2)
imagesc(boat2); axis image; axis off;
figure(3)
imagesc(img); axis image; axis off;
%% Color images: RGB channels
img = imread('../Data/speelgoed.tif');
close all
R = squeeze(img(:,:,1));
G = squeeze(img(:,:,2));
B = squeeze(img(:,:,3));
figure(1)
imagesc(R, [0 255]); axis image; axis off; colormap gray; title('R channel'); colorbar;
figure(2)
imagesc(G, [0 255]); axis image; axis off; colormap gray; title('G channel'); colorbar;
figure(3)
imagesc(B, [0 255]); axis image; axis off; colormap gray; title('B channel'); colorbar;
figure(4)
imagesc(img); axis image; axis off;
%% swap colors
swap_img = cat(3, B, R, G);
close all
figure(1)
imagesc(img); axis image; axis off;
figure(2)
imagesc(swap_img); axis image; axis off;
%% Use a for loop to do the color swap on all jpeg images in the directory
d = dir('../Data/*.jpg'); % the dir command retruns an M-by-1 structure with
% the filename of the files contained in the 'name' field.
% d(1).name returns the nam?e of the first file etc
% filenames are organized in alphanumerical order
nImg = length(d); % 'length' is like the 'size' command but for vectors
for ii = 1:nImg
img = imread(fullfile('../Data',d(ii).name));
siz = size(img);
if length(siz)>2
R = squeeze(img(:,:,1));
G = squeeze(img(:,:,2));
B = squeeze(img(:,:,3));
% swap colors
swap_img = cat(3, G, B, R);
figure(1)
imagesc(img); axis image; axis off;
figure(2)
imagesc(swap_img); axis image; axis off;
pause; % the pause command expects a keyboard input for the program to resume
end
end