KEMBAR78
02 image processing | PPTX
2
This presentation is for informational purposes only. INTEL MAKES NO WARRANTIES, EXPRESS OR
IMPLIED, IN THIS SUMMARY.
Intel technologies’ features and benefits depend on system configuration and may require enabled
hardware, software or service activation. Performance varies depending on system configuration. Check
with your system manufacturer or retailer or learn more at intel.com.
This sample source code is released under the Intel Sample Source Code License Agreement.
Intel and the Intel logo are trademarks of Intel Corporation in the U.S. and/or other countries.
*Other names and brands may be claimed as the property of others.
Copyright © 2018, Intel Corporation. All rights reserved.
Legal Notices and Disclaimers
Processing Type
Applications of Image Processing
Example Applications
Enhancement
Example: Changing
saturation, contrast
to enhance look
• Marketing
• Photography
Understanding
Example: Object
detection, image
classification to
understand image
content
• Self-driving cars
• Security
Perturbation
Example: Blurring,
rotating images to
produce desired effect
• Censoring
• Animations
Image Processing Tools
Jupyter
Notebook*†
Matlab*
C++
IDEs
(Integrated Development
Environments)
Languages
Python*†
Libraries
NumPy† OpenCV†
Focus of this lesson
† = Taught in this course
If you do arithmetic with NumPy, overflow can occur:
Here, uint8 stores a max of 256: 300 is rounded down to 300 - 256 = 44.
To avoid this, we have to promote to a bigger type than our expected result, do arithmetic, then convert
back to smaller type:
Here, we promote uint8 to uint16, do arithmetic, convert back into uint8.
Computer Arithmetic with NumPy
Or
• Can use openCV arithmetic, which handles overflow (numbers being added to
quantities greater than 255), and clipping (automatically reducing numbers greater
than 255 down to 255) for us.
• With OpenCV, adding 100+200 with a max of 255 gets rounded down to 255, as
expected.
Computer Arithmetic with OpenCV
Resizing an image requires reassigning pixels from the original image to the new
image space.
Matplotlib* and OpenCV have the same interpolation methods.
The difference: Matplotlib applies interpolation at display time. OpenCV applies
interpolation prior to displaying the images.
Interpolation is used to address forward projection problems. These problems are
defined as:
1. Stretching the size of an image creates locations in the new image where pixels do
not have values.
2. Shrinking the size of an image creates float pixel values that need to be assigned to
an integer pixel value in the new image.
Interpolation
Interpolation Methods: Images
Nearest Neighbor (cv2.INTER.NN)
Linear (cv2.INTER_LINEAR)
Interpolation Methods: Data
spline16 quadric laczos hermite kaiser
mitchell sinc hamming bessel hanning
guassian spline36 catron area
You can play with these, but we will not get into them.
Other Interpolation in OpenCV
Imshow arguments
o (m,n):
Rows, columns: NOT x,y coordinates
Can (m,n,3) and (m,n,4)
Values uint8 or float in [0,1]
(m,n,3) = RGB
(m,n,4) = RGBA
A  alpha
o “cmap”
Color map: can be RGB, grayscale, and so on
See upcoming slide for examples
o “vmin”, “vmax”
Used to change brightness
Display with matplotlib*: imshow
Display of 2D Arrays with matplotlib*: matshow
vmin (minimum brightness) defaults to min data
vmax (maximum brightness) defaults to max of data
Scalar, optional, default: None
Used to normalize
Tip: To prevent scaling (0,255) for a restricted range (say 100,200), pass in vmin=0,
vmax=255.
Display with matplotlib*: vmin and vmax
Interpolation = 'none' and interpolation = 'nearest'
• Are equivalent when converting a figure to an image file (that is, PNG)
Interpolation = 'none' and interpolation = 'nearest'
• Behave quite differently when converting a figure to a vector graphics file (that is,
PDF)
In our examples, interpolation = 'none' works well when a big image is scaled down.
While interpolation = 'nearest' works well when a small image is scaled up.
Display with matplotlib*: Interpolation
Display with matplotlib*: cmap
Color Spaces
Color Spaces: Introduction
All images are represented by pixels with values in color spaces. Must represent an image
in a color space before applying any processing techniques.
Examples of color spaces:
Grayscale (black and white)
Color
RGB (red, green, blue)
HSV (hue, saturation, value)
CMYK (cyan, magenta, yellow, black)
Color Spaces: Grayscale
Grayscale to RGB conversion assumes all components of images are equal.
RGB to grayscale conversion uses
Y = (0.299)R + (0.587)G + (0.114)B
Color Spaces: RGB (Red, Green, Blue)
RGB (red, green, blue)
8-bit images 0-255
16-bit images 0-65,536
Floating point 0.0-1.0
Color Spaces: HSV (Hue, Saturation, Value)
Hue is normally 0-360
• Hue is walking around a color wheel
• OpenCV uses 0-179 to fit in 8 bit
Saturation (s) and value (v) are ranges
of light to dark (intensity)
• Trade off black/white
• OpenCV uses 0-255 for both s and v
In cone models
• s is white; distance from cone axis
• v is black; distance from cone point
Color Conversion in OpenCV
RGB is 8- or 16-bit colors.
Each pixel is represented by three values (one for each channel).
These values are pixel intensities
000 = black
111 = white
Color Conversion in OpenCV
When converted to grayscale, all RGB are equal.
When converted from RGB (or BGR) to grayscale:
𝑌 = 0.299 𝑅 + 0.587 𝐺 + 0.114 𝐵
Color in matplotlib*
Histograms
Histograms are a way of showing the intensity of color across an image.
Abnormal histograms can be used to detect poor image exposure, saturation,
and contrast.
Example:
The image on the left has poor exposure, which can be detected via the
histogram.
Histograms: Introduction and Grayscale
Example
Histogram Examples: RGB
2D Histograms
Histograms: Bin Widths
Histogram Equalization
Histogram equalization: Technique to adjust contrast to evenly spread
y-values from an original distribution to a new distribution.
Usually increases global contrast of an image.
Histogram Equalization: Probability Integral
Transform
Recall our original distribution:
• Bimodal peaks
• Want to flatten this effect to get a uniform
distribution
Probability integral transform allows us to convert
an arbitrary distribution to a uniform distribution
Histogram Equalization: Probability Integral
Transform
Covert an event from an arbitrary to a uniform distribution.
OriginalConverted
Histogram Equalization: Probability Integral
TransformOur original is a distribution of pixel intensities.
We will use percentiles from both the arbitrary distribution and the uniform distribution
to do this conversion.
We need to know, for our input intensity x, what percentile it falls in to.
We compute the cumulative (running total) of the probabilities of the values less than x.
Once we know that, we have an output value between [0,1] or U(0,1)
We can map U(0,1) to U(0,255) to get uniformly scaled pixel intensities.
Simple linear scaling for (0,1) to (0, 255)
Thresholding
Foreground/Background
Only part of the image has important data; we call these regions of interest (ROI).
In this case, we are interested in the foreground, but NOT the background of the logo
image.
foreground
background
foreground
In the case when the background has distinctively different color than the foreground,
we can use thresholding to separate them.
Example application:
Thresholding Application
Image source: https://upload.wikimedia.org/wikipedia/commons/2/23/Gfp-texas-big-bend-national-park-plants-on-the-desert-horizon.jpg
OpenCV Logo Example
Thresholding: Foreground Versus Background
We need to build and interpret an image as foreground and background.
We need to make a final decision about points.
We will create a mask to denote regions of interest (foreground versus background).
Thresholding: Simple Methods
cv2.threshold( ) methods:
These methods are global / independent of neighbors.
Thresholding: Simple Methods
Thresholding: Neighborhood Methods
cvAdaptiveThreshold( ) is used when there is uneven illumination.
To calculate threshold for neighborhood:
Calculate neighborhood means, using equal weights or weighted-averages based on
Gaussian windows/filters.
Subtract neighborhood mean from each point in the neighborhood.
Code to plot neighborhood thresholding:
Thresholding: Neighborhood Methods
Neighborhood methods illustration:
Thresholding: Neighborhood Methods
Thresholding: Otsu’s Method
In Otsu's method, we don't know the classes:
• We try every break point (for example, say 127, or an empirical mean or median)
and assume it is the separator.
• The data is in two classes and we can compute the ratio of the within class variance
and the between class variance.
• Only one break point maximizes the ratio of within class variance to between class
variance.
This is our desired threshold.
Generally, Otsu works well with a large region of interest (compared to background) and
a strong bimodal distribution.
Thresholding: Otsu’s Method
Optimal threshold unclear
(OpenCV finds 116)
Optimal threshold = 126
Geometric Transforms
Geometric Transforms: Resizing
x,y  fx(x,y), fy(x,y)
Decreasing resolution (throwing out data) requires
decimation
Increasing resolution (creating data from nothing)
requires interpolation
Matrix Glossary
T Translation
R Rotation
A Affine (arbitrary)
S Scaling
H Homography (prospective, projective)
Translation
What you would see in trig class: OpenCV affine matrix:
Translation
The OpenCV affine matrix is applied to an augmented p
• p with a constant 1 tacked on to it.
Translation - Code
Rigid (Rotation and Translation)
Again, in OpenCV, this is applied to an augmented point:
Rigid (Rotation and Translation)
Scale (Similarity Transform)
What you would see in trig class: OpenCV affine matrix:
Scale (Similarity Transform)
Scale
When combining scaling with other transformation:
When xscale= yscale = 1
We get a rigid transformation
– Also called isometry or a Euclidean transformation
– Preserves scale (distance)
When xscale= yscale
We have a similarity transformation
– Also called a scaled-rotation
Perspective (Projective or Homography)
In a perspective transform, we have an arbitrary 3x3 matrix, P, applied to an augmented
point:
The first two columns represent the scale-rotation component.
The last column represents the shift component.
Perspective (Projective or Homography)
Geometric Transforms
Translation
Rigid
Similarity
Affine
Perspective
2
3
4
6
8
Orientation
Lengths
Angles
Parallelism
Straight lines
Transform Type Degrees of Freedom Attribute
preserved

