SUNY Geneseo Department of Mathematics
Wednesday, April 19
Math 230 02
Spring 2017
Prof. Doug Baldwin
Probably due to an upgrade to Chrome.
Work around by disabling upgraded blocker, or sorting notes into most-recent-first order, or using a different browser.
See Canvas announcement for more
A day of talks, posters, performances, etc, highlighting students’ scholarly and creative work. No classes, so you can go to the sessions. See https://www.geneseo.edu/great_day for more information.
Next Tuesday (April 25).
Extra credit for writing a summary of any one presentation and connections you make to it.
Suppose you wanted to select just those elements of a matrix that were greater than 100, for instance in this 10-by-10 array of random integers between 1 and 255:
>> M = randi( 255, 10, 10 )
M =
208 41 168 181 112 71 192 215 90 20
231 248 10 9 98 174 66 65 212 14
33 245 217 71 196 168 130 208 150 136
233 124 239 12 203 42 179 63 141 199
162 205 174 25 48 31 228 237 234 239
25 37 194 210 125 128 245 90 73 34
72 108 190 178 114 245 140 51 194 146
140 234 101 81 165 87 36 65 193 120
245 203 168 243 181 150 39 158 98 4
247 245 44 9 193 58 66 121 145 86
Relevant ideas or questions from Monday’s reading:
Solution: Use the “> 100” idea to make a logical indexing array, then use
it to index M
.
>> M > 100
ans =
1 0 1 1 1 0 1 1 0 0
1 1 0 0 0 1 0 0 1 0
0 1 1 0 1 1 1 1 1 1
1 1 1 0 1 0 1 0 1 1
1 1 1 0 0 0 1 1 1 1
0 0 1 1 1 1 1 0 0 0
0 1 1 1 1 1 1 0 1 1
1 1 1 0 1 0 0 0 1 1
1 1 1 1 1 1 0 1 0 0
1 1 0 0 1 0 0 1 1 0
>> n = M > 100
n =
1 0 1 1 1 0 1 1 0 0
1 1 0 0 0 1 0 0 1 0
0 1 1 0 1 1 1 1 1 1
1 1 1 0 1 0 1 0 1 1
1 1 1 0 0 0 1 1 1 1
0 0 1 1 1 1 1 0 0 0
0 1 1 1 1 1 1 0 1 1
1 1 1 0 1 0 0 0 1 1
1 1 1 1 1 1 0 1 0 0
1 1 0 0 1 0 0 1 1 0
>> M( n )
ans =
208
231
233
162
140
245
247
248
245
124
205
108
234
203
245
168
217
239
174
194
190
101
168
181
210
178
243
112
196
203
125
114
165
181
193
174
168
128
245
150
192
130
179
228
245
140
215
208
237
158
121
212
150
141
234
194
193
145
136
199
239
146
120
Notice that when you actually use the logical indexing array to extract elements from a matrix, you get a column vector. This fact often stays hidden behind the scenes within Matlab though.
Now suppose you wanted to replace the elements greater than 100 in matrix M
with the corresponding elements of matrix New
.
>> M
M =
208 41 168 181 112 71 192 215 90 20
231 248 10 9 98 174 66 65 212 14
33 245 217 71 196 168 130 208 150 136
233 124 239 12 203 42 179 63 141 199
162 205 174 25 48 31 228 237 234 239
25 37 194 210 125 128 245 90 73 34
72 108 190 178 114 245 140 51 194 146
140 234 101 81 165 87 36 65 193 120
245 203 168 243 181 150 39 158 98 4
247 245 44 9 193 58 66 121 145 86
>> New = - randi( 255, 10, 10 )
New =
-42 -115 -28 -111 -218 -107 -199 -60 -140 -237
-203 -22 -246 -233 -159 -13 -100 -91 -76 -198
-80 -59 -2 -47 -90 -231 -62 -210 -190 -125
-135 -233 -198 -68 -131 -241 -103 -4 -49 -112
-43 -39 -209 -38 -103 -126 -25 -11 -176 -114
-154 -211 -222 -35 -20 -125 -34 -44 -47 -79
-68 -138 -22 -222 -62 -87 -241 -166 -94 -130
-167 -255 -102 -148 -32 -230 -244 -187 -160 -131
-176 -20 -67 -141 -47 -95 -147 -166 -199 -209
-191 -113 -205 -37 -62 -29 -16 -115 -21 -203
Once again, use the logical indexing array, but this time not only to pick elements out
of New
, but also to pick positions out of M
to replace.
>> x = New(n)
x =
-42
-203
-135
-43
-167
-176
-191
-22
-59
-233
-39
-138
-255
-20
-113
-28
-2
-198
-209
-222
-22
-102
-67
-111
-35
-222
-141
-218
-90
-131
-20
-62
-32
-47
-62
-13
-231
-125
-87
-95
-199
-62
-103
-25
-34
-241
-60
-210
-11
-166
-115
-76
-190
-49
-176
-94
-160
-21
-125
-112
-114
-130
-131
>> M(n) = New(n)
M =
-42 41 -28 -111 -218 71 -199 -60 90 20
-203 -22 10 9 98 -13 66 65 -76 14
33 -59 -2 71 -90 -231 -62 -210 -190 -125
-135 -233 -198 12 -131 42 -103 63 -49 -112
-43 -39 -209 25 48 31 -25 -11 -176 -114
25 37 -222 -35 -20 -125 -34 90 73 34
72 -138 -22 -222 -62 -87 -241 51 -94 -130
-167 -255 -102 81 -32 87 36 65 -160 -131
-176 -20 -67 -141 -47 -95 39 -166 98 4
-191 -113 44 9 -62 58 66 -115 -21 86
Adapt these ideas to replacing pixels of a subject image in which each of the 3 colors are between lower and upper bounds with the corresponding pixels of a background image.
Some ideas in pseudocode:
function chroma( subject, background, minRed, maxRed, minGreen, maxGreen, minBlue, maxBlue ) subjectGreen = subject( :, :, 2 ) ... similarly extract the subject’s red and blue planes ... mask = subjectGreen < maxGreen & subjectGreen > minGreen & subjectBlue < maxBlue & subjectBlue > minBlue & subjectRed < maxRed & subjectRed > minRed ... subject( mask ) = background( mask ) ... but note that this idea is now implemented as working with each of the 3 color planes separately and then combining them together into an image. return subject
Lab day for chroma key lab.