File: demo_image.sci

package info (click to toggle)
scilab 4.1.2-6
  • links: PTS, VCS
  • area: non-free
  • in suites: lenny
  • size: 113,992 kB
  • ctags: 65,732
  • sloc: ansic: 406,468; fortran: 242,412; xml: 223,812; tcl: 46,703; sh: 10,945; ml: 9,441; makefile: 4,697; cpp: 1,354; java: 926; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (354 lines) | stat: -rw-r--r-- 7,306 bytes parent folder | download
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
function demo_image()

demo_help demo_image
xdel() ;

// read image
[mat,h] = readppm('./scilab_logo.ppm');

// get three matrixes with the three channels
[r,g,b] = col2rgb(mat) ;

// display the initial image
[RGB,mat] = ppm2sci(mat,'p') ;
drawlater();
imageview(RGB,mat) ;
SetPosition_Small();
drawnow();


// compute the intensity matrix
intens = double( r + g + b ) ;

// fill the edges matrix
// and compute the intensity histogramm
[n,m] = size(intens) ;
edges = ones(n,m) ;

// 16 baquets
nb_baquet   = 32 ;
size_3      = 3 / 256 ;
inv_size    = 1 / 256 ;
histo       = zeros(1,nb_baquet) ;



for i = 2:n-1,
  for j= 2:m-1,
    xDiff = intens( i+1, j   ) - intens( i-1, j   ) ;
    yDiff = intens( i,   j+1 ) - intens( i  , j-1 ) ;

    // get the number of the baquet
    histo_color = ( intens(i,j) * nb_baquet ) / 256 + 1 ;
    histo( histo_color ) = histo( histo_color ) + 1 ;

    //edges(i,j) = 0.25 * sqrt( xDiff * xDiff + yDiff * yDiff ) ;
    edges(i,j) = 0.5 * ( abs( xDiff ) + abs( yDiff ) ) ;
  end
end

// display the histogramm
drawlater() ;
scf();
x = 0:256/nb_baquet:255;
//xtitle('Image intensity histogram');

// put histo between 0 and 1
histo = histo / ( n*m ) ;
bar( x, histo ) ;

SetPosition() ;
a = gca() ;
a.auto_ticks = ['on','on','on'] ;
t = a.title ;
t.text = 'Image intensity histogram' ;
t.font_size = 5 ;
SetPosition() ;
drawnow() ;

realtimeinit(1.0)
for i=1:10,
  realtime(i);
end
xdel() ;

// dipslay a pie with the three colors channels
drawlater() ;
scf();
red_sum   = double( sum(r) ) ;
green_sum = double( sum(g) ) ;
blue_sum  = double( sum(b) ) ;

sums = [green_sum,red_sum,blue_sum];
disp = [0 0 0];
//txt = ["blue","green","red"];
//xtitle('Proportion of the red, green and blue channels in the image');
pie( sums, disp ) ;
a = gca() ;
t = a.title ;
t.text = 'Proportion of the red, green and blue channels in the image' ;
t.font_size = 5 ;
SetPosition() ;
drawnow() ;

realtimeinit(1.0)
for i=1:10,
  realtime(i);
end
xdel() ;

res = uint8(edges);

// convert back thre three channels in one matrix
mat = rgb2col(res,res,res) ;

// dislay the result image
[RGB,mat] = ppm2sci(mat,'p') ;
xdel();
drawlater();
imageview(RGB,mat);
SetPosition_Small();
drawnow();


realtimeinit(1.0)
for i=1:10,
  realtime(i);
end

xdel() ;

endfunction