02 image processing

  • 2.
    2 This presentation isfor informational purposes only. INTEL MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY. Intel technologies’ features and benefits depend on system configuration and may require enabled hardware, software or service activation. Performance varies depending on system configuration. Check with your system manufacturer or retailer or learn more at intel.com. This sample source code is released under the Intel Sample Source Code License Agreement. Intel and the Intel logo are trademarks of Intel Corporation in the U.S. and/or other countries. *Other names and brands may be claimed as the property of others. Copyright © 2018, Intel Corporation. All rights reserved. Legal Notices and Disclaimers
  • 3.
    Processing Type Applications ofImage Processing Example Applications Enhancement Example: Changing saturation, contrast to enhance look • Marketing • Photography Understanding Example: Object detection, image classification to understand image content • Self-driving cars • Security Perturbation Example: Blurring, rotating images to produce desired effect • Censoring • Animations
  • 4.
    Image Processing Tools Jupyter Notebook*† Matlab* C++ IDEs (IntegratedDevelopment Environments) Languages Python*† Libraries NumPy† OpenCV† Focus of this lesson † = Taught in this course
  • 5.
    If you doarithmetic with NumPy, overflow can occur: Here, uint8 stores a max of 256: 300 is rounded down to 300 - 256 = 44. To avoid this, we have to promote to a bigger type than our expected result, do arithmetic, then convert back to smaller type: Here, we promote uint8 to uint16, do arithmetic, convert back into uint8. Computer Arithmetic with NumPy
  • 6.
    Or • Can useopenCV arithmetic, which handles overflow (numbers being added to quantities greater than 255), and clipping (automatically reducing numbers greater than 255 down to 255) for us. • With OpenCV, adding 100+200 with a max of 255 gets rounded down to 255, as expected. Computer Arithmetic with OpenCV
  • 7.
    Resizing an imagerequires reassigning pixels from the original image to the new image space. Matplotlib* and OpenCV have the same interpolation methods. The difference: Matplotlib applies interpolation at display time. OpenCV applies interpolation prior to displaying the images. Interpolation is used to address forward projection problems. These problems are defined as: 1. Stretching the size of an image creates locations in the new image where pixels do not have values. 2. Shrinking the size of an image creates float pixel values that need to be assigned to an integer pixel value in the new image. Interpolation
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
    spline16 quadric laczoshermite kaiser mitchell sinc hamming bessel hanning guassian spline36 catron area You can play with these, but we will not get into them. Other Interpolation in OpenCV
  • 13.
    Imshow arguments o (m,n): Rows,columns: NOT x,y coordinates Can (m,n,3) and (m,n,4) Values uint8 or float in [0,1] (m,n,3) = RGB (m,n,4) = RGBA A  alpha o “cmap” Color map: can be RGB, grayscale, and so on See upcoming slide for examples o “vmin”, “vmax” Used to change brightness Display with matplotlib*: imshow
  • 14.
    Display of 2DArrays with matplotlib*: matshow
  • 15.
    vmin (minimum brightness)defaults to min data vmax (maximum brightness) defaults to max of data Scalar, optional, default: None Used to normalize Tip: To prevent scaling (0,255) for a restricted range (say 100,200), pass in vmin=0, vmax=255. Display with matplotlib*: vmin and vmax
  • 16.
    Interpolation = 'none'and interpolation = 'nearest' • Are equivalent when converting a figure to an image file (that is, PNG) Interpolation = 'none' and interpolation = 'nearest' • Behave quite differently when converting a figure to a vector graphics file (that is, PDF) In our examples, interpolation = 'none' works well when a big image is scaled down. While interpolation = 'nearest' works well when a small image is scaled up. Display with matplotlib*: Interpolation
  • 17.
  • 18.
  • 19.
    Color Spaces: Introduction Allimages are represented by pixels with values in color spaces. Must represent an image in a color space before applying any processing techniques. Examples of color spaces: Grayscale (black and white) Color RGB (red, green, blue) HSV (hue, saturation, value) CMYK (cyan, magenta, yellow, black)
  • 20.
    Color Spaces: Grayscale Grayscaleto RGB conversion assumes all components of images are equal. RGB to grayscale conversion uses Y = (0.299)R + (0.587)G + (0.114)B
  • 21.
    Color Spaces: RGB(Red, Green, Blue) RGB (red, green, blue) 8-bit images 0-255 16-bit images 0-65,536 Floating point 0.0-1.0
  • 22.
    Color Spaces: HSV(Hue, Saturation, Value) Hue is normally 0-360 • Hue is walking around a color wheel • OpenCV uses 0-179 to fit in 8 bit Saturation (s) and value (v) are ranges of light to dark (intensity) • Trade off black/white • OpenCV uses 0-255 for both s and v In cone models • s is white; distance from cone axis • v is black; distance from cone point
  • 23.
    Color Conversion inOpenCV RGB is 8- or 16-bit colors. Each pixel is represented by three values (one for each channel). These values are pixel intensities 000 = black 111 = white
  • 24.
    Color Conversion inOpenCV When converted to grayscale, all RGB are equal. When converted from RGB (or BGR) to grayscale: 𝑌 = 0.299 𝑅 + 0.587 𝐺 + 0.114 𝐵
  • 25.
  • 26.
  • 27.
    Histograms are away of showing the intensity of color across an image. Abnormal histograms can be used to detect poor image exposure, saturation, and contrast. Example: The image on the left has poor exposure, which can be detected via the histogram. Histograms: Introduction and Grayscale Example
  • 28.
  • 29.
  • 30.
  • 31.
    Histogram Equalization Histogram equalization:Technique to adjust contrast to evenly spread y-values from an original distribution to a new distribution. Usually increases global contrast of an image.
  • 32.
    Histogram Equalization: ProbabilityIntegral Transform Recall our original distribution: • Bimodal peaks • Want to flatten this effect to get a uniform distribution Probability integral transform allows us to convert an arbitrary distribution to a uniform distribution
  • 33.
    Histogram Equalization: ProbabilityIntegral Transform Covert an event from an arbitrary to a uniform distribution. OriginalConverted
  • 34.
    Histogram Equalization: ProbabilityIntegral TransformOur original is a distribution of pixel intensities. We will use percentiles from both the arbitrary distribution and the uniform distribution to do this conversion. We need to know, for our input intensity x, what percentile it falls in to. We compute the cumulative (running total) of the probabilities of the values less than x. Once we know that, we have an output value between [0,1] or U(0,1) We can map U(0,1) to U(0,255) to get uniformly scaled pixel intensities. Simple linear scaling for (0,1) to (0, 255)
  • 35.
  • 36.
    Foreground/Background Only part ofthe image has important data; we call these regions of interest (ROI). In this case, we are interested in the foreground, but NOT the background of the logo image. foreground background foreground
  • 37.
    In the casewhen the background has distinctively different color than the foreground, we can use thresholding to separate them. Example application: Thresholding Application Image source: https://upload.wikimedia.org/wikipedia/commons/2/23/Gfp-texas-big-bend-national-park-plants-on-the-desert-horizon.jpg
  • 38.
  • 39.
    Thresholding: Foreground VersusBackground We need to build and interpret an image as foreground and background. We need to make a final decision about points. We will create a mask to denote regions of interest (foreground versus background).
  • 40.
    Thresholding: Simple Methods cv2.threshold() methods: These methods are global / independent of neighbors.
  • 41.
  • 42.
    Thresholding: Neighborhood Methods cvAdaptiveThreshold() is used when there is uneven illumination. To calculate threshold for neighborhood: Calculate neighborhood means, using equal weights or weighted-averages based on Gaussian windows/filters. Subtract neighborhood mean from each point in the neighborhood.
  • 43.
    Code to plotneighborhood thresholding: Thresholding: Neighborhood Methods
  • 44.
  • 45.
    Thresholding: Otsu’s Method InOtsu's method, we don't know the classes: • We try every break point (for example, say 127, or an empirical mean or median) and assume it is the separator. • The data is in two classes and we can compute the ratio of the within class variance and the between class variance. • Only one break point maximizes the ratio of within class variance to between class variance. This is our desired threshold. Generally, Otsu works well with a large region of interest (compared to background) and a strong bimodal distribution.
  • 46.
    Thresholding: Otsu’s Method Optimalthreshold unclear (OpenCV finds 116) Optimal threshold = 126
  • 47.
  • 48.
    Geometric Transforms: Resizing x,y fx(x,y), fy(x,y) Decreasing resolution (throwing out data) requires decimation Increasing resolution (creating data from nothing) requires interpolation
  • 49.
    Matrix Glossary T Translation RRotation A Affine (arbitrary) S Scaling H Homography (prospective, projective)
  • 50.
    Translation What you wouldsee in trig class: OpenCV affine matrix:
  • 51.
    Translation The OpenCV affinematrix is applied to an augmented p • p with a constant 1 tacked on to it.
  • 52.
  • 53.
    Rigid (Rotation andTranslation) Again, in OpenCV, this is applied to an augmented point:
  • 54.
    Rigid (Rotation andTranslation)
  • 55.
    Scale (Similarity Transform) Whatyou would see in trig class: OpenCV affine matrix:
  • 56.
  • 57.
    Scale When combining scalingwith other transformation: When xscale= yscale = 1 We get a rigid transformation – Also called isometry or a Euclidean transformation – Preserves scale (distance) When xscale= yscale We have a similarity transformation – Also called a scaled-rotation
  • 58.
    Perspective (Projective orHomography) In a perspective transform, we have an arbitrary 3x3 matrix, P, applied to an augmented point: The first two columns represent the scale-rotation component. The last column represents the shift component.
  • 59.
  • 60.

