-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageRGB_write_file.m
284 lines (245 loc) · 9.32 KB
/
imageRGB_write_file.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
%6.111 Image Color Table MATLAB deme
%Edgar Twigg [email protected]
%4/1/2008 (But I swear this file isn't a joke)
close all
Number = 'raindrop_Pixel';
%% How to use this file
%Notice how %% divides up sections? If you hit ctrl+enter, then MATLAB
%will execute all the lines within that section, but nothing else. You can
%also navigate quickly through the file using ctrl+arrow_key
%% Getting 24 bit data
%So when you look at a 24 bit bitmap file, the file specifies three 8 bit
%values for each color, 8 each for red, green, and blue.
[picture] = imread(strcat('./Images/',Number,'.bmp'));
%% View the image
%This command image will draw the picture you just loaded
figure %opens a new window
image(picture) %draws your picture
title('24 bit bitmap') %gives it a title so you don't forget what it is
%% Manipulate the data in the image
%So now you have a matrix of values that represent the image. You can
%access them in the following way:
%
%picture(row,column,color)
%
%Remember that MATLAB uses 1-based indexes, and Verilog uses 0!
%
%Also, you can use MATLAB's slice operator to do nifty things.
%picture(:,:,1) would return a 2D matrix with the red value for every row and
%column.
%
%picture(:,1,2) would return a 1D matrix with the green value for every row
%in the first column.
%This is how MATLAB indexes the colors
RED = 1;
GREEN = 2;
BLUE = 3;
%So if we wanted to see the red values of the image only, we could say
figure
image(picture(:,:,RED))
title('Red values in 24 bit bitmap')
%Because the image we gave matlab above specifies only one value per pixel
%rather than usual three (red,blue,green), MATLAB colors each pixel from
%blue to red based on the value at that pixel.
%% Getting 8 bit data
%When you store an 8 bit bitmap, things get a little more complicated. Now
%each pixel in the image only gets one 8 bit value. But, you need to send
%the monitor an r,g, and b! How can this work?
%
%8 bit bitmaps include a table which specifies the rgb values for each of
%the 8 bits in the image.
%
%So each pixel is represented by one byte, and that byte is an index into a
%table where each index specifies an r, g, and b value separately.
%
%Because of this, now we need to load both the image and it's colormap.
[picture, color_table] = imread(strcat('./Images/',Number,'.bmp'));
%% Displaying without the color table
%If we try to display the picture without the colormap, the image does not
%make sense
figure
image(picture)
title('Per pixel values in 8 bit bitmap')
%% Displaying WITH the color table
%So to display the picture with the proper color table, we need to tell
%MATLAB to set its colormap to be in line with our colorbar. The image
%quality is somewhat reduced compared to the 24 bit image, but not too bad.
figure
image(picture)
colormap(color_table) %This command tells MATLAB to use the image's color table
colorbar %This command tells MATLAB to draw the color table it is using
title('8 bit bitmap displayed using color table')
% %% More about the color table
% %The color table is in the format:
% %
% %color_table(color_index,1=r 2=g 3=b)
% %
% %So to get the r g b values for color index 3, we only need to say:
% disp(' r g b for color 3 is:')
% disp(color_table(3,:)) %disp = print to console
%
% %Although in the bitmap file the colors are indexed as 0-255 and each rgb
% %value is an integer between 0-255, MATLAB images don't work like that, so
% %MATLAB has automatically scaled them to be indexed 1-256 and to have a
% %floating point value between 0 and 1. To turn the floats into integer
% %values between 0 and 256:
%
% color_table_8bit = uint8(round(256*color_table));
%
% disp(' r g b for color 3 in integers is:')
% disp(color_table_8bit(3,:))
%
% %Note that this doesn't fix the indexing (and it can't, since MATLAB won't
% %let you have indexes below 1)
%
% %another way to look at the color table is like this (don't worry about how
% %to make this graph)
% figure
% stem3(color_table_8bit)
% set(gca,'XTick',1:3);
% set(gca,'YTick',[1,65,129,193,256]);
% set(gca,'YTickLabel',[' 0';' 64';'128';'192';'255']);
% set(gca,'ZTick',[0,64,128,192,255]);
%
% xlabel('red = 1, green = 2, blue = 3')
% ylabel('color index')
% zlabel('value')
% title('Another way to see the color table')
%% Even smaller bitmaps
%You can extend what we did for 8-bit bitmaps to even more compressed
%forms, such as this 4-bit bitmap. Now we only have 16 colors to work with
%though, and our image quality is significantly reduced:
% [picture, color_table] = imread('./Images/3.bmp');
%
% figure
% image(picture)
% colormap(color_table)
% colorbar
% title('4 bit bitmap displayed using color table')
%% Writing data to coe files for putting them on the fpga
%You can instantiate BRAMs to take their values from a file you feed them
%when you flash the FPGA. You can use this technique to send them
%colortables, image data, anything. Here's how to send the red component
%of the color table of the last example
red = color_table(:,1); %grabs the red part of the colortable
scaled_data = red*255; %scales the floats back to 0-255
rounded_data = round(scaled_data); %rounds them down
data = dec2bin(rounded_data,8); %convert the binary data to 8 bit binary #s
%open a file
output_name = strcat('./Images/',Number,'_rcm.coe');
file = fopen(output_name,'w');
%write the header info
fprintf(file,'memory_initialization_radix=2;\n');
fprintf(file,'memory_initialization_vector=\n');
fclose(file);
%put commas in the data
rowxcolumn = size(data);
rows = rowxcolumn(1);
columns = rowxcolumn(2);
output = data;
for i = 1:(rows-1)
output(i,(columns+1)) = ',';
end
output(rows,(columns+1)) = ';';
%append the numeric values to the file
dlmwrite(output_name,output,'-append','delimiter','', 'newline', 'pc');
% create color table for green (2) and blue (3) and you're done!
green = color_table(:,2); %grabs the red part of the colortable
scaled_data = green*255; %scales the floats back to 0-255
rounded_data = round(scaled_data); %rounds them down
data = dec2bin(rounded_data,8); %convert the binary data to 8 bit binary #s
%open a file
output_name = strcat('./Images/',Number,'_gcm.coe');
file = fopen(output_name,'w');
%write the header info
fprintf(file,'memory_initialization_radix=2;\n');
fprintf(file,'memory_initialization_vector=\n');
fclose(file);
%put commas in the data
rowxcolumn = size(data);
rows = rowxcolumn(1);
columns = rowxcolumn(2);
output = data;
for i = 1:(rows-1)
output(i,(columns+1)) = ',';
end
output(rows,(columns+1)) = ';';
%append the numeric values to the file
dlmwrite(output_name,output,'-append','delimiter','', 'newline', 'pc');
%Blue
blue = color_table(:,3); %grabs the red part of the colortable
scaled_data = blue*255; %scales the floats back to 0-255
rounded_data = round(scaled_data); %rounds them down
data = dec2bin(rounded_data,8); %convert the binary data to 8 bit binary #s
%open a file
output_name = strcat('./Images/',Number,'_bcm.coe');
file = fopen(output_name,'w');
%write the header info
fprintf(file,'memory_initialization_radix=2;\n');
fprintf(file,'memory_initialization_vector=\n');
fclose(file);
%put commas in the data
rowxcolumn = size(data);
rows = rowxcolumn(1);
columns = rowxcolumn(2);
output = data;
for i = 1:(rows-1)
output(i,(columns+1)) = ',';
end
output(rows,(columns+1)) = ';';
%append the numeric values to the file
dlmwrite(output_name,output,'-append','delimiter','', 'newline', 'pc');
%% Turning a 2D image into a 1D memory array
%The code above is all well and good for the color table, since it's 1-D
%(well, at least you can break it into 3 1-D arrays). But what about a 2D
%array? We need to turn it into a 1-D array:
picture_size = size(picture); %figure out how big the image is
num_rows = picture_size(1);
num_columns = picture_size(2);
pixel_columns = zeros(picture_size(1)*picture_size(2),1,'uint8'); %pre-allocate a space for a new column vector
for r = 1:num_rows
for c = 1:num_columns
pixel_columns((r-1)*num_columns+c) = picture(r,c); %pixel# = (y*numColumns)+x
end
end
%so now pixel_columns is a column vector of the pixel values in the image
rounded_data = round(pixel_columns); %rounds them down
data = dec2bin(rounded_data,8); %convert the binary data to 8 bit binary #s
%open a file
output_name = strcat('./Images/',Number,'.coe');
file = fopen(output_name,'w');
%write the header info
fprintf(file,'memory_initialization_radix=2;\n');
fprintf(file,'memory_initialization_vector=\n');
fclose(file);
%put commas in the data
rowxcolumn = size(data);
rows = rowxcolumn(1);
columns = rowxcolumn(2);
output = data;
for i = 1:(rows-1)
output(i,(columns+1)) = ',';
end
output(rows,(columns+1)) = ';';
%append the numeric values to the file
dlmwrite(output_name,output,'-append','delimiter','', 'newline', 'pc');
%just to make sure that we're doing things correctly
regen_picture = zeros(num_rows,num_columns,'uint8');
for r = 1:num_rows
for c = 1:num_columns
regen_picture(r,c) = pixel_columns((r-1)*num_columns+c,1);
end
end
figure
subplot(121)
image(picture)
axis square
colormap(color_table)
colorbar
title('Original Picture')
subplot(122)
image(regen_picture)
axis square
colormap(color_table)
colorbar
title('Regenerated Picture')