-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotebook_hw1.jl
1614 lines (1300 loc) · 48 KB
/
notebook_hw1.jl
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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
### A Pluto.jl notebook ###
# v0.11.14
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing
el
end
end
# ╔═╡ 83eb9ca0-ed68-11ea-0bc5-99a09c68f867
md"_homework 1, version 4_"
# ╔═╡ ac8ff080-ed61-11ea-3650-d9df06123e1f
md"""
# **Homework 1** - _convolutions_
`18.S191`, fall 2020
This notebook contains _built-in, live answer checks_! In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again.
_For MIT students:_ there will also be some additional (secret) test cases that will be run as part of the grading process, and we will look at your notebook and write comments.
Feel free to ask questions!
"""
# ╔═╡ 911ccbce-ed68-11ea-3606-0384e7580d7c
# edit the code below to set your name and kerberos ID (i.e. email without @mit.edu)
student = (name = "Andreas Theodosiou", kerberos_id = "at694")
# press the ▶ button in the bottom right of this cell to run your edits
# or use Shift+Enter
# you might need to wait until all other cells in this notebook have completed running.
# scroll down the page to see what's up
# ╔═╡ 8ef13896-ed68-11ea-160b-3550eeabbd7d
md"""
Submission by: **_$(student.name)_** ($(student.kerberos_id)@mit.edu)
"""
# ╔═╡ 5f95e01a-ee0a-11ea-030c-9dba276aba92
md"_Let's create a package environment:_"
# ╔═╡ 65780f00-ed6b-11ea-1ecf-8b35523a7ac0
begin
import Pkg
Pkg.activate(mktempdir())
end
# ╔═╡ 74b008f6-ed6b-11ea-291f-b3791d6d1b35
begin
Pkg.add(["Images", "ImageMagick"])
using Images
end
# ╔═╡ 6b30dc38-ed6b-11ea-10f3-ab3f121bf4b8
begin
Pkg.add("PlutoUI")
using PlutoUI
end
# ╔═╡ 67461396-ee0a-11ea-3679-f31d46baa9b4
md"_We set up Images.jl again:_"
# ╔═╡ 540ccfcc-ee0a-11ea-15dc-4f8120063397
md"""
## **Exercise 1** - _Manipulating vectors (1D images)_
A `Vector` is a 1D array. We can think of that as a 1D image.
"""
# ╔═╡ 467856dc-eded-11ea-0f83-13d939021ef3
example_vector = [0.5, 0.4, 0.3, 0.2, 0.1, 0.0, 0.7, 0.0, 0.7, 0.9]
# ╔═╡ ad6a33b0-eded-11ea-324c-cfabfd658b56
md"#### Exerise 1.1
👉 Make a random vector `random_vect` of length 10 using the `rand` function.
"
# ╔═╡ f51333a6-eded-11ea-34e6-bfbb3a69bcb0
random_vect = rand(1:10, 10) # replace this with your code!
# ╔═╡ cf738088-eded-11ea-2915-61735c2aa990
md"👉 Make a function `mean` using a `for` loop, which computes the mean/average of a vector of numbers."
# ╔═╡ bb3143ac-f924-11ea-216f-bd39f9c33bdf
function sum_to(n)
s = 0 # new local
for i = 1:n
s = s + i # assign existing local
end
return s # same local
end
# ╔═╡ 0ffa8354-edee-11ea-2883-9d5bfea4a236
function mean(x)
mn = 0
for a in x
mn += a
end
return mn / length(x)
end
# ╔═╡ 1f104ce4-ee0e-11ea-2029-1d9c817175af
mean([1, 2, 3])
# ╔═╡ 1f229ca4-edee-11ea-2c56-bb00cc6ea53c
md"👉 Define `m` to be the mean of `random_vect`."
# ╔═╡ 2a391708-edee-11ea-124e-d14698171b68
m = mean(random_vect)
# ╔═╡ e2863d4c-edef-11ea-1d67-332ddca03cc4
md"""👉 Write a function `demean`, which takes a vector `x` and subtracts the mean from each value in `x`."""
# ╔═╡ ec5efe8c-edef-11ea-2c6f-afaaeb5bc50c
function demean(x)
return x .- mean(x)
end
# ╔═╡ 29e10640-edf0-11ea-0398-17dbf4242de3
md"Let's check that the mean of the `demean(random_vect)` is 0:
_Due to floating-point round-off error it may *not* be *exactly* 0._"
# ╔═╡ 6f67657e-ee1a-11ea-0c2f-3d567bcfa6ea
if ismissing(random_vect)
md"""
!!! info
The following cells error because `random_vect` is not yet defined. Have you done the first exercise?
"""
end
# ╔═╡ 73ef1d50-edf0-11ea-343c-d71706874c82
copy_of_random_vect = copy(random_vect); # in case demean modifies `x`
# ╔═╡ 38155b5a-edf0-11ea-3e3f-7163da7433fb
mean(demean(copy_of_random_vect))
# ╔═╡ a5f8bafe-edf0-11ea-0da3-3330861ae43a
md"""
#### Exercise 1.2
👉 Generate a vector of 100 zeros. Change the center 20 elements to 1.
"""
# ╔═╡ b6b65b94-edf0-11ea-3686-fbff0ff53d08
function create_bar()
hundred_zeros = zeros(100)
n = length(hundred_zeros) ÷ 2
hundred_zeros[n-20÷2:n+20÷2] .= 1
return hundred_zeros
end
# ╔═╡ 22f28dae-edf2-11ea-25b5-11c369ae1253
md"""
#### Exercise 1.3
👉 Write a function that turns a `Vector` of `Vector`s into a `Matrix`.
"""
# ╔═╡ 11e8f422-f986-11ea-1afa-79293188d743
A = [ x+y for x=1:3, y=1:4 ]
# ╔═╡ 8c19fb72-ed6c-11ea-2728-3fa9219eddc4
function vecvec_to_matrix(vecvec)
return [ vecvec[y][x] for y=1:length(vecvec), x=1:length(vecvec[1]) ]
end
# ╔═╡ c4761a7e-edf2-11ea-1e75-118e73dadbed
vecvec_to_matrix([[1,2], [3,4]])
# ╔═╡ 393667ca-edf2-11ea-09c5-c5d292d5e896
md"""
👉 Write a function that turns a `Matrix` into a`Vector` of `Vector`s .
"""
# ╔═╡ 9f1c6d04-ed6c-11ea-007b-75e7e780703d
function matrix_to_vecvec(matrix)
return [row for row in eachrow(matrix)]
end
# ╔═╡ 70955aca-ed6e-11ea-2330-89b4d20b1795
matrix_to_vecvec([6 7; 8 9])
# ╔═╡ 5da8cbe8-eded-11ea-2e43-c5b7cc71e133
begin
colored_line(x::Vector{<:Real}) = Gray.(Float64.((hcat(x)')))
colored_line(x::Any) = nothing
end
# ╔═╡ 56ced344-eded-11ea-3e81-3936e9ad5777
colored_line(example_vector)
# ╔═╡ b18e2c54-edf1-11ea-0cbf-85946d64b6a2
colored_line(random_vect)
# ╔═╡ d862fb16-edf1-11ea-36ec-615d521e6bc0
colored_line(create_bar())
# ╔═╡ e083b3e8-ed61-11ea-2ec9-217820b0a1b4
md"""
## **Exercise 2** - _Manipulating images_
In this exercise we will get familiar with matrices (2D arrays) in Julia, by manipulating images.
Recall that in Julia images are matrices of `RGB` color objects.
Let's load a picture of Philip again.
"""
# ╔═╡ c5484572-ee05-11ea-0424-f37295c3072d
philip_file = download("https://i.imgur.com/VGPeJ6s.jpg")
# ╔═╡ e86ed944-ee05-11ea-3e0f-d70fc73b789c
md"_Hi there Philip_"
# ╔═╡ 807d6d1e-fac6-11ea-072f-d770e5e5eddb
md"""
#### Exercise 2.1
👉 Write a function **`mean_colors`** that accepts an object called `image`. It should calculate the mean (average) amounts of red, green and blue in the image and return a tuple `(r, g, b)` of those means.
"""
# ╔═╡ abbe9b0e-fac8-11ea-1803-23f451f7fa1b
RGBX(0, 0, 0)
# ╔═╡ f6898df6-ee07-11ea-2838-fde9bc739c11
function mean_colors(image)
mn = RGB(0, 0, 0)
for pixel in image
mn += pixel
end
mn /= length(image)
return (mn.r, mn.g, mn.b)
end
# ╔═╡ f68d4a36-ee07-11ea-0832-0360530f102e
md"""
#### Exercise 2.2
👉 Look up the documentation on the `floor` function. Use it to write a function `quantize(x::Number)` that takes in a value $x$ (which you can assume is between 0 and 1) and "quantizes" it into bins of width 0.1. For example, check that 0.267 gets mapped to 0.2.
"""
# ╔═╡ f6991a50-ee07-11ea-0bc4-1d68eb028e6a
begin
function quantize(x::Number)
return floor(x, digits=1)
end
function quantize(color::AbstractRGB)
return RGB(quantize(color.r), quantize(color.g), quantize(color.b))
end
function quantize(image::AbstractMatrix)
# you will write me in a later exercise!
return quantize.(image)
end
end
# ╔═╡ f6a655f8-ee07-11ea-13b6-43ca404ddfc7
quantize(0.267), quantize(0.91)
# ╔═╡ f6b218c0-ee07-11ea-2adb-1968c4fd473a
md"""
#### Exercise 2.3
👉 Write the second **method** of the function `quantize`, i.e. a new *version* of the function with the *same* name. This method will accept a color object called `color`, of the type `AbstractRGB`.
_Write the function in the same cell as `quantize(x::Number)` from the last exercise. 👆_
Here, `::AbstractRGB` is a **type annotation**. This ensures that this version of the function will be chosen when passing in an object whose type is a **subtype** of the `AbstractRGB` abstract type. For example, both the `RGB` and `RGBX` types satisfy this.
The method you write should return a new `RGB` object, in which each component ($r$, $g$ and $b$) are quantized.
"""
# ╔═╡ f6bf64da-ee07-11ea-3efb-05af01b14f67
md"""
#### Exercise 2.4
👉 Write a method `quantize(image::AbstractMatrix)` that quantizes an image by quantizing each pixel in the image. (You may assume that the matrix is a matrix of color objects.)
_Write the function in the same cell as `quantize(x::Number)` from the last exercise. 👆_
"""
# ╔═╡ 25dad7ce-ee0b-11ea-3e20-5f3019dd7fa3
md"Let's apply your method!"
# ╔═╡ f6cc03a0-ee07-11ea-17d8-013991514d42
md"""
#### Exercise 2.5
👉 Write a function `invert` that inverts a color, i.e. sends $(r, g, b)$ to $(1 - r, 1-g, 1-b)$.
"""
# ╔═╡ 63e8d636-ee0b-11ea-173d-bd3327347d55
function invert(color::AbstractRGB)
return RGB(1 - color.r, 1 - color.g, 1 - color.b)
end
# ╔═╡ 2cc2f84e-ee0d-11ea-373b-e7ad3204bb00
md"Let's invert some colors:"
# ╔═╡ b8f26960-ee0a-11ea-05b9-3f4bc1099050
black = RGB(0.0, 0.0, 0.0)
# ╔═╡ 5de3a22e-ee0b-11ea-230f-35df4ca3c96d
invert(black)
# ╔═╡ 4e21e0c4-ee0b-11ea-3d65-b311ae3f98e9
red = RGB(0.8, 0.1, 0.1)
# ╔═╡ 6dbf67ce-ee0b-11ea-3b71-abc05a64dc43
invert(red)
# ╔═╡ 846b1330-ee0b-11ea-3579-7d90fafd7290
md"Can you invert the picture of Philip?"
# ╔═╡ f6d6c71a-ee07-11ea-2b63-d759af80707b
md"""
#### Exercise 2.6
👉 Write a function `noisify(x::Number, s)` to add randomness of intensity $s$ to a value $x$, i.e. to add a random value between $-s$ and $+s$ to $x$. If the result falls outside the range $(0, 1)$ you should "clamp" it to that range. (Note that Julia has a `clamp` function, but you should write your own function `myclamp(x)`.)
"""
# ╔═╡ eff43f76-fb82-11ea-24de-5780bd2c6e39
function myclamp(x::Number, lo::Number, hi::Number)
if lo <= x <= hi
return x
elseif x < lo
return lo
else
return hi
end
end
# ╔═╡ c1e713c8-fb83-11ea-3fd9-f1b1c7d22318
a = RGB(0.2, 0.3, 0.4)
# ╔═╡ f6e2cb2a-ee07-11ea-06ee-1b77e34c1e91
begin
function noisify(x::Number, s)
return clamp(x + rand(-s:0.01:s), 0, 1)
end
function noisify(color::AbstractRGB, s)
return RGB(noisify(color.r, s), noisify(color.b, s), noisify(color.b, s))
end
function noisify(image::AbstractMatrix, s)
return noisify.(image, s)
end
end
# ╔═╡ f6fc1312-ee07-11ea-39a0-299b67aee3d8
md"""
👉 Write the second method `noisify(c::AbstractRGB, s)` to add random noise of intensity $s$ to each of the $(r, g, b)$ values in a colour.
_Write the function in the same cell as `noisify(x::Number)` from the last exercise. 👆_
"""
# ╔═╡ 774b4ce6-ee1b-11ea-2b48-e38ee25fc89b
@bind color_noise Slider(0:0.01:1, show_value=true)
# ╔═╡ 7e4aeb70-ee1b-11ea-100f-1952ba66f80f
noisify(red, color_noise)
# ╔═╡ 6a05f568-ee1b-11ea-3b6c-83b6ada3680f
# ╔═╡ f70823d2-ee07-11ea-2bb3-01425212aaf9
md"""
👉 Write the third method `noisify(image::AbstractMatrix, s)` to noisify each pixel of an image.
_Write the function in the same cell as `noisify(x::Number)` from the last exercise. 👆_
"""
# ╔═╡ e70a84d4-ee0c-11ea-0640-bf78653ba102
@bind philip_noise Slider(0:0.01:8, show_value=true)
# ╔═╡ 9604bc44-ee1b-11ea-28f8-7f7af8d0cbb2
# ╔═╡ f714699e-ee07-11ea-08b6-5f5169861b57
md"""
👉 For which noise intensity does it become unrecognisable?
You may need noise intensities larger than 1. Why?
"""
# ╔═╡ bdc2df7c-ee0c-11ea-2e9f-7d2c085617c1
answer_about_noise_intensity = md"""
The image is unrecognisable with intensity 4.29. As the magnitude of the noise increases beyond the range of the RGB values, each pixel if moved very far away from its original value to the point were it is unrecongisable. Thus, the image becomes unrecognisable.
"""
# ╔═╡ 81510a30-ee0e-11ea-0062-8b3327428f9d
# ╔═╡ e3b03628-ee05-11ea-23b6-27c7b0210532
decimate(image, ratio=5) = image[1:ratio:end, 1:ratio:end]
# ╔═╡ c8ecfe5c-ee05-11ea-322b-4b2714898831
philip = let
original = Images.load(philip_file)
decimate(original, 8)
end
# ╔═╡ 5be9b144-ee0d-11ea-2a8d-8775de265a1d
mean_colors(philip)
# ╔═╡ 9751586e-ee0c-11ea-0cbb-b7eda92977c9
quantize(philip)
# ╔═╡ 943103e2-ee0b-11ea-33aa-75a8a1529931
philip_inverted = invert.(philip)
# ╔═╡ ac15e0d0-ee0c-11ea-1eaf-d7f88b5df1d7
noisify(philip, philip_noise)
# ╔═╡ e08781fa-ed61-11ea-13ae-91a49b5eb74a
md"""
## **Exercise 3** - _Convolutions_
As we have seen in the videos, we can produce cool effects using the mathematical technique of **convolutions**. We input one image $M$ and get a new image $M'$ back.
Conceptually we think of $M$ as a matrix. In practice, in Julia it will be a `Matrix` of color objects, and we may need to take that into account. Ideally, however, we should write a **generic** function that will work for any type of data contained in the matrix.
A convolution works on a small **window** of an image, i.e. a region centered around a given point $(i, j)$. We will suppose that the window is a square region with odd side length $2\ell + 1$, running from $-\ell, \ldots, 0, \ldots, \ell$.
The result of the convolution over a given window, centred at the point $(i, j)$ is a *single number*; this number is the value that we will use for $M'_{i, j}$.
(Note that neighbouring windows overlap.)
To get started let's restrict ourselves to convolutions in 1D.
So a window is just a 1D region from $-\ell$ to $\ell$.
"""
# ╔═╡ 7fc8ee1c-ee09-11ea-1382-ad21d5373308
md"""
---
Let's create a vector `v` of random numbers of length `n=100`.
"""
# ╔═╡ 7fcd6230-ee09-11ea-314f-a542d00d582e
n = 100
# ╔═╡ 7fdb34dc-ee09-11ea-366b-ffe10d1aa845
v = rand(n)
# ╔═╡ 7fe9153e-ee09-11ea-15b3-6f24fcc20734
md"_Feel free to experiment with different values!_"
# ╔═╡ 80108d80-ee09-11ea-0368-31546eb0d3cc
md"""
#### Exercise 3.1
You've seen some colored lines in this notebook to visualize arrays. Can you make another one?
👉 Try plotting our vector `v` using `colored_line(v)`.
"""
# ╔═╡ 01070e28-ee0f-11ea-1928-a7919d452bdd
colored_line(v)
# ╔═╡ 7522f81e-ee1c-11ea-35af-a17eb257ff1a
md"Try changing `n` and `v` around. Notice that you can run the cell `v = rand(n)` again to regenerate new random values."
# ╔═╡ 801d90c0-ee09-11ea-28d6-61b806de26dc
md"""
#### Exercise 3.2
We need to decide how to handle the **boundary conditions**, i.e. what happens if we try to access a position in the vector `v` beyond `1:n`. The simplest solution is to assume that $v_{i}$ is 0 outside the original vector; however, this may lead to strange boundary effects.
A better solution is to use the *closest* value that is inside the vector. Effectively we are extending the vector and copying the extreme values into the extended positions. (Indeed, this is one way we could implement this; these extra positions are called **ghost cells**.)
👉 Write a function `extend(v, i)` that checks whether the position $i$ is inside `1:n`. If so, return the $i$th component of `v`; otherwise, return the nearest end value.
"""
# ╔═╡ 802bec56-ee09-11ea-043e-51cf1db02a34
function extend(v, i)
if i ∈ 1:length(v)
return v[i]
else
return v[clamp(i, 1, length(v))]
end
end
# ╔═╡ 28e20950-ee0c-11ea-0e0a-b5f2e570b56e
begin
using LinearAlgebra
function convolve_vector(v, k)
lₖ = length(k)
l = (lₖ - 1) ÷ 2
extended_v = [extend(v, j) for i ∈ 1:length(v) for j ∈ i-l:i+l]
return [extended_v[i:i+lₖ-1] ⋅ k for i ∈ 1:lₖ:length(extended_v)]
end
end
# ╔═╡ b7f3994c-ee1b-11ea-211a-d144db8eafc2
md"_Some test cases:_"
# ╔═╡ 803905b2-ee09-11ea-2d52-e77ff79693b0
extend(v, 1)
# ╔═╡ 80479d98-ee09-11ea-169e-d166eef65874
extend(v, -8)
# ╔═╡ 805691ce-ee09-11ea-053d-6d2e299ee123
extend(v, n + 10)
# ╔═╡ 806e5766-ee0f-11ea-1efc-d753cd83d086
md"Extended with 0:"
# ╔═╡ 38da843a-ee0f-11ea-01df-bfa8b1317d36
colored_line([0, 0, example_vector..., 0, 0])
# ╔═╡ 9bde9f92-ee0f-11ea-27f8-ffef5fce2b3c
md"Extended with your `extend`:"
# ╔═╡ 45c4da9a-ee0f-11ea-2c5b-1f6704559137
if extend(v,1) === missing
missing
else
colored_line([extend(example_vector, i) for i in -1:12])
end
# ╔═╡ 80664e8c-ee09-11ea-0702-711bce271315
md"""
#### Exercise 3.3
👉 Write a function `blur_1D(v, l)` that blurs a vector `v` with a window of length `l` by averaging the elements within a window from $-\ell$ to $\ell$. This is called a **box blur**.
"""
# ╔═╡ 807e5662-ee09-11ea-3005-21fdcc36b023
function blur_1D(v, l)
#return [ mean(extend(v[i-1:i+1])) for i in 1:length(v) ]
side = 2 * l + 1
extended = [extend(v, j) for i ∈ 1:length(v) for j ∈ i-l:i+l]
return [ mean(extended[i:i+side-1]) for i ∈ 1:side:length(extended) ]
end
# ╔═╡ 808deca8-ee09-11ea-0ee3-1586fa1ce282
let
try
test_v = rand(n)
original = copy(test_v)
blur_1D(test_v, 5)
if test_v != original
md"""
!!! danger "Oopsie!"
It looks like your function _modifies_ `v`. Can you write it without doing so? Maybe you can use `copy`.
"""
end
catch
end
end
# ╔═╡ 809f5330-ee09-11ea-0e5b-415044b6ac1f
md"""
#### Exercise 3.4
👉 Apply the box blur to your vector `v`. Show the original and the new vector by creating two cells that call `colored_line`. Make the parameter $\ell$ interactive, and call it `l_box` instead of just `l` to avoid a variable naming conflict.
"""
# ╔═╡ 0d99f92a-fd0c-11ea-24de-e3882f0a98d3
colored_line(v)
# ╔═╡ 7d6f50fe-fd0c-11ea-0fb3-5dede91605e2
@bind l_box Slider(0:50, show_value=true)
# ╔═╡ ca1ac5f4-ee1c-11ea-3d00-ff5268866f87
blured_v = blur_1D(v, l_box)
# ╔═╡ 14a64d72-fd0c-11ea-0f0c-4593f0e779c5
colored_line(blured_v)
# ╔═╡ 80ab64f4-ee09-11ea-29b4-498112ed0799
md"""
#### Exercise 3.5
The box blur is a simple example of a **convolution**, i.e. a linear function of a window around each point, given by
$$v'_{i} = \sum_{n} \, v_{i - n} \, k_{n},$$
where $k$ is a vector called a **kernel**.
Again, we need to take care about what happens if $v_{i -n }$ falls off the end of the vector.
👉 Write a function `convolve_vector(v, k)` that performs this convolution. You need to think of the vector $k$ as being *centred* on the position $i$. So $n$ in the above formula runs between $-\ell$ and $\ell$, where $2\ell + 1$ is the length of the vector $k$. You will need to do the necessary manipulation of indices.
"""
# ╔═╡ 93284f92-ee12-11ea-0342-833b1a30625c
test_convolution = let
v = [1, 10, 100, 1000, 10000]
k = [0, 1, 0]
convolve_vector(v, k)
end
# ╔═╡ 5eea882c-ee13-11ea-0d56-af81ecd30a4a
colored_line(test_convolution)
# ╔═╡ cf73f9f8-ee12-11ea-39ae-0107e9107ef5
md"_Edit the cell above, or create a new cell with your own test cases!_"
# ╔═╡ 80b7566a-ee09-11ea-3939-6fab470f9ec8
md"""
#### Exercise 3.6
👉 Write a function `gaussian_kernel`.
The definition of a Gaussian in 1D is
$$G(x) = \frac{1}{\sqrt{2\pi \sigma^2}} \exp \left( \frac{-x^2}{2\sigma^2} \right)$$
We need to **sample** (i.e. evaluate) this at each pixel in a region of size $n^2$,
and then **normalize** so that the sum of the resulting kernel is 1.
For simplicity you can take $\sigma=1$.
"""
# ╔═╡ 1c8b4658-ee0c-11ea-2ede-9b9ed7d3125e
function gaussian_kernel(n)
σ = 1
l = (n - 1) ÷ 2
_kernel = [ 1 / √(2 * π * σ^2) * exp(-x^2 / (2 * σ^2)) for x ∈ -l:+l ]
return _kernel ./ sum(_kernel)
end
# ╔═╡ f8bd22b8-ee14-11ea-04aa-ab16fd01826e
md"Let's test your kernel function!"
# ╔═╡ 2a9dd06a-ee13-11ea-3f84-67bb309c77a8
@bind gaussian_kernel_size_1D Slider(3:1:50, show_value=true) # change this value, or turn me into a slider!
# ╔═╡ 38eb92f6-ee13-11ea-14d7-a503ac04302e
test_gauss_1D_a = let
v = random_vect
k = gaussian_kernel(gaussian_kernel_size_1D)
if k !== missing
convolve_vector(v, k)
end
end
# ╔═╡ b424e2aa-ee14-11ea-33fa-35491e0b9c9d
colored_line(test_gauss_1D_a)
# ╔═╡ 24c21c7c-ee14-11ea-1512-677980db1288
test_gauss_1D_b = let
v = create_bar()
k = gaussian_kernel(gaussian_kernel_size_1D)
if k !== missing
convolve_vector(v, k)
end
end
# ╔═╡ bc1c20a4-ee14-11ea-3525-63c9fa78f089
colored_line(test_gauss_1D_b)
# ╔═╡ b01858b6-edf3-11ea-0826-938d33c19a43
md"""
## **Exercise 4** - _Convolutions of images_
Now let's move to 2D images. The convolution is then given by a **kernel** matrix $K$:
$$M'_{i, j} = \sum_{k, l} \, M_{i- k, j - l} \, K_{k, l},$$
where the sum is over the possible values of $k$ and $l$ in the window. Again we think of the window as being *centered* at $(i, j)$.
A common notation for this operation is $*$:
$$M' = M * K.$$
"""
# ╔═╡ 7c1bc062-ee15-11ea-30b1-1b1e76520f13
md"""
#### Exercise 4.1
👉 Write a function `extend_mat` that takes a matrix `M` and indices `i` and `j`, and returns the closest element of the matrix.
"""
# ╔═╡ 7c2ec6c6-ee15-11ea-2d7d-0d9401a5e5d1
function extend_mat(M::AbstractMatrix, i, j)
if i ∈ 1:size(M, 1) && j ∈ 1:size(M, 2)
return M[i, j]
else
return M[clamp(i, 1, size(M, 1)), clamp(j, 1, size(M, 2))]
end
end
# ╔═╡ 9afc4dca-ee16-11ea-354f-1d827aaa61d2
md"_Let's test it!_"
# ╔═╡ cf6b05e2-ee16-11ea-3317-8919565cb56e
small_image = Gray.(rand(5,5))
# ╔═╡ e3616062-ee27-11ea-04a9-b9ec60842a64
md"Extended with `0`:"
# ╔═╡ e5b6cd34-ee27-11ea-0d60-bd4796540b18
[get(small_image, (i, j), Gray(0)) for (i,j) in Iterators.product(-1:7,-1:7)]
# ╔═╡ d06ea762-ee27-11ea-2e9c-1bcff86a3fe0
md"Extended with your `extend`:"
# ╔═╡ e1dc0622-ee16-11ea-274a-3b6ec9e15ab5
[extend_mat(small_image, i, j) for (i,j) in Iterators.product(-1:7,-1:7)]
# ╔═╡ 3cd535e4-ee26-11ea-2482-fb4ad43dda19
let
philip_head = philip[250:430,110:230]
[extend_mat(philip_head, i, j) for (i,j) in Iterators.product(-50:size(philip_head,1)+51, (-50:size(philip_head,2)+51))]
end
# ╔═╡ 7c41f0ca-ee15-11ea-05fb-d97a836659af
md"""
#### Exercise 4.2
👉 Implement a function `convolve_image(M, K)`.
"""
# ╔═╡ 8b96e0bc-ee15-11ea-11cd-cfecea7075a0
function convolve_image(M::AbstractMatrix, K::AbstractMatrix)
rows, cols = size(M)
row_offset, col_offset = (size(K) .- 1) .÷ 2
extended_M = [ extend_mat(M, i, j) for (i,j) ∈ Iterators.product(1-row_offset:rows+row_offset, 1-col_offset:cols+col_offset) ]
# broadcast * between submatrices and K and then sum for each entry of M
hₖ, lₖ = size(K)
return [ sum(extended_M[i:i+hₖ-1, j:j+lₖ-1] .* K) for i ∈ 1:size(extended_M, 1)-(hₖ-1), j ∈ 1:size(extended_M, 2)-(lₖ-1) ]
end
# ╔═╡ 5a5135c6-ee1e-11ea-05dc-eb0c683c2ce5
md"_Let's test it out! 🎃_"
# ╔═╡ 577c6daa-ee1e-11ea-1275-b7abc7a27d73
test_image_with_border = [get(small_image, (i, j), Gray(0)) for (i,j) in Iterators.product(-1:7,-1:7)]
# ╔═╡ 275a99c8-ee1e-11ea-0a76-93e3618c9588
K_test = [
0 0 0
1/2 0 1/2
0 0 0
]
# ╔═╡ 42dfa206-ee1e-11ea-1fcd-21671042064c
convolve_image(test_image_with_border, K_test)
# ╔═╡ 6e53c2e6-ee1e-11ea-21bd-c9c05381be07
md"_Edit_ `K_test` _to create your own test case!_"
# ╔═╡ e7f8b41a-ee25-11ea-287a-e75d33fbd98b
convolve_image(philip, K_test)
# ╔═╡ 8a335044-ee19-11ea-0255-b9391246d231
md"""
---
You can create all sorts of effects by choosing the kernel in a smart way. Today, we will implement two special kernels, to produce a **Gaussian blur** and a **Sobel edge detect** filter.
Make sure that you have watched [the lecture](https://www.youtube.com/watch?v=8rrHTtUzyZA) about convolutions!
"""
# ╔═╡ 7c50ea80-ee15-11ea-328f-6b4e4ff20b7e
md"""
#### Exercise 4.3
👉 Apply a **Gaussian blur** to an image.
Here, the 2D Gaussian kernel will be defined as
$$G(x,y)=\frac{1}{2\pi \sigma^2}e^{\frac{-(x^2+y^2)}{2\sigma^2}}$$
"""
# ╔═╡ aad67fd0-ee15-11ea-00d4-274ec3cda3a3
function with_gaussian_blur(image, l=100000)
G(x, y, σ=1) = 1 / (2π * σ) * exp(-(x^2 + y^2) / (2σ^2))
n = 2 * l + 1
kernel = [ G(i, j) for i ∈ 1:n, j ∈ 1:n ]
kernel ./= sum(kernel)
return convolve_image(image, kernel)
end
# ╔═╡ 8ae59674-ee18-11ea-3815-f50713d0fa08
md"_Let's make it interactive. 💫_"
# ╔═╡ 7c6642a6-ee15-11ea-0526-a1aac4286cdd
md"""
#### Exercise 4.4
👉 Create a **Sobel edge detection filter**.
Here, we will need to create two separate filters that separately detect edges in the horizontal and vertical directions:
```math
\begin{align}
G_x &= \left(\begin{bmatrix}
1 \\
2 \\
1 \\
\end{bmatrix} \otimes [1~0~-1]
\right) * A = \begin{bmatrix}
1 & 0 & -1 \\
2 & 0 & -2 \\
1 & 0 & -1 \\
\end{bmatrix}*A\\
G_y &= \left(
\begin{bmatrix}
1 \\
0 \\
-1 \\
\end{bmatrix} \otimes [1~2~1]
\right) * A = \begin{bmatrix}
1 & 2 & 1 \\
0 & 0 & 0 \\
-1 & -2 & -1 \\
\end{bmatrix}*A
\end{align}
```
Here $A$ is the array corresponding to your image.
We can think of these as derivatives in the $x$ and $y$ directions.
Then we combine them by finding the magnitude of the **gradient** (in the sense of multivariate calculus) by defining
$$G_\text{total} = \sqrt{G_x^2 + G_y^2}.$$
For simplicity you can choose one of the "channels" (colours) in the image to apply this to.
"""
# ╔═╡ def6071e-0487-11eb-1695-49d0ec2c5921
begin
Base.:^(b::ColorTypes.RGB{Float64}, p::Number) = RGB{Float64}(b.r^p, b.g^p, b.b^p)
end
# ╔═╡ 9eeb876c-ee15-11ea-1794-d3ea79f47b75
function with_sobel_edge_detect(image)
K_x = [1 0 -1; 2 0 -2; 1 0 -1]
K_y = [1 2 1; 0 0 0; -1 -2 -1]
G_x = convolve_image(image, K_x)
G_y = convolve_image(image, K_y)
square_root(pxl) = RGB(sqrt(pxl.r),sqrt(pxl.g),sqrt(pxl.b))
return (G_x.^2 + G_y.^2).^(1/2)
end
# ╔═╡ 1b85ee76-ee10-11ea-36d7-978340ef61e6
md"""
## **Exercise 5** - _Lecture transcript_
_(MIT students only)_
Please see the Canvas post for transcript document for week 1 [here](https://canvas.mit.edu/courses/5637/discussion_topics/27880).
We need each of you to correct about 100 lines (see instructions in the beginning of the document.)
👉 Please mention the name of the video and the line ranges you edited:
"""
# ╔═╡ 477d0a3c-ee10-11ea-11cf-07b0e0ce6818
lines_i_edited = md"""
Convolution, lines 100-0 (_for example_)
"""
# ╔═╡ 8ffe16ce-ee20-11ea-18bd-15640f94b839
if student.kerberos_id === "jazz"
md"""
!!! danger "Oops!"
**Before you submit**, remember to fill in your name and kerberos ID at the top of this notebook!
"""
end
# ╔═╡ 5516c800-edee-11ea-12cf-3f8c082ef0ef
hint(text) = Markdown.MD(Markdown.Admonition("hint", "Hint", [text]))
# ╔═╡ b1d5ca28-edf6-11ea-269e-75a9fb549f1d
hint(md"You can find out more about any function (like `rand`) by creating a new cell and typing:
```
?rand
```
Once the Live Docs are open, you can select any code to learn more about it. It might be useful to leave it open all the time, and get documentation while you type code.")
# ╔═╡ f6ef2c2e-ee07-11ea-13a8-2512e7d94426
hint(md"The `rand` function generates (uniform) random floating-point numbers between $0$ and $1$.")
# ╔═╡ ea435e58-ee11-11ea-3785-01af8dd72360
hint(md"Have a look at Exercise 2 to see an example of adding interactivity with a slider. You can read the [Interactivity](./sample/Interactivity.jl) and the [PlutoUI](./sample/PlutoUI.jl) sample notebooks _(right click -> Open in new tab)_ to learn more.")
# ╔═╡ e9aadeee-ee1d-11ea-3525-95f6ba5fda31
hint(md"`l = (length(k) - 1) ÷ 2`")
# ╔═╡ 649df270-ee24-11ea-397e-79c4355e38db
hint(md"`num_rows, num_columns = size(M)`")
# ╔═╡ 0cabed84-ee1e-11ea-11c1-7d8a4b4ad1af
hint(md"`num_rows, num_columns = size(K)`")
# ╔═╡ 57360a7a-edee-11ea-0c28-91463ece500d
almost(text) = Markdown.MD(Markdown.Admonition("warning", "Almost there!", [text]))
# ╔═╡ dcb8324c-edee-11ea-17ff-375ff5078f43
still_missing(text=md"Replace `missing` with your answer.") = Markdown.MD(Markdown.Admonition("warning", "Here we go!", [text]))
# ╔═╡ 58af703c-edee-11ea-2963-f52e78fc2412
keep_working(text=md"The answer is not quite right.") = Markdown.MD(Markdown.Admonition("danger", "Keep working on it!", [text]))
# ╔═╡ f3d00a9a-edf3-11ea-07b3-1db5c6d0b3cf
yays = [md"Great!", md"Yay ❤", md"Great! 🎉", md"Well done!", md"Keep it up!", md"Good job!", md"Awesome!", md"You got the right answer!", md"Let's move on to the next section."]
# ╔═╡ 5aa9dfb2-edee-11ea-3754-c368fb40637c
correct(text=rand(yays)) = Markdown.MD(Markdown.Admonition("correct", "Got it!", [text]))
# ╔═╡ 74d44e22-edee-11ea-09a0-69aa0aba3281
not_defined(variable_name) = Markdown.MD(Markdown.Admonition("danger", "Oopsie!", [md"Make sure that you define a variable called **$(Markdown.Code(string(variable_name)))**"]))
# ╔═╡ 397941fc-edee-11ea-33f2-5d46c759fbf7
if !@isdefined(random_vect)
not_defined(:random_vect)
elseif ismissing(random_vect)
still_missing()
elseif !(random_vect isa Vector)
keep_working(md"`random_vect` should be a `Vector`.")
elseif length(random_vect) != 10
keep_working(md"`random_vect` does not have the correct size.")
else
correct()
end
# ╔═╡ 38dc80a0-edef-11ea-10e9-615255a4588c
if !@isdefined(mean)
not_defined(:mean)
else
let
result = mean([1,2,3])
if ismissing(result)
still_missing()
elseif isnothing(result)
keep_working(md"Did you forget to write `return`?")
elseif result != 2
keep_working()
else
correct()
end
end
end
# ╔═╡ 2b1ccaca-edee-11ea-34b0-c51659f844d0
if !@isdefined(m)
not_defined(:m)
elseif ismissing(m)
still_missing()
elseif !(m isa Number)
keep_working(md"`m` should be a number.")
elseif m != mean(random_vect)
keep_working()
else
correct()
end
# ╔═╡ e3394c8a-edf0-11ea-1bb8-619f7abb6881
if !@isdefined(create_bar)
not_defined(:create_bar)
else
let
result = create_bar()
if ismissing(result)
still_missing()
elseif isnothing(result)
keep_working(md"Did you forget to write `return`?")
elseif !(result isa Vector) || length(result) != 100
keep_working(md"The result should be a `Vector` with 100 elements.")
elseif result[[1,50,100]] != [0,1,0]
keep_working()
else
correct()
end
end
end
# ╔═╡ adfbe9b2-ed6c-11ea-09ac-675262f420df
if !@isdefined(vecvec_to_matrix)
not_defined(:vecvec_to_matrix)
else
let
input = [[6,7],[8,9]]
result = vecvec_to_matrix(input)
shouldbe = [6 7; 8 9]
if ismissing(result)
still_missing()
elseif isnothing(result)
keep_working(md"Did you forget to write `return`?")
elseif !(result isa Matrix)
keep_working(md"The result should be a `Matrix`")
elseif result != shouldbe && result != shouldbe'
keep_working()
else
correct()
end
end