RoboCV Module 4: Image Processing Techniques using OpenCV

7,134 views

Published on

These are the slides of the RoboCV Workshop organized by roboVITics on August 11th-12th, 2012 in TT311 Smart Classroom, VIT University, Vellore.

The workshop was delivered by the following people:
1. Mayank Prasad, President of roboVITics
2. Akash Kashyap, President of TEC – The Electronics Club of VIT
3. Akshat Wahi, Asst. Project Manager of roboVITics

Published in: Technology, Art & Photos
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,134
On SlideShare
0
From Embeds
0
Number of Embeds
362
Actions
Shares
0
Downloads
3
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

RoboCV Module 4: Image Processing Techniques using OpenCV

  1. 1. By Mayank Prasad President, roboVITics VIT University, VelloreIMAGE PROCESSING TECHNIQUES USING OPENCV 4
  2. 2. OVERVIEW• Smoothing• Threshold• Image Morphology • Dilation and Erosion • Making your own Kernel • Opening and Closing • Morphological Gradient • Top Hat and Black Hat• Resize © roboVITics | Mayank Prasad, 2012 2
  3. 3. • These are a set of exhaustive slides in order to assist theCONTENT user in learning theINFORMATIONBy concepts easily. • These slides areMayank PrasadB.Tech ECE (IVth Year)VIT University incomplete without the following: • Learning OpenCV • Code Gallery © roboVITics | Mayank Prasad, 2012 3
  4. 4. SMOOTHINGBLURRING AN IMAGE © roboVITics | Mayank Prasad, 2012 4
  5. 5. SMOOTHING/BLURRING• Usually done to reduce noise and camera artifacts• Or used to reduce the resolution of an image• Method used is cvSmooth()• Virtual Pixels added at the borders © roboVITics | Mayank Prasad, 2012 5
  6. 6. TYPES OF SMOOTHING © roboVITics | Mayank Prasad, 2012 6
  7. 7. SYNTAX/CODE SNIPPETS• cvSmooth (img1, img1, CV_BLUR, 7, 7, 0, 0);• cvSmooth (img1, img2, CV_MEDIAN, 7, 7);• cvSmooth (img1, img1, CV_GAUSSIAN, 7, 7);• cvSmooth (img1, img2, CV_BILATERAL, 7, 7); © roboVITics | Mayank Prasad, 2012 7
  8. 8. © roboVITics | Mayank Prasad, 2012 8
  9. 9. SIMPLE BLUR• Each pixel in the output is the simple mean of all of the pixels in a window around the corresponding pixel in the input.• Simple blur supports 1–4 image channels• Works on 8-bit images or 32-bit floating-point images• Simple blur without scaling is also possible, but requires a different numerical position• Simple blur without scaling is faster than Simple blur• Noise is removed by exploiting the redundancies in the image © roboVITics | Mayank Prasad, 2012 9
  10. 10. AVERAGING MASK• © roboVITics | Mayank Prasad, 2012 10
  11. 11. © roboVITics | Mayank Prasad, 2012 11
  12. 12. MEDIAN BLUR• Replaces each pixel by the median or “middle” pixel (as opposed to the mean pixel) value in a square neighborhood around the center pixel• Works on single-channel or three-channel or four- channel 8-bit images• Cannot be done in place• Insensitive to “shot noise” i.e. images with large isolated outlier points since it takes the middle pixel• It is a closed operator © roboVITics | Mayank Prasad, 2012 12
  13. 13. MEDIAN BLUR 11 12 1311 14 15 14 11 14 1513 12 25 15 Median 13 15 2515 19 26 15 15 19 26 19 25 26 © roboVITics | Mayank Prasad, 2012 13
  14. 14. © roboVITics | Mayank Prasad, 2012 14
  15. 15. GAUSSIAN BLUR• Most useful, though not the fastest• Done by convolving each point in the input array with a Gaussian kernel and then summing to produce the output array © roboVITics | Mayank Prasad, 2012 15
  16. 16. GAUSSIAN BLUR ON 1D ARRAY © roboVITics | Mayank Prasad, 2012 16
  17. 17. GAUSSIAN MASK• 1 2 3 4 5 1 1 2 2 0.06 0.05 3 3 0.04 0.03 4 4 0.02 0.01 5 5  2  1 1 2 1 2 3 4 5 © roboVITics | Mayank Prasad, 2012 17
  18. 18. GAUSSIAN MASK • Value of central pixel should be maximum since it has maximum relevance for computation • Gaussian Matrix with r = 20, σ = 20 1 10 20 30 41 0.20 1 1 10 10 0.15 20 20 0.10 30 30 0.05 41 41 20  10 10 20 1 10 20 30 41 © roboVITics | Mayank Prasad, 2012 18
  19. 19. © roboVITics | Mayank Prasad, 2012 19
  20. 20. BILATERAL BLUR• Edge-preserving Smoothing• Can be contrasted with Gaussian Blur (next slide)• Think of bilateral filtering as Gaussian smoothing that weights more similar pixels more highly than less similar ones• Gives watercolor painting effect © roboVITics | Mayank Prasad, 2012 20
  21. 21. WHY GAUSSIAN FAILS?• Pixels in a real image should be correlated with its neighbors, whereas noise is not spatially correlated• Hence, it reduces noise while preserving signal• But this method breaks down at the edges, where you do expect the neighbor pixels to be uncorrelated, and thus it smoothens the edges• Can be prevented using Bilateral Smoothing at a cost of extra processing time © roboVITics | Mayank Prasad, 2012 21
  22. 22. Examplesof different types ofsmoothing © roboVITics | Mayank Prasad, 2012 22
  23. 23. SMOOTHING EXAMPLENoisy Input Average Gaussian Median © roboVITics | Mayank Prasad, 2012 23
  24. 24. SURFACE BLUR NOISE REDUCTION © roboVITics | Mayank Prasad, 2012 24
  25. 25. THRESHOLDCATEGORICAL REJECTION OF PIXELS © roboVITics | Mayank Prasad, 2012 25
  26. 26. THRESHOLD• Used to categorically reject some pixels above or below a particular threshold level• Handles only 8-bit or floating-point grayscale image• Method used is cvThreshold() © roboVITics | Mayank Prasad, 2012 26
  27. 27. THRESHOLD TYPES © roboVITics | Mayank Prasad, 2012 27
  28. 28. Types of Threshold © roboVITics | Mayank Prasad, 2012 28
  29. 29. Types of Threshold © roboVITics | Mayank Prasad, 2012 29
  30. 30. BinaryThreshold with differentthreshold values © roboVITics | Mayank Prasad, 2012 30
  31. 31. ADAPTIVE THRESHOLD• Threshold level itself varies• Used when thresholding strong illumination or reflectance gradients wrt general intensity gradient © roboVITics | Mayank Prasad, 2012 31
  32. 32. ADAPTIVE METHOD• Adaptive Threshold set on pixel by pixel basis• Weighted average computed of b-by-b block around the pixel minus a constant• Two methods: • CV_ADAPTIVE_THRESH_MEAN_C • CV_ADAPTIVE_THRESH_GAUSSIAN_C• b = block_size, const = param1, © roboVITics | Mayank Prasad, 2012 32
  33. 33. © roboVITics | Mayank Prasad, 2012 33
  34. 34. IMAGE MORPHOLOGY FILTERING BINARY IMAGES © roboVITics | Mayank Prasad, 2012 34
  35. 35. BASIC IMAGE MORPHOLOGY• Two basic morphological image transforms • Dilation, cvDilate() • Erosion, cvErode()• Applications of morphological operations • Removing noise • Isolating individual elements • Joining disparate elements • Detecting intensity bumps or holes • Finding intensity gradient © roboVITics | Mayank Prasad, 2012 35
  36. 36. DILATION & EROSIONDILATION, EROSION, EFFECTS, KERNEL © roboVITics | Mayank Prasad, 2012 36
  37. 37. DILATION• Convolution of an image A, with a kernel B• Kernel can be of any shape (solid square, disk, diamond, etc.) or size (3x3, 3x4, 5x5, r5, etc.)• Local maximum operation• Kernel B is scanned over Image A, and the anchor point is replaced by the maximal pixel value overlapped by B• Causes bright regions within an image to grow © roboVITics | Mayank Prasad, 2012 37
  38. 38. DILATION© roboVITics | Mayank Prasad, 2012 38
  39. 39. DILATION• Often done on binary images obtained from thresholding• But can be applied to intensity images also © roboVITics | Mayank Prasad, 2012 39
  40. 40. © roboVITics | Mayank Prasad, 2012 40
  41. 41. DILATION EXAMPLE © roboVITics | Mayank Prasad, 2012 41
  42. 42. EROSION• Converse of Dilation• Anchor point is replaced by the local minimal pixel value under the region of the kernel B © roboVITics | Mayank Prasad, 2012 42
  43. 43. EROSION© roboVITics | Mayank Prasad, 2012 43
  44. 44. © roboVITics | Mayank Prasad, 2012 44
  45. 45. Erosion ofan image with differentdisk radius © roboVITics | Mayank Prasad, 2012 45
  46. 46. MATHEMATICAL REPRESENTATION © roboVITics | Mayank Prasad, 2012 46
  47. 47. EFFECTS OF DILATION/EROSION• Erosion • Reduces region of image A • Tends to smooth away protrusions • Used to remove “speckle” noise • Speckles are eroded to nothing whereas larger visually significant content are left intact• Dilation • Expands region of image A • Tends to smoothen cavities • Often used to find connected components • A large region might be broken into several smaller components which “melt” into one another after dilation © roboVITics | Mayank Prasad, 2012 47
  48. 48. SAMPLE EXAMPLE © roboVITics | Mayank Prasad, 2012 48
  49. 49. MAKING YOUR OWN KERNEL © roboVITics | Mayank Prasad, 2012 49
  50. 50. KERNEL OPTIONS• Unlike Convolution Kernel, Morphological Kernel doesn’t require any numerical value since they simply indicate the position of max/min• Anchor point indicates how the kernel is aligned and also the destination point• Rows and Columns specify the size of rectangle which holds the kernel• Shape of the kernel is specified as per next slide• In case of custom kernel, the parameter values is used to determine the kernel © roboVITics | Mayank Prasad, 2012 50
  51. 51. KERNEL SHAPE• Possible IplConvKernel() Shape values: © roboVITics | Mayank Prasad, 2012 51
  52. 52. 9X9 DIAMOND KERNEL0 0 0 0 1 0 0 0 00 0 0 1 1 1 0 0 00 0 1 1 1 1 1 0 00 1 1 1 1 1 1 1 01 1 1 1 1 1 1 1 10 1 1 1 1 1 1 1 00 0 1 1 1 1 1 1 00 0 0 1 1 1 0 0 00 0 0 0 1 0 0 0 0 © roboVITics | Mayank Prasad, 2012 52
  53. 53. 9X9 DISC KERNEL0 0 0 0 1 0 0 0 00 0 1 1 1 1 1 0 00 1 1 1 1 1 1 1 01 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 10 1 1 1 1 1 1 1 00 0 1 1 1 1 1 0 00 0 0 0 1 0 0 0 0 © roboVITics | Mayank Prasad, 2012 53
  54. 54. 9X9 CROSS KERNEL0 0 0 0 1 0 0 0 00 0 0 0 1 0 0 0 00 0 0 0 1 0 0 0 00 0 0 0 1 0 0 0 01 1 1 1 1 1 1 1 10 0 0 0 1 0 0 0 00 0 0 0 1 0 0 0 00 0 0 0 1 0 0 0 00 0 0 0 1 0 0 0 0 © roboVITics | Mayank Prasad, 2012 54
  55. 55. 9X9 RECTANGLE KERNEL1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 © roboVITics | Mayank Prasad, 2012 55
  56. 56. MORE MORPHOLOGY!MORE GENERAL MORPHOLOGICAL OPERATIONS © roboVITics | Mayank Prasad, 2012 56
  57. 57. MORE GENERAL MORPHOLOGY• For grayscale/color images, several other morphological operations can be performed using the multi-purpose method cvMorphologyEx() © roboVITics | Mayank Prasad, 2012 57
  58. 58. MORE GENERAL MORPHOLOGY• Five different types of operations can be performed as follows • Opening and Closing • Top Hat and Black Hat • Morphological Gradient• May/may not require a temp image © roboVITics | Mayank Prasad, 2012 58
  59. 59. OPENING & CLOSINGOPENING, CLOSING, PROCESS, EXAMPLE © roboVITics | Mayank Prasad, 2012 59
  60. 60. OPENING & CLOSING• Combination of erosion and dilation• Opening • First erode, then dilate • Often used to count regions in binary image• Closing • First dilate, then erode • Used in most sophisticated connected-component algorithms to reduce unwanted/noise-driven segments © roboVITics | Mayank Prasad, 2012 60
  61. 61. Opening Closing© roboVITics | Mayank Prasad, 2012 61
  62. 62. © roboVITics | Mayank Prasad, 2012 62
  63. 63. © roboVITics | Mayank Prasad, 2012 63
  64. 64. OPENING & CLOSING• Both are approximately area-preserving• Closing eliminates lone outliers that are lower than their neighbors• Opening eliminates lone outliers that are higher than their neighbors• Number of iterations should be carefully chosen • Instead of dilate-erode-dilate-erode, it is dilate-dilate- erode-erode • In this way, not only the single outliers but also neighboring pairs of outliers will disappear © roboVITics | Mayank Prasad, 2012 64
  65. 65. OPENING & CLOSING Opening Closing © roboVITics | Mayank Prasad, 2012 65
  66. 66. GRADIENTMORPHOLOGICAL GRADIENT © roboVITics | Mayank Prasad, 2012 66
  67. 67. MORPHOLOGICAL GRADIENT• Isolates perimeter of existing blobs• Indicates how fast the image brightness changes• Used to isolate the perimeters of bright regions so we can treat them as whole objects © roboVITics | Mayank Prasad, 2012 67
  68. 68. Operation of Morphological Gradient © roboVITics | Mayank Prasad, 2012 68
  69. 69. © roboVITics | Mayank Prasad, 2012 69
  70. 70. TOP HAT & BLACK HAT ISOLATE INTENSITY PATCHES © roboVITics | Mayank Prasad, 2012 70
  71. 71. TOP HAT & BLACK HAT• Used to isolate patches that are, respectively, brighter or dimmer than their immediate neighbors• Top Hat reveals areas that are lighter than the surrounding region of A, relative to the size of the kernel• Black Hat is the converse of above © roboVITics | Mayank Prasad, 2012 71
  72. 72. © roboVITics | Mayank Prasad, 2012 72
  73. 73. © roboVITics | Mayank Prasad, 2012 73
  74. 74. SUMMARY – MORPHOLOGY © roboVITics | Mayank Prasad, 2012 74
  75. 75. GENERAL METHODOLOGY Threshold• BGR Format • Morphology• HSV Format • In Range Operations• Grayscale • Segmenting • Open/Close • Binary Image Raw Image Filtering © roboVITics | Mayank Prasad, 2012 75
  76. 76. MORPHOLOGY APPLICATIONRaw Opening Closing © roboVITics | Mayank Prasad, 2012 76
  77. 77. EVEN MORE MORPHOLOGY! Bottom Hat Bridge Clean Close Diagonal Fill Erode Fill H Break Hit-Miss Majority Open Remove Shrink Skeleton Spur Thicken Thin Top Hat © roboVITics | Mayank Prasad, 2012 77
  78. 78. HIT-MISS TRANSFORM © roboVITics | Mayank Prasad, 2012 78
  79. 79. IMAGE THINNING © roboVITics | Mayank Prasad, 2012 79
  80. 80. IMAGE SKELETON © roboVITics | Mayank Prasad, 2012 80
  81. 81. RESIZEZOOMING AN IMAGE © roboVITics | Mayank Prasad, 2012 81
  82. 82. IMAGE RESIZE• Upsize (Zoom In) & Downsize (Zoom Out) possible• cvResize() fits the source image into the destination image• ROI of source can be fit into ROI of destination © roboVITics | Mayank Prasad, 2012 82
  83. 83. INTERPOLATION• Handles the smoothness of the destination image © roboVITics | Mayank Prasad, 2012 83
  84. 84. WHY INTERPOLATION?! 11 rows 6464 10 rows 6868 7 rows 6468 68 6767 Zoom Out Zoom In 6770 How to 66 How to 6666 remove include 65 65 selected selected65 pixels? 63 pixels? 6363 6664 6666 © roboVITics | Mayank Prasad, 2012 84
  85. 85. INTERPOLATION TYPES• © roboVITics | Mayank Prasad, 2012 85
  86. 86. Resize using NearestNeighbor & Bilinear Interpol © roboVITics | Mayank Prasad, 2012 86
  87. 87. ACKNOWLEDGEMENTS References Image Courtesy• Gary Bradski and Adrian • Gary Bradski and Adrian Kaehler, Learning Kaehler, Learning OpenCV, O’Reilly Media, OpenCV, O’Reilly Inc. Media, Inc. • Web Sources earthseaimagery.wordpress.com• AI Shack, www.aishack.in sitepointstatic.com• Rafael C. Gonzalez and • Wikipedia Richard E. Woods, Digital • Rafael C. Gonzalez and Image Processing Richard E. Woods, Digital 2ed, Prentice Hall, Inc. Image Processing 2ed, Prentice Hall, Inc. © roboVITics | Mayank Prasad, 2012 87
  88. 88. UP NEXT CONTOURS 5
  89. 89. CONTACTS• Mayank Prasad President, roboVITics mayank@robovitics.in• Akshat Wahi Asst. Project Manager, roboVITics +91 909 250 3053 akshat@core.robovitics.in• Akash Kashyap President, TEC – The Electronics Club of VIT akash130791@gmail.com © roboVITics | Mayank Prasad, 2012 89

×