|
1 | 1 | %[text] %[text:anchor:F4CCA683] # Robotics, Vision & Control 3e: for MATLAB |
2 | 2 | %[text] %[text:anchor:CAAB408D] # Chapter 11: Images and Image Processing |
3 | | -%[text:tableOfContents]{"heading":"**Table of Contents**"} |
| 3 | +%[text:tableOfContents]{"heading":"Table of Contents"} |
4 | 4 | %[text] %[text:anchor:DD428C66] # |
5 | | -%[text] Copyright 2022\-2023 Peter Corke, Witold Jachimczyk, Remo Pillat |
6 | | -%[text] %[text:anchor:C44A23DD] ## 11\.1 Obtaining an Image |
7 | | -%[text] %[text:anchor:5AD1F85D] ### 11\.1\.1 Images from Files |
| 5 | +%[text] Copyright 2022-2023 Peter Corke, Witold Jachimczyk, Remo Pillat |
| 6 | +%[text] %[text:anchor:C44A23DD] ## 11.1 Obtaining an Image |
| 7 | +%[text] %[text:anchor:5AD1F85D] ### 11.1.1 Images from Files |
8 | 8 | street = imread("street.png"); |
9 | 9 |
|
10 | 10 | whos street |
|
39 | 39 | md = imfinfo("roof.jpg") |
40 | 40 |
|
41 | 41 | md.DigitalCamera |
42 | | -%[text] %[text:anchor:8EAC253F] ### 11\.1\.2 Images from an Attached Camera |
| 42 | +%[text] %[text:anchor:8EAC253F] ### 11.1.2 Images from an Attached Camera |
43 | 43 | % Not runnable section. |
44 | 44 | % The following code only works if you have a webcam attached to your |
45 | 45 | % computer. Uncomment if you want to run it. |
|
53 | 53 | %im = cam.snapshot; |
54 | 54 |
|
55 | 55 | %cam.preview |
56 | | -%[text] %[text:anchor:13D2568B] ### 11\.1\.3 Images from a Video File |
| 56 | +%[text] %[text:anchor:13D2568B] ### 11.1.3 Images from a Video File |
57 | 57 | vid = VideoReader("traffic_sequence.mpg"); |
58 | 58 |
|
59 | 59 | vid.NumFrames |
|
71 | 71 | cont = vid.hasFrame && videoPlayer.isOpen; |
72 | 72 | pause(0.05); |
73 | 73 | end |
74 | | -%[text] %[text:anchor:54CABEB2] ### 11\.1\.4 Images from the Web |
| 74 | +%[text] %[text:anchor:54CABEB2] ### 11.1.4 Images from the Web |
75 | 75 | img = imread("http://uk.jokkmokk.jp/photo/nr4/latest.jpg"); |
76 | 76 |
|
77 | 77 | size(img) |
78 | 78 |
|
79 | 79 | imshow(img) |
80 | | -%[text] %[text:anchor:5B5701F4] ### 11\.1\.5 Images from Code |
| 80 | +%[text] %[text:anchor:5B5701F4] ### 11.1.5 Images from Code |
81 | 81 | im = testpattern("rampx",256,2); |
82 | 82 | im = testpattern("siny",256,2); |
83 | 83 | im = testpattern("squares",256,50,25); |
|
98 | 98 |
|
99 | 99 | imshow(canvas) |
100 | 100 | %% |
101 | | -%[text] %[text:anchor:61BDB3A2] ## 11\.2 Image Histograms |
| 101 | +%[text] %[text:anchor:61BDB3A2] ## 11.2 Image Histograms |
102 | 102 | church = rgb2gray(imread("church.png")); |
103 | 103 | imhist(church) |
104 | 104 |
|
|
110 | 110 | [~,p] = findpeaks(counts,x,MinPeakDistance=60); |
111 | 111 | p' % transpose for display |
112 | 112 | %% |
113 | | -%[text] %[text:anchor:9736A4AD] ## 11\.3 Monadic Operations |
| 113 | +%[text] %[text:anchor:9736A4AD] ## 11.3 Monadic Operations |
114 | 114 | imd = im2double(church); |
115 | 115 |
|
116 | 116 | im = im2uint8(imd); |
|
138 | 138 |
|
139 | 139 | imshow(church/64,[]) |
140 | 140 | %% |
141 | | -%[text] %[text:anchor:38013E66] ## 11\.4 Dyadic Operations |
| 141 | +%[text] %[text:anchor:38013E66] ## 11.4 Dyadic Operations |
142 | 142 | subject = im2double(imread("greenscreen.jpg")); |
143 | 143 | imshow(subject) |
144 | 144 |
|
|
159 | 159 | imshow(bg.*(1-mask)) |
160 | 160 |
|
161 | 161 | imshow(subject.*mask + bg.*(1-mask)) |
162 | | -%[text] %[text:anchor:EDFF06F1] ### 11\.4\.1 Application: Motion detection |
| 162 | +%[text] %[text:anchor:EDFF06F1] ### 11.4.1 Application: Motion detection |
163 | 163 | vid = VideoReader("traffic_sequence.mpg"); |
164 | 164 |
|
165 | 165 | bg = im2double(im2gray(vid.readFrame())); |
|
173 | 173 | imshow(bg); |
174 | 174 | end |
175 | 175 | %% |
176 | | -%[text] %[text:anchor:AE329B49] ## 11\.5 Spatial Operations |
177 | | -%[text] %[text:anchor:1A46459C] ### 11\.5\.1 Linear Spatial Filtering |
| 176 | +%[text] %[text:anchor:AE329B49] ## 11.5 Spatial Operations |
| 177 | +%[text] %[text:anchor:1A46459C] ### 11.5.1 Linear Spatial Filtering |
178 | 178 | %Y = imfilter(X,K,"conv"); |
179 | 179 |
|
180 | | -%[text] %[text:anchor:348E58ED] #### 11\.5\.1\.1 Image Smoothing |
| 180 | +%[text] %[text:anchor:348E58ED] #### 11.5.1.1 Image Smoothing |
181 | 181 |
|
182 | 182 | K = ones(21,21) / 21^2; |
183 | 183 |
|
|
196 | 196 | K = fspecial("disk",8); |
197 | 197 |
|
198 | 198 | %[text] |
199 | | -%[text] %[text:anchor:855F2550] #### 11\.5\.1\.3 Edge Detection |
| 199 | +%[text] %[text:anchor:855F2550] #### 11.5.1.3 Edge Detection |
200 | 200 |
|
201 | 201 | castle = im2double(imread("castle.png")); |
202 | 202 |
|
|
241 | 241 | plot(570:600,p,"-o"); |
242 | 242 |
|
243 | 243 | bw = edge(castle,"log"); |
244 | | -%[text] %[text:anchor:3FAEB9E9] ### 11\.5\.2 Template Matching |
| 244 | +%[text] %[text:anchor:3FAEB9E9] ### 11.5.2 Template Matching |
245 | 245 | mona = im2double(rgb2gray(imread("monalisa.png"))); |
246 | 246 | A = mona(170:220,245:295); |
247 | 247 |
|
|
285 | 285 |
|
286 | 286 | imshow(S,[]), colorbar |
287 | 287 |
|
288 | | -peakFinder = vision.LocalMaximaFinder(MaximumNumLocalMaxima=5, ... |
289 | | - NeighborhoodSize=[3 3],Threshold=min(S(:))); |
290 | | -uvLoc = peakFinder.step(S); |
291 | | -vals = S(sub2ind(size(S),uvLoc(:,2),uvLoc(:,1)))' |
292 | 288 |
|
293 | | -labels = "Wally " + string(1:5) + ": " + string(vals); |
294 | | -markedSim = insertObjectAnnotation(S,"circle",[uvLoc, ... |
295 | | - 10*ones(5,1)],labels,FontSize=21); |
296 | | -imshow(markedSim) |
297 | | -%[text] %[text:anchor:5981651E] ### 11\.5\.3 Nonlinear Operations |
| 289 | +%% vision.LocalMaximaFinder has been deprecated. See errata: https://github.com/petercorke/RVC3-MATLAB/wiki/Errata |
| 290 | + |
| 291 | +% peakFinder = vision.LocalMaximaFinder(MaximumNumLocalMaxima=5, ... |
| 292 | +% NeighborhoodSize=[3 3],Threshold=min(S(:))); |
| 293 | +% uvLoc = peakFinder.step(S); |
| 294 | +% vals = S(sub2ind(size(S),uvLoc(:,2),uvLoc(:,1)))' |
| 295 | + |
| 296 | +% labels = "Wally " + string(1:5) + ": " + string(vals); |
| 297 | +% markedSim = insertObjectAnnotation(S,"circle",[uvLoc, ... |
| 298 | +% 10*ones(5,1)],labels,FontSize=21); |
| 299 | +% imshow(markedSim) |
| 300 | +%[text] %[text:anchor:5981651E] ### 11.5.3 Nonlinear Operations |
298 | 301 | out = nlfilter(mona,[7 7],@(x)var(x(:))); |
299 | 302 |
|
300 | 303 | mx = ordfilt2(mona,1,ones(5,5)); |
|
312 | 315 |
|
313 | 316 | imshow(mona > mxn) |
314 | 317 | %% |
315 | | -%[text] %[text:anchor:8A53ACA5] ## 11\.6 Mathematical Morphology |
| 318 | +%[text] %[text:anchor:8A53ACA5] ## 11.6 Mathematical Morphology |
316 | 319 | load eg_morph1.mat |
317 | 320 | imshow(im,InitialMagnification=800) |
318 | 321 |
|
|
323 | 326 | morphdemo(im,S,"min") |
324 | 327 |
|
325 | 328 | mx = imdilate(mn,S); |
326 | | -%[text] %[text:anchor:9676C371] ### 11\.6\.1 Noise Removal |
| 329 | +%[text] %[text:anchor:9676C371] ### 11.6.1 Noise Removal |
327 | 330 | objects = imread("segmentation.png"); |
328 | 331 |
|
329 | 332 | S = strel("disk",4); |
|
335 | 338 |
|
336 | 339 | opened = imopen(objects,S); |
337 | 340 | closed = imclose(opened,S); |
338 | | -%[text] %[text:anchor:27B57E39] ### 11\.6\.2 Boundary Detection |
| 341 | +%[text] %[text:anchor:27B57E39] ### 11.6.2 Boundary Detection |
339 | 342 | eroded = imerode(clean,strel("disk",1)); |
340 | 343 |
|
341 | 344 | imshow(clean-eroded) |
342 | | -%[text] %[text:anchor:16339476] ### 11\.6\.3 Hit or Miss Transform |
| 345 | +%[text] %[text:anchor:16339476] ### 11.6.3 Hit or Miss Transform |
343 | 346 | skeleton = bwmorph(clean,"skel",Inf); |
344 | 347 |
|
345 | 348 | ends = bwmorph(skeleton,"endpoints"); |
346 | 349 |
|
347 | 350 | joints = bwmorph(skeleton,"branchpoints"); |
348 | | -%[text] %[text:anchor:3CB666F7] ### 11\.6\.4 Distance Transform |
| 351 | +%[text] %[text:anchor:3CB666F7] ### 11.6.4 Distance Transform |
349 | 352 | im = zeros(256); |
350 | 353 | im = insertShape(im,"FilledRectangle",[64 64 128 128], ... |
351 | 354 | Color="w",Opacity=1); |
|
354 | 357 |
|
355 | 358 | dx = bwdist(edges); |
356 | 359 | %% |
357 | | -%[text] %[text:anchor:8649171A] ## 11\.7 Shape Changing |
358 | | -%[text] %[text:anchor:F52A212B] ### 11\.7\.1 Cropping |
| 360 | +%[text] %[text:anchor:8649171A] ## 11.7 Shape Changing |
| 361 | +%[text] %[text:anchor:F52A212B] ### 11.7.1 Cropping |
359 | 362 | mona = imread("monalisa.png"); |
360 | 363 |
|
361 | 364 | % The following 3 lines are interactive. |
|
364 | 367 | %roi |
365 | 368 |
|
366 | 369 | smile = imcrop(mona,[265 264 77 22]); |
367 | | -%[text] %[text:anchor:564539E5] ### 11\.7\.2 Image Resizing |
| 370 | +%[text] %[text:anchor:564539E5] ### 11.7.2 Image Resizing |
368 | 371 | roof = rgb2gray(imread("roof.jpg")); |
369 | 372 | whos roof |
370 | 373 |
|
|
373 | 376 | smaller = imresize(roof,1/7); |
374 | 377 |
|
375 | 378 | bigger = imresize(smaller,7); |
376 | | -%[text] %[text:anchor:57A9E901] ### 11\.7\.3 Image Pyramids |
| 379 | +%[text] %[text:anchor:57A9E901] ### 11.7.3 Image Pyramids |
377 | 380 | p0 = impyramid(rgb2gray(mona),"reduce"); |
378 | 381 | p1 = impyramid(p0,"reduce"); |
379 | 382 | p2 = impyramid(p1,"reduce"); |
380 | 383 | p3 = impyramid(p2,"reduce"); |
381 | | -%[text] %[text:anchor:46D85206] ### 11\.7\.4 Image Warping |
| 384 | +%[text] %[text:anchor:46D85206] ### 11.7.4 Image Warping |
382 | 385 | mona = im2double(rgb2gray(imread("monalisa.png"))); |
383 | 386 | [Ui,Vi] = meshgrid(1:size(mona,2),1:size(mona,1)); |
384 | 387 |
|
|
438 | 441 | %#ok<*NBRAK2> |
439 | 442 |
|
440 | 443 |
|
441 | | -%[appendix] |
| 444 | +%[appendix]{"version":"1.0"} |
442 | 445 | %--- |
443 | 446 | %[metadata:view] |
444 | 447 | % data: {"layout":"inline","rightPanelPercent":40} |
|
0 commit comments