Editor's Notes

  • #4 Image url?
  • #6 x= np.unit8(200) + np.unit8(100) np.clip(x, 0, 255) Clipping happens after the value to clip is calculated
  • #18 https://matplotlib.org/users/colormaps.html
  • #23 https://commons.wikimedia.org/wiki/File:Color_solid_comparison_hsl_hsv_rgb_cone_sphere_cube_cylinder.png
  • #29 Image url?
  • #32 For colors, convert to HSV; apply to V
  • #34 Note: In the case of a purely continuous distribution the result will be truly uniform. But using our discrete approximations, the target will show artifacts and will be mostly uniform (similar to histogram binning).
  • #35 NOTE: This is all on grayscale; for color images, convert to HSV and perform on V
  • #38 Image source: https://upload.wikimedia.org/wikipedia/commons/2/23/Gfp-texas-big-bend-national-park-plants-on-the-desert-horizon.jpg
  • #49 NOTE: Can resize using a special case, but most transformations require special machinery (which we will see in the next slides). Uses a low-pass filter to keep rth sample points. In reality, only evaluates at rth points.
  • #51 NOTE: all of the geometric transformations in OpenCV are defined in terms of an affine matrix; simple operations may have a simpler operations structure… For example, translation has an identity matrix as the left-hand components.
  • #52 (pronounced “p-hat”) NOTE: all of the geometric transformations in OpenCV are defined in terms of an affine matrix; simple operations may have a simpler operations structure… For example, translation has an identity matrix as the left-hand components.
  • #54 NOTE: Here, we have replaced the identity part of the affine matrix with sin() cos() pieces that represent a rotation and the shift portion has been set to zeros. We could use an affine matrix with both rotation components and shift components.