-
Notifications
You must be signed in to change notification settings - Fork 2
/
04-imfilter.html
812 lines (614 loc) · 34.7 KB
/
04-imfilter.html
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>In this notebook, I will learn how to run some basic edge detection algorithms from Julia. Given an image, return an edge map. To do this, I want to learn how to do the following:</p>
<ol>
<li>Run a Sobel kernel on an input image</li>
<li>Create an edge map using the high frequency signal from the image</li>
<li>Have a long aside about array notation in Julia</li>
</ol>
<p>This is a natural follow-up to my <a href="https://learningjulia.com/2017/02/24/blurring-and-manipulation.html">blurring computation from a previous exercise</a>, since the Sobel operator is just a different kind of kernel. But it is also a common kernel needed in image manipulation, so I can compare my implemenation timing to the implementation in the <a href="https://github.com/JuliaImages/ImageFiltering.jl">ImageFiltering.jl</a> package. After timing the built-in functions, I won't even try to time my own implemenation...</p>
<h2 id="Setup">Setup<a class="anchor-link" href="#Setup">¶</a></h2><p>First things first, let's set up for manipulating images.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="k">using</span> <span class="n">Images</span><span class="p">,</span> <span class="n">FileIO</span><span class="p">,</span> <span class="n">Colors</span><span class="p">;</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>The test image is going to be of our former president, Barack Obama.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">img</span> <span class="o">=</span> <span class="n">load</span><span class="p">(</span><span class="s">"obama.jpg"</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[2]:</div>
<div class="output_png output_subarea output_execute_result">
<img src="/assets/imgs/04-imfilter/04-imfilter_3_0.png"
>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>The Sobel kernel should operate on grayscale images, and we can use operator broadcasting to do that:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">img_gray</span> <span class="o">=</span> <span class="n">Gray</span><span class="o">.</span><span class="p">(</span><span class="n">img</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[3]:</div>
<div class="output_png output_subarea output_execute_result">
<img src="/assets/imgs/04-imfilter/04-imfilter_5_0.png"
>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Sobel-kernels">Sobel kernels<a class="anchor-link" href="#Sobel-kernels">¶</a></h2><p>The first thing we'll try doing is manually running a Sobel image kernel on the input image. The <a href="https://en.wikipedia.org/wiki/Sobel_operator">Sobel operator</a> is basically an approximation of derivatives in the <code>X</code> and <code>Y</code> directions of the image. The theory is that if there is a high gradient magnitude, there is an edge in that location. The way you compute the Sobel operator is to convolve this kernel:</p>
$$K_x = \begin{bmatrix} 1 & 0 & -1 \\ 2 & 0 & -2 \\ 1 & 0 & -1 \end{bmatrix}$$<p>in the <code>X</code> direction, and</p>
$$K_y = \begin{bmatrix} 1 & 2 & 1 \\ 0 & 0 & 0 \\ -1 & -2 & -1 \end{bmatrix}$$<p>in the <code>Y</code> direction. Note how they are just transposes of each other.</p>
<p>Practically, to compute the kernel, we need to iterate over the output image. As we discussed in <a href="https://learningjulia.com/2017/02/24/blurring-and-manipulation.html">a previous post</a>, when transforming one image into another, you need to iterate over the output image, and for each pixel, find the pixels from the input image needed to compute that particular pixel. In the case of the Sobel kernel, we need to iterate over the output twice - once for the <code>X</code> direction, which needs 9 pixels for the computation, and once for the <code>Y</code> direction computation, which also needs 9 pixels.</p>
<h2 id="The-imfilter-function">The <code>imfilter</code> function<a class="anchor-link" href="#The-imfilter-function">¶</a></h2><p>To apply image kernels, I am going to use the <code>imfilter</code> function from JuliaImages: <a href="http://juliaimages.github.io/latest/function_reference.html#ImageFiltering.imfilter">http://juliaimages.github.io/latest/function_reference.html#ImageFiltering.imfilter</a>. Rather than manually trying to implement out-of-bounds implementations or worrying about applying a dot product / convolution, let's just use the builtins.</p>
<p>Another awesome feature of the JuliaImages library is the ability to pad the input according to 4 rules:</p>
<ol>
<li><em>replicate</em> - repeat the edge value until infinity</li>
<li><em>circular</em> - image edges "wrap around"</li>
<li><em>symmetric</em> - reflect relative to the required position</li>
<li><em>reflect</em> - reflect relative to the edge</li>
</ol>
<p>Read more here: <a href="http://juliaimages.github.io/latest/function_reference.html#Boundaries-and-padding-1">http://juliaimages.github.io/latest/function_reference.html#Boundaries-and-padding-1</a>. Which you can specify by doing something like:</p>
<pre><code> imfilter(img, kernel, "replicate")</code></pre>
<p>In my case, I will just use the "replicate" mode.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">kernel</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span> <span class="mi">0</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="mi">2</span> <span class="mi">0</span> <span class="o">-</span><span class="mi">2</span><span class="p">;</span><span class="mi">1</span> <span class="mi">0</span> <span class="o">-</span><span class="mi">1</span><span class="p">];</span>
<span class="n">sobel_x</span> <span class="o">=</span> <span class="n">imfilter</span><span class="p">(</span><span class="n">img_gray</span><span class="p">,</span> <span class="n">kernel</span><span class="p">);</span>
<span class="n">grad</span> <span class="o">=</span> <span class="n">imfilter</span><span class="p">(</span><span class="n">sobel_x</span><span class="p">,</span> <span class="n">kernel</span><span class="o">'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[4]:</div>
<div class="output_subarea output_stream output_stderr output_text">
<pre><span class="ansi-red-intense-fg ansi-bold">WARNING: assuming that the origin is at the center of the kernel; to avoid this warning, call `centered(kernel)` or use an OffsetArray</span>
in depwarn</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>There are a few things to note about the <code>imfilter</code> function:</p>
<ol>
<li>It doesn't do <em>convolution</em>. Instead, it does <em>correlation</em>. The difference is basically that in <em>convolution</em> the kernel is flipped, so if you want to do convolution with <code>imfilter</code>, you should do <code>reflect()</code> around your kernel.</li>
<li><p>You need to assign a "center" to the kernels. Normally when we think of kernels we think of the center as being the central number in the kernel - in the Sobel kernels above the center is (1, 1). To do this in Julia, the default calling of <code>imfilter</code> will do this, or you can explicitly instantiate the a kernel by calling <code>centered()</code>:</p>
<pre><code> kernel = centered([1 0 -1; 2 0 -2;1 0 -1]);
imfilter(img_gray, kernel)</code></pre>
<p>But in case you want a different center, you can use the <a href="https://github.com/alsam/OffsetArrays.jl">OffsetArrays</a> package.</p>
</li>
</ol>
<h2 id="Sobel-to-edges">Sobel to edges<a class="anchor-link" href="#Sobel-to-edges">¶</a></h2><p>What you can see from the image after the Sobel kernel is applied in the x and y directions is that there is a lot of noise all over the image - this is because the kernel is only looking at neighborhoods of 3x3. To get around this, we can just take the magnitude of the gradient, or even the 4th power of the gradient.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">grad</span> <span class="o">.^</span> <span class="mi">4</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[5]:</div>
<div class="output_png output_subarea output_execute_result">
<img src="/assets/imgs/04-imfilter/04-imfilter_9_0.png"
>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>As you can see, much less noise.</p>
<h2 id="Separable-kernels">Separable kernels<a class="anchor-link" href="#Separable-kernels">¶</a></h2><p>However, using some interesting properties of the Sobel convolution operation, we can do even better. The Sobel kernel is separable - we can compute the full 3x3 kernel as a multiplication of a 1x3 and 3x1 kernels.</p>
<p>The kernels above ($K_x$ and $K_y$) can each be factored into two 1-dimensional kernels:</p>
$$K_x = \begin{bmatrix} 1 & 0 & -1 \\ 2 & 0 & -2 \\ 1 & 0 & -1 \end{bmatrix} = \begin{bmatrix}1 \\ 2 \\ 1 \end{bmatrix} \cdot \begin{bmatrix}1 & 0 & -1 \end{bmatrix}$$$$K_y = \begin{bmatrix} 1 & 2 & 1 \\ 0 & 0 & 0 \\ -1 & -2 & -1 \end{bmatrix} = \begin{bmatrix}1 \\ 0 \\ -1 \end{bmatrix} \cdot \begin{bmatrix}1 & 2 & 1 \end{bmatrix}$$<p>So we can pass these 4 smaller kernels into <code>imfilter()</code> to get the same result:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">kernel_1</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span> <span class="mi">2</span> <span class="mi">1</span><span class="p">]</span><span class="o">'</span><span class="p">;</span>
<span class="n">kernel_2</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span> <span class="mi">0</span> <span class="o">-</span><span class="mi">1</span><span class="p">];</span>
<span class="n">grad_sep</span> <span class="o">=</span> <span class="n">imfilter</span><span class="p">(</span><span class="n">img_gray</span><span class="p">,</span> <span class="p">(</span><span class="n">kernel_1</span><span class="p">,</span> <span class="n">kernel_2</span><span class="p">,</span> <span class="n">kernel_2</span><span class="o">'</span><span class="p">,</span> <span class="n">kernel_1</span><span class="o">'</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[6]:</div>
<div class="output_png output_subarea output_execute_result">
<img src="/assets/imgs/04-imfilter/04-imfilter_11_0.png"
>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Note the only difference is that the boundaries of the image are pure black. This is probably because of the kernel size - the kernel that is <code>3x1</code> needs only pixels horizontally, not vertically, so the "replication" step of the <code>imfilter</code> code replicates the default value, which is black.</p>
<h2 id="Factoring-Kernels">Factoring Kernels<a class="anchor-link" href="#Factoring-Kernels">¶</a></h2><p>We can do even one better than manually factorizong kernels. We can use a feature built into the <code>imfilter</code> library to automatically factor the kernel itself before calling the <code>imfilter</code> function. You can see a detailed example of how to use it here: <a href="http://juliaimages.github.io/latest/imagefiltering.html#Factored-kernels-1">http://juliaimages.github.io/latest/imagefiltering.html#Factored-kernels-1</a>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [7]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">grad_auto_factoring</span> <span class="o">=</span> <span class="n">imfilter</span><span class="p">(</span><span class="n">img_gray</span><span class="p">,</span> <span class="n">kernelfactors</span><span class="p">((</span><span class="n">kernel</span><span class="p">,</span> <span class="n">kernel</span><span class="o">'</span><span class="p">)))</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[7]:</div>
<div class="output_png output_subarea output_execute_result">
<img src="/assets/imgs/04-imfilter/04-imfilter_13_0.png"
>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>In fact, the <code>imfilter()</code> function implementation automatically tries to factorize the kernels when it is called: <a href="https://github.com/JuliaImages/ImageFiltering.jl/blob/master/src/imfilter.jl#L10">https://github.com/JuliaImages/ImageFiltering.jl/blob/master/src/imfilter.jl#L10</a>, so you don't have to remember to get this functionality for free.</p>
<p>There is one more way to apply Sobel kernels to the image. Of course, <code>imfilter()</code> has a Sobel kernel built in. and it's even automatically separable. The difference as you can see with the built-in <code>sobel</code> kernel is that it is normalized - the kernel is divided by the sum of the kernel (which in this case is 8), so the sum of all the factors in the kernel is equal to 1. This is a common technique in computer vision and image processing, but for visualizing the results here, we will multiply the output by 64 to get the same output image as above (8 for each kernel, and there are 2 kernels in the sobel kernel).</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [8]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">Kernel</span><span class="o">.</span><span class="n">sobel</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[8]:</div>
<div class="output_text output_subarea output_execute_result">
<pre>(
[-0.125 -0.25 -0.125; 0.0 0.0 0.0; 0.125 0.25 0.125],
[-0.125 0.0 0.125; -0.25 0.0 0.25; -0.125 0.0 0.125])</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [9]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">grad_builtin_sobel</span> <span class="o">=</span> <span class="mi">64</span> <span class="o">*</span> <span class="n">imfilter</span><span class="p">(</span><span class="n">img_gray</span><span class="p">,</span> <span class="n">Kernel</span><span class="o">.</span><span class="n">sobel</span><span class="p">())</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[9]:</div>
<div class="output_png output_subarea output_execute_result">
<img src="/assets/imgs/04-imfilter/04-imfilter_16_0.png"
>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="The-notable-lack-of-OpenCV">The notable lack of OpenCV<a class="anchor-link" href="#The-notable-lack-of-OpenCV">¶</a></h2><p>When I first started out on this learning, I thought <a href="https://github.com/JuliaOpenCV/OpenCV.jl">Opencv.jl</a> would have a wrapper around all the functions available in the <a href="http://opencv.org/">OpenCV library</a>. However, the Opencv.jl library is not mature, and is only a manual wrapper around functions available in Opencv. The Sobel kernel is not one of the available functions, so I didn't get a chance to test it! Notably, one of the issues in the Opencv.jl package is to automatically wrap all the Opencv functions: <a href="https://github.com/JuliaOpenCV/OpenCV.jl/issues/7">https://github.com/JuliaOpenCV/OpenCV.jl/issues/7</a>. I can't wait!</p>
<h2 id="Timings">Timings<a class="anchor-link" href="#Timings">¶</a></h2><p>Now let's compare the timings of all the methods. The Julia documentation talks about timing code regularly with the <code>@time</code> macro, and using that to guide development: <a href="http://docs.julialang.org/en/stable/manual/performance-tips/#measure-performance-with-time-and-pay-attention-to-memory-allocation">http://docs.julialang.org/en/stable/manual/performance-tips/#measure-performance-with-time-and-pay-attention-to-memory-allocation</a>. The most important thing to note about the <code>@time</code> macro is that first time it is called will also time all the compilation time needed for any code that is being timed AND code that actually will be timed. So to get an accurate reading, you should call <code>@time</code> twice. It also tells you how much memory is allocated.</p>
<p>Sometimes the output of <code>@time</code> will say something like <code>40.71% gc time</code> - this means that 40% of the time is spent garbage-collecting unused variables and freeing memory. So you can either ignore those runs or ignore that amount of time in your final analysis.</p>
<p>We want to time all these runs:</p>
<ol>
<li>Manually calling <code>imfilter</code> with the full kernel</li>
<li>Manually calling <code>imfilter</code> with the manually-factored kernel</li>
<li>Calling <code>imfilter</code> with an explicit call to <code>kernelfactor</code></li>
</ol>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [10]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="c"># scenario 1</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">5</span>
<span class="nd">@time</span> <span class="n">imfilter</span><span class="p">(</span><span class="n">img_gray</span><span class="p">,</span> <span class="p">(</span><span class="n">kernel</span><span class="p">,</span> <span class="n">kernel</span><span class="o">'</span><span class="p">));</span>
<span class="k">end</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[10]:</div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
0.023770 seconds (188 allocations: 2.087 MB)
0.020832 seconds (188 allocations: 2.087 MB)
0.018937 seconds (188 allocations: 2.087 MB)
0.017686 seconds (188 allocations: 2.087 MB)
0.016757 seconds (188 allocations: 2.087 MB)
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [11]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="c"># scenario 2</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">5</span>
<span class="nd">@time</span> <span class="n">imfilter</span><span class="p">(</span><span class="n">img_gray</span><span class="p">,</span> <span class="p">(</span><span class="n">kernel_1</span><span class="p">,</span> <span class="n">kernel_2</span><span class="p">,</span> <span class="n">kernel_2</span><span class="o">'</span><span class="p">,</span> <span class="n">kernel_1</span><span class="o">'</span><span class="p">));</span>
<span class="k">end</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[11]:</div>
<div class="output_subarea output_stream output_stdout output_text">
<pre> 0.012856 seconds (1.64 k allocations: 2.164 MB)
0.012849 seconds (1.64 k allocations: 2.164 MB)
0.019774 seconds (1.64 k allocations: 2.164 MB, 35.54% gc time)
0.012376 seconds (1.64 k allocations: 2.164 MB)
0.012353 seconds (1.64 k allocations: 2.164 MB)
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [12]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="c"># scenario 3</span>
<span class="k">for</span> <span class="n">i</span> <span class="kp">in</span> <span class="mi">1</span><span class="o">:</span><span class="mi">5</span>
<span class="nd">@time</span> <span class="mi">64</span> <span class="o">*</span> <span class="n">imfilter</span><span class="p">(</span><span class="n">img_gray</span><span class="p">,</span> <span class="n">Kernel</span><span class="o">.</span><span class="n">sobel</span><span class="p">())</span>
<span class="k">end</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[12]:</div>
<div class="output_subarea output_stream output_stdout output_text">
<pre> 0.016268 seconds (300 allocations: 6.164 MB)
0.012701 seconds (300 allocations: 6.164 MB)
0.016306 seconds (300 allocations: 6.164 MB, 27.14% gc time)
0.009772 seconds (300 allocations: 6.164 MB)
0.008432 seconds (304 allocations: 6.164 MB)
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Based on the timing results, we can see that manually trying to factor yields the <em>WORST</em> results! Julia does a lot of the heavy lifting behind-the-scenes with function inlining and compiling. For the kernel that is built-in, like the Sobel kernel (<a href="https://github.com/JuliaImages/ImageFiltering.jl/blob/master/src/kernelfactors.jl#L151">https://github.com/JuliaImages/ImageFiltering.jl/blob/master/src/kernelfactors.jl#L151</a>), you can actually see that the factors are hand-coded, so it will naturally be faster.</p>
<p>But you never know if this will be true in general! So from what I can tell, the <code>@time</code> macro is critical for development.</p>
<h2 id="Wrapping-up-with-gradients">Wrapping up with gradients<a class="anchor-link" href="#Wrapping-up-with-gradients">¶</a></h2><p>I originally intended this exploration to be USING the image gradient rather than all about computing it, but instead it turned into an explanation of <code>@time</code> and a divergence into array notation (see below), so next time I will actually use the image gradient to do some fun image manipulation!</p>
<h2 id="An-aside-on-array-notation">An aside on array notation<a class="anchor-link" href="#An-aside-on-array-notation">¶</a></h2><p>The major learning from this post was actually about how array notation works in Julia. When I was first trying to get the kernel factoring calls working, I was having a problem with a particular error:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [13]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">imfilter</span><span class="p">(</span><span class="n">img_gray</span><span class="p">,</span> <span class="p">([</span><span class="mi">1</span><span class="p">;</span><span class="mi">2</span><span class="p">;</span><span class="mi">1</span><span class="p">]))</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[13]:</div>
<div class="output_subarea output_stream output_stderr output_text">
<pre><span class="ansi-red-intense-fg ansi-bold">WARNING: assuming that the origin is at the center of the kernel; to avoid this warning, call `centered(kernel)` or use an OffsetArray</span>
in depwarn(::String, ::Symbol) at ./deprecated.jl:64
in _kernelshift at /home/mprat/.julia/v0.5/ImageFiltering/src/imfilter.jl:1048 [inlined]
in kernelshift at /home/mprat/.julia/v0.5/ImageFiltering/src/imfilter.jl:1045 [inlined]
in factorkernel at /home/mprat/.julia/v0.5/ImageFiltering/src/imfilter.jl:1015 [inlined]
in imfilter at /home/mprat/.julia/v0.5/ImageFiltering/src/imfilter.jl:10 [inlined]
in imfilter(::Array{ColorTypes.Gray{FixedPointNumbers.Normed{UInt8,8}},2}, ::Array{Int64,1}) at /home/mprat/.julia/v0.5/ImageFiltering/src/imfilter.jl:5
in include_string(::String, ::String) at ./loading.jl:441
in execute_request(::ZMQ.Socket, ::IJulia.Msg) at /home/mprat/.julia/v0.5/IJulia/src/execute_request.jl:157
in eventloop(::ZMQ.Socket) at /home/mprat/.julia/v0.5/IJulia/src/eventloop.jl:8
in (::IJulia.##13#19)() at ./task.jl:360
while loading In[13], in expression starting on line 1
</pre>
</div>
</div>
<div class="output_area">
<div class="prompt output_prompt">Out[13]:</div>
<div class="output_subarea output_text output_error">
<pre>
ArgumentError: ImageFiltering.Pad{1}(:replicate,(1,),(1,)) lacks the proper padding sizes for an array with 2 dimensions
in padindices(::Array{ColorTypes.Gray{FixedPointNumbers.Normed{UInt8,8}},2}, ::ImageFiltering.Pad{1}) at /home/mprat/.julia/v0.5/ImageFiltering/src/border.jl:119
in padarray at /home/mprat/.julia/v0.5/ImageFiltering/src/border.jl:145 [inlined]
in imfilter! at /home/mprat/.julia/v0.5/ImageFiltering/src/imfilter.jl:234 [inlined]
in imfilter!(::Array{ColorTypes.Gray{Float32},2}, ::Array{ColorTypes.Gray{FixedPointNumbers.Normed{UInt8,8}},2}, ::Tuple{OffsetArrays.OffsetArray{Int64,1,Array{Int64,1}}}, ::ImageFiltering.Pad{0}, ::ImageFiltering.Algorithm.FIR) at /home/mprat/.julia/v0.5/ImageFiltering/src/imfilter.jl:145
in imfilter!(::Array{ColorTypes.Gray{Float32},2}, ::Array{ColorTypes.Gray{FixedPointNumbers.Normed{UInt8,8}},2}, ::Tuple{OffsetArrays.OffsetArray{Int64,1,Array{Int64,1}}}, ::ImageFiltering.Pad{0}) at /home/mprat/.julia/v0.5/ImageFiltering/src/imfilter.jl:139
in imfilter(::Array{ColorTypes.Gray{FixedPointNumbers.Normed{UInt8,8}},2}, ::Array{Int64,1}) at /home/mprat/.julia/v0.5/ImageFiltering/src/imfilter.jl:5</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Contrast to this:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [14]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="n">imfilter</span><span class="p">(</span><span class="n">img_gray</span><span class="p">,</span> <span class="p">([</span><span class="mi">1</span> <span class="mi">2</span> <span class="mi">1</span><span class="p">]))</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[14]:</div>
<div class="output_png output_subarea output_execute_result">
<img src="/assets/imgs/04-imfilter/04-imfilter_24_0.png"
>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>I couldn't figure out what was happening. But what I realized was that the actual type that was returned when the kernel array was created was different. Take a look:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [15]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="p">[</span><span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">]</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[15]:</div>
<div class="output_text output_subarea output_execute_result">
<pre>1×3 Array{Int64,2}:
1 2 3</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [16]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="p">[</span><span class="mi">1</span><span class="p">;</span> <span class="mi">2</span><span class="p">;</span> <span class="mi">3</span><span class="p">;]</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[16]:</div>
<div class="output_text output_subarea output_execute_result">
<pre>3-element Array{Int64,1}:
1
2
3</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [17]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="p">[[</span><span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">];</span> <span class="p">[</span><span class="mi">4</span> <span class="mi">5</span> <span class="mi">6</span><span class="p">]]</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[17]:</div>
<div class="output_text output_subarea output_execute_result">
<pre>2×3 Array{Int64,2}:
1 2 3
4 5 6</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>My original thought was that <code>;</code> means "new row" and space means "new column", but I was wrong. Actually, the distinction is between vertical and horizontal concatenation. So while <code>[1 2 3]</code> gives me a <code>1x3</code> array, I expected <code>[1; 2; 3;]</code> to give me a <code>3x1</code> array, but it doesn't - it returns a 3-element array instead. To get a <code>3x1</code> array I need to do <code>[1 2 3]'</code>:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [18]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-julia"><pre><span></span><span class="p">[</span><span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">]</span><span class="o">'</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[18]:</div>
<div class="output_text output_subarea output_execute_result">
<pre>3×1 Array{Int64,2}:
1
2
3</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>I actually thought this was inconsistent and filed an issue on the Julia issue tracker: <a href="https://github.com/JuliaLang/julia/issues/20957">https://github.com/JuliaLang/julia/issues/20957</a>. But it turns out I was obviously not the first one to notice this or report it. In fact, there is a whole thread on the <a href="http://discourse.julialang.org">Julia Discourse forums</a> that I am now following: <a href="https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938">https://discourse.julialang.org/t/whats-the-meaning-of-the-array-syntax/938</a>, and would be interested in learning how it gets fixed. I will be following this issue closely as the language evolves!</p>
<p>I think consistency would go a long way to making newbies not confused with new syntax. In any case, I learned something new.</p>
</div>
</div>
</div>