function imageview(RGB,m)
[M,N]=size(m)
xset('colormap',RGB)
xsetech([-0.16 -0.16 1.32 1.32])
xset('wdim',M,N)
Matplot(m','020')
endfunction

function [RGB,m]=ppm2sci(m,flag)
[nr,nc]=size(m);
// this function can be improved by the use of short integers but this
// require a sort for short ints
if flag=='p' then // color
  //get the colormap
  disp('Warning, this may take a while, be patient please...')
  k=1:3:nr;
  C=double(m(k,:))+double(m(k+1,:))*256+double(m(k+2,:))*(65536);
  cs=sort(C);k2=find(cs(2:$)<>cs(1:$-1));
  rgb=cs([k2(1) k2+1]);cs=[];rgb=rgb(:)

  R=modulo(rgb,256);
  w=(rgb-R)/256;G=modulo(w,256);B=(w-G)/256;w=[]
  RGB=[R G B]/256;R=[];G=[];B=[];

  m=zeros(nr/3,nc);
  for l=1:size(rgb,'*')
    m(find(C==rgb(l)))=l
  end
else //gray
  m=double(m)+1
  RGB=graycolormap(max(m)+1)
end
endfunction

function [m,head]=readimage(fil)
// this function reads images in PBM, PGM, PPM (ascii) formats
// fil is a string containing the file name containing the image
head=[]
lignecourante=[]
m=[]
//this command adds a carriage return to the end of the file
u=file('open',fil,'old')
file('close',u)
host('echo "'"' >> '+fil)
u=file('open',fil,'old')
//errcatch(-1,'continue')
head=read(u,1,1,'(a)')
niveau=1;
if head<>'P1'&head<>'P2'&head<>'P3' then
  error('Not a PBM/PGM/PPM file')
end
if head=='P1' then  niveau=0; end
if head=='P3' then niveau=2;end
c='#';


while c=='#',
  lignecourante=read(u,1,1,'(a)');
  head=[head;lignecourante];
  c=sscanf(lignecourante,'%c');
end;
[sz1,sz2]=sscanf(lignecourante,'%d%d');
if niveau~=0 then
  head=[head;read(u,1,1,'(a)')];
end;

if niveau==2 then
  m=matrix(read(u,1,3*sz1*sz2),3*sz1,sz2);
else
  m=matrix(read(u,1,sz1*sz2),sz1,sz2);
end;

file('close',u)
endfunction


function [r,g,b]=col2rgb(m)
//this function converts m, the result of reading a ppm color image
//into 3 matrices coding the red, green and blue components.
sz=size(m);
r=zeros(sz(1)/3,sz(2));
g=zeros(sz(1)/3,sz(2));
b=zeros(sz(1)/3,sz(2));
k=1:3:sz(1)
r=m(k,:);g=m(k+1,:);b=m(k+2,:);
endfunction

function [q]=rgb2col(r,g,b)
//this function constructs a matrix m coding a color image from
//3 matrices coding the red, green and blue components.
sz=size(r);
k=1:3:3*sz(1);
q=zeros(3*sz(1),sz(2));
q(k,:)=r;q(k+1,:)=g;q(k+2,:)=b;
endfunction

function [m,head]=readppm(fil)
// this function reads images in PBM, PGM, PPM (ascii and binary) formats
// fil is a string containing the file name containing the image
head=[]
lignecourante=[]
m=[]
[u,err]=mopen(fil,'rb')
if err<>0 then error('Impossible to open file '+fil),end
// read the type
head=getimgline(u)
h=head
if h~='P'+string([1 2 3 4 5 6]) then
  error('Not a PBM/PGM/PPM file')
end
ftyp=evstr(part(h,2))


//comments
c='#';
while c=='#',
  lignecourante=getimgline(u)
  head=[head;lignecourante];
  c=part(lignecourante,1)
end;
//image size
execstr('sz=['+lignecourante+']')
// number of colors

if and(ftyp<>[1 4]) then
  execstr('nc='+getimgline(u))
else
  nc=2
end;
if or(ftyp==[3 6]) then
  nv=3*sz(1)*sz(2),
  m=uint8(0);m(3*sz(1),sz(2))=uint8(0);
else
  nv=sz(1)*sz(2),
  m=uint8(0);m(sz(1),sz(2))=uint8(0);
end

l=1
if or(ftyp==[1 2 3]) then //ascii files
  mclose(u)
  u=file('open',fil,'old')
  w=read(u,size(head,1),1,'(a)');
  select ftyp
  case 1 then step=35*500;
  case 2 then step=17*1000;
  case 3 then step=15*1000;
  end
  nr=int(nv/step)

  errcatch(62,'continue','nomessage')
  for k=1:nr
    m(l:l+step-1)=uint8(read(u,1,step))
    if iserror(62)<>0 then
      errcatch(-1);errclear(-1)
      pause
      error('End of file reached, check for carriage return at the file end')
    end
    l=l+step
  end
  if nr*step<nv then
    r=nv-nr*step
    m(l:nv)=uint8(read(u,1,nv-l+1))
    if iserror(62)<>0 then
      errcatch(-1);errclear(-1)
      pause
      error('End of file reached, check for carriage return at the file end')
    end
    l=l+r
  end
  errcatch(-1)
  file('close',u)
else
  if ftyp<>4 then // uchar stored
    step=10000
    nr=ceil(nv/step)
    for k=1:nr
      tmp=mget(step,'uc',u);nt=size(tmp,'*')
      if tmp==[] then pause,end
      m(l:l+nt-1)=uint8(tmp);l=l+nt
    end
  else //bits are stored
    error('Binary Black and White  files cannot be read yet')
    step=1000
    nr=ceil(nv/(8*step))
    f=uint8(2.^(0:7))
    for k=1:nr
      tmp=uint8(mget(step,'uc',u));
      if tmp==[] then pause,end
      tmp=matrix([tmp&f(1);
	   (tmp&f(2));
	   (tmp&f(3));
	   (tmp&f(4));
	   (tmp&f(5));
           (tmp&f(6));
	   (tmp&f(7));
	   (tmp&f(8))],1,-1);
       tmp(find(tmp>uint8(1)))=uint8(1) //ne marche pas
       nt=size(tmp,'*')
       m(l:l+nt-1)=tmp
      l=l+nt;
    end
    m(find(m>uint8(1)))=uint8(1)

  end
  mclose(u)
end

endfunction


function l=getimgline(u)
h=[]
while %t
 c=mget(1,'uc',u)
 if c==10 then break,end
 h=[h c]
end

//
// Correcting BUG : Vince & Titiii
// We have to take care about the file Format !
// We do not care about the OS *<8-)
//
if h($) == 13 then
  s = size(h);
  s = s(2) - 1 ;
  l = ascii(h(1:s)) ;
else
  l = ascii(h) ;
end

endfunction