91.204.201 Computing IV Chapter Three: imgproc module Image Processing Xinwen Fu.

Slides:



Advertisements
Similar presentations
Laboratory of Image Processing Pier Luigi Mazzeo
Advertisements

November 12, 2013Computer Vision Lecture 12: Texture 1Signature Another popular method of representing shape is called the signature. In order to compute.
Digital Image Processing
Grey Level Enhancement Contrast stretching Linear mapping Non-linear mapping Efficient implementation of mapping algorithms Design of classes to support.
Computational Biology, Part 23 Biological Imaging II Robert F. Murphy Copyright  1996, 1999, All rights reserved.
Chapter 3 Image Enhancement in the Spatial Domain.
Random Sampling and Data Description
Copyright © 2010, 2007, 2004 Pearson Education, Inc. All Rights Reserved. Lecture Slides Elementary Statistics Eleventh Edition and the Triola.
Extended Gaussian Images
Chapter 8 Content-Based Image Retrieval. Query By Keyword: Some textual attributes (keywords) should be maintained for each image. The image can be indexed.
Digital Image Processing Lecture11: Histogram Processing.
Image Processing IB Paper 8 – Part A Ognjen Arandjelović Ognjen Arandjelović
Image Segmentation Image segmentation (segmentace obrazu) –division or separation of the image into segments (connected regions) of similar properties.
CS324e - Elements of Graphics and Visualization Color Histograms.
Mapping: Scaling Rotation Translation Warp
Maths for Computer Graphics
HCI 530 : Seminar (HCI) Damian Schofield. HCI 530: Seminar (HCI) Transforms –Two Dimensional –Three Dimensional The Graphics Pipeline.
Raster Data. The Raster Data Model The Raster Data Model is used to model spatial phenomena that vary continuously over a surface and that do not have.
Chapter 4.1 Mathematical Concepts. 2 Applied Trigonometry Trigonometric functions Defined using right triangle  x y h.
Pattern Recognition. Introduction. Definitions.. Recognition process. Recognition process relates input signal to the stored concepts about the object.
MOHAMMAD IMRAN DEPARTMENT OF APPLIED SCIENCES JAHANGIRABAD EDUCATIONAL GROUP OF INSTITUTES.
College Algebra Fifth Edition James Stewart Lothar Redlin Saleem Watson.
1 Chapter 2 Matrices Matrices provide an orderly way of arranging values or functions to enhance the analysis of systems in a systematic manner. Their.
Robust fitting Prof. Noah Snavely CS1114
Using Matrices to Perform Geometric Transformations
October 8, 2013Computer Vision Lecture 11: The Hough Transform 1 Fitting Curve Models to Edges Most contours can be well described by combining several.
Geometric Intuition Randy Gaul. Vectors, Points and Basis Matrices Rotation Matrices Dot product and how it’s useful Cross product and how it’s useful.
CS 376b Introduction to Computer Vision 02 / 26 / 2008 Instructor: Michael Eckmann.
Transformations Aaron Bloomfield CS 445: Introduction to Graphics
Image Processing Lecture 2 - Gaurav Gupta - Shobhit Niranjan.
Adobe Photoshop CS Design Professional COLORS ADJUSTING.
© 2011 Delmar, Cengage Learning Chapter 11 Adjusting Colors.
Chapter 11 Adjusting Colors. Chapter Lessons Correct and adjust color Enhance colors by altering saturation Modify color channels using levels Create.
Technology and Historical Overview. Introduction to 3d Computer Graphics  3D computer graphics is the science, study, and method of projecting a mathematical.
Geometric Transformation. So far…. We have been discussing the basic elements of geometric programming. We have discussed points, vectors and their operations.
Chapter 2 Arithmetic Strategies & Area
ArrayCluster: an analytic tool for clustering, data visualization and module finder on gene expression profiles 組員:李祥豪 謝紹陽 江建霖.
BACKGROUND LEARNING AND LETTER DETECTION USING TEXTURE WITH PRINCIPAL COMPONENT ANALYSIS (PCA) CIS 601 PROJECT SUMIT BASU FALL 2004.
Computational Biology, Part E Basic Principles of Computer Graphics Robert F. Murphy Copyright  1996, 1999, 2000, All rights reserved.
Copyright © 2010 Pearson Education, Inc. Chapter 6 The Standard Deviation as a Ruler and the Normal Model.
Texture. Texture is an innate property of all surfaces (clouds, trees, bricks, hair etc…). It refers to visual patterns of homogeneity and does not result.
September 5, 2013Computer Vision Lecture 2: Digital Images 1 Computer Vision A simple two-stage model of computer vision: Image processing Scene analysis.
EE663 Image Processing Dr. Samir H. Abdul-Jauwad Electrical Engineering Department King Fahd University of Petroleum & Minerals.
CS654: Digital Image Analysis Lecture 18: Image Enhancement in Spatial Domain (Histogram)
Computer Graphics Chapter 6 Andreas Savva. 2 Interactive Graphics Graphics provides one of the most natural means of communicating with a computer. Interactive.
Image Segmentation in Color Space By Anisa Chaudhary.
June 14, ‘99 COLORS IN MATLAB.
CS COMPUTER GRAPHICS LABORATORY. LIST OF EXPERIMENTS 1.Implementation of Bresenhams Algorithm – Line, Circle, Ellipse. 2.Implementation of Line,
Chapter 2: Frequency Distributions. Frequency Distributions After collecting data, the first task for a researcher is to organize and simplify the data.
October 1, 2013Computer Vision Lecture 9: From Edges to Contours 1 Canny Edge Detector However, usually there will still be noise in the array E[i, j],
Computer Graphics Matrices
Basic Theory (for curve 01). 1.1 Points and Vectors  Real life methods for constructing curves and surfaces often start with points and vectors, which.
Digital Image Processing Image Enhancement in Spatial Domain
Geometric Transformations Ceng 477 Introduction to Computer Graphics Computer Engineering METU.
1 Objective To provide background material in support of topics in Digital Image Processing that are based on matrices and/or vectors. Review Matrices.
Computer Graphics CC416 Lecture 04: Bresenham Line Algorithm & Mid-point circle algorithm Dr. Manal Helal – Fall 2014.
3D Ojbects: Transformations and Modeling. Matrix Operations Matrices have dimensions: Vectors can be thought of as matrices: v=[2,3,4,1] is a 1x4 matrix.
Linear Algebra Review.
Geometric Transformations
1-Introduction (Computing the image histogram).
Mean Shift Segmentation
Feature description and matching
Sec. 1.1 HW Review Pg. 19 Titanic Data Exploration (Excel File)
Fitting Curve Models to Edges
Histogram Histogram is a graph that shows frequency of anything. Histograms usually have bars that represent frequency of occuring of data. Histogram has.
COMP 175: Computer Graphics February 9, 2016
Computer Vision Lecture 16: Texture II
CSC 381/481 Quarter: Fall 03/04 Daniela Stan Raicu
CHAPTER 1 Exploring Data
Game Programming Algorithms and Techniques
Presentation transcript:

Computing IV Chapter Three: imgproc module Image Processing Xinwen Fu

References  Application Development in Visual Studio Application Development in Visual Studio  Reading assignment: Chapter 3  An online OpenCV Quick Guide with nice examplesOpenCV Quick Guide By Dr. Xinwen Fu2

Matrix Algebra Basics Introduction

Matrix A matrix is any doubly subscripted array of elements arranged in rows and columns.

Row Vector [1 × n] matrix

Column Vector [m × 1] matrix

Square Matrix Same number of rows and columns

Transpose Matrix Rows become columns and columns become rows

Matrix Addition and Subtraction A new matrix C may be defined as the additive combination of matrices A and B where: C = A + B is defined by: Note: all three matrices are of the same dimension

Addition If and then

Matrix Addition Example

Matrix Subtraction C = A - B Is defined by

 If A is an m × n matrix and s is a scalar, let kA denote the matrix obtained by multiplying every element of A by k.  This procedure is called scalar multiplication. Scalar Multiplication

Matrix Multiplication Matrices A and B have these dimensions: [r x c] and [s x d]

Matrix Multiplication Matrices A and B can be multiplied if: [r x c] and [s x d] c = s

Matrix Multiplication The resulting matrix will have the dimensions: [r x c] and [s x d] r x d

Computation: A x B = C [2 x 2] [2 x 3]

Computation: A x B = C [3 x 2][2 x 3] A and B can be multiplied [3 x 3]

Computation: A x B = C [3 x 2][2 x 3] [3 x 3] Result is 3 x 3

Outline By Dr. Xinwen Fu20  3.15 Affine Transformations  3.16 Histogram Equalization  3.17 Histogram Calculation  3.18 Histogram Comparison  3.19 Back Projection  3.20 Template Matching  3.21 Finding contours in your image  3.22 Convex Hull  3.23 Creating Bounding boxes and circles for contours  3.24 Creating Bounding rotated boxes and ellipses for contours  3.25 Image Moments  3.26 Point Polygon Test

What is an Affine Transformation?  It is any transformation that can be expressed in the form of a matrix multiplication (linear transformation) followed by a vector addition (translation).  We can use an Affine Transformation to express: Rotations (linear transformation) Translations (vector addition) Scale operations (linear transformation)  In essence, an Affine Transformation represents a relation between two images. By Dr. Xinwen Fu21

Represent an Affine Transform  The usual way to represent an Affine Transform is by using a matrix.  Considering that we want to transform a 2D vector by using A and B, we can do it equivalently with: By Dr. Xinwen Fu22 or

How do we get an Affine Transformation?  We mentioned that an Affine Transformation is basically a relation between two images.  The information about this relation can come, roughly, in two ways: a.We know both X and T and we also know that they are related. Then our job is to find M b.We know M and X. To obtain T we only need to apply T = M.X. Our information for M may be explicit (i.e. have the 2-by-3 matrix) or it can come as a geometric relation between points. By Dr. Xinwen Fu23

Better Explanation of how to get M  Since M relates 2 images, we can analyze the simplest case in which it relates three points in both images. Look at the figure below: The points 1, 2 and 3 (forming a triangle in image 1) are mapped into image 2, still forming a triangle, but now they have changed notoriously. If we find the Affine Transformation with these 3 points (you can choose them as you like), then we can apply this found relation to the whole pixels in the image. By Dr. Xinwen Fu24

Example Code  Loads an image  Applies an Affine Transform to the image. This Transform is obtained from the relation between three points. We use the function warpAffine for that purpose.warpAffine  Applies a Rotation to the image after being transformed. This rotation is with respect to the image center  Waits until the user exits the program By Dr. Xinwen Fu25

Outline By Dr. Xinwen Fu26  3.15 Affine Transformations  3.16 Histogram Equalization  3.17 Histogram Calculation  3.18 Histogram Comparison  3.19 Back Projection  3.20 Template Matching  3.21 Finding contours in your image  3.22 Convex Hull  3.23 Creating Bounding boxes and circles for contours  3.24 Creating Bounding rotated boxes and ellipses for contours  3.25 Image Moments  3.26 Point Polygon Test

What is an Image Histogram?  It is a graphical representation of the intensity distribution of an image.  It quantifies the number of pixels for each intensity value considered. By Dr. Xinwen Fu27

What is Histogram Equalization?  It is a method that improves the contrast in an image, in order to stretch out the intensity range.  From the image above, you can see that the pixels seem clustered around the middle of the available range of intensities. What Histogram Equalization does is to stretch out this range.  Take a look at the figure below: The green circles indicate the under-populated intensities. After applying the equalization, we get an histogram like the figure in the center. The resulting image is shown in the picture at right. By Dr. Xinwen Fu28

How does it work?  Equalization implies mapping one distribution (the given histogram) to another distribution (a wider and more uniform distribution of intensity values) so the intensity values are spread over the whole range.  To accomplish the equalization effect, the remapping should be the cumulative distribution function (cdf)  For the histogram H(i), its cumulative distribution H’(i) is: By Dr. Xinwen Fu29

How does it work?  To use this as a remapping function, we have to normalize H’(i) such that the maximum value is 255 ( or the maximum value for the intensity of the image ). From the example above, the cumulative function is: H’(i) = (L-1) H’(i) = 255 H’(i)  Finally, we use a simple remapping procedure to obtain the intensity values of the equalized image: By Dr. Xinwen Fu30

Example Code  Loads an image  Convert the original image to grayscale  Equalize the Histogram by using the OpenCV function EqualizeHistEqualizeHist  Display the source and equalized images in a window. By Dr. Xinwen Fu31

Outline By Dr. Xinwen Fu32  3.15 Affine Transformations  3.16 Histogram Equalization  3.17 Histogram Calculation  3.18 Histogram Comparison  3.19 Back Projection  3.20 Template Matching  3.21 Finding contours in your image  3.22 Convex Hull  3.23 Creating Bounding boxes and circles for contours  3.24 Creating Bounding rotated boxes and ellipses for contours  3.25 Image Moments  3.26 Point Polygon Test

What are histograms?  Histograms are collected counts of data organized into a set of predefined bins  data can be intensity values and can also be whatever feature you find useful to describe your image.  Let’s see an example. Imagine that a Matrix contains information of an image (i.e. intensity in the range ): By Dr. Xinwen Fu33

By Dr. Xinwen Fu Counting the data  Since we know that the range of information value for this case is 256 values, we can segment our range in subparts (called bins) like:  Keep count of the number of pixels that fall in the range of each bin. For the example above we get the image below ( axis x represents the bins and axis y the number of pixels in each of them). 34

Histogram  Above is an example of histogram. An histogram can keep count color intensities, and whatever image features to measure (i.e. gradients, directions, etc).  Let’s identify some parts of the histogram: dims: The number of parameters you want to collect data of. In our example, dims = 1 because we are only counting the intensity values of each pixel (in a greyscale image). bins: the number of subdivisions in each dim. In our example, bins = 16 range: The limits for the values to be measured. In this case: range = [0,255]  What if you want to count two features? In this case your resulting histogram would be a 3D plot (in which x and y would be bin x and bin y for each feature and z would be the number of counts for each combination of (bin x, bin y ). The same would apply for more features. By Dr. Xinwen Fu35

Histogram in OpenCV  For simple purposes, OpenCV implements the function calcHist, which calculates the histogram of a set of arrays (usually images or image planes).calcHist  It can operate with up to 32 dimensions. By Dr. Xinwen Fu36

Example Code  Loads an image  Splits the image into its R, G and B planes using the function splitsplit  Calculate the Histogram of each 1-channel plane by calling the function calcHistcalcHist  Plot the three histograms in a window By Dr. Xinwen Fu37

Outline By Dr. Xinwen Fu38  3.15 Affine Transformations  3.16 Histogram Equalization  3.17 Histogram Calculation  3.18 Histogram Comparison  3.19 Back Projection  3.20 Template Matching  3.21 Finding contours in your image  3.22 Convex Hull  3.23 Creating Bounding boxes and circles for contours  3.24 Creating Bounding rotated boxes and ellipses for contours  3.25 Image Moments  3.26 Point Polygon Test

Theory  To compare two histograms (H 1 and H 2 ), first we have to choose a metric ( d(H 1, H 2 ) ) to express how well both histograms match.  OpenCV implements the function compareHist to perform a comparison. It also offers 4 different metrics to compute the matching:compareHist Correlation ( CV_COMP_CORREL ) Where and N is the total number of histogram bins. By Dr. Xinwen Fu39

More Metrics Chi-Square ( CV_COMP_CHISQR ) Intersection ( method=CV_COMP_INTERSECT ) Bhattacharyya distance ( CV_COMP_BHATTACHARYYA ) By Dr. Xinwen Fu40

Example Code  Loads a base image and 2 test images to be compared with it.  Generate 1 image that is the lower half of the base image  Convert the images to HSV format  Calculate the H-S histogram for all the images and normalize them in order to compare them.  Compare the histogram of the base image with respect to the 2 test histograms, the histogram of the lower half base image and with the same base image histogram.  Display the numerical matching parameters obtained. By Dr. Xinwen Fu41

Outline By Dr. Xinwen Fu42  3.15 Affine Transformations  3.16 Histogram Equalization  3.17 Histogram Calculation  3.18 Histogram Comparison  3.19 Back Projection  3.20 Template Matching  3.21 Finding contours in your image  3.22 Convex Hull  3.23 Creating Bounding boxes and circles for contours  3.24 Creating Bounding rotated boxes and ellipses for contours  3.25 Image Moments  3.26 Point Polygon Test

What is Back Projection?  Back Projection is a way of recording how well the pixels of a given image fit the distribution of pixels in a histogram model.  To make it simpler: For Back Projection, you calculate the histogram model of a feature and then use it to find this feature in an image.  Application example: If you have a histogram of flesh color (say, a Hue-Saturation histogram ), then you can use it to find flesh color areas in an image: By Dr. Xinwen Fu43

How does it work?  We explain this by using the skin example:  Let’s say you have gotten a skin histogram (Hue- Saturation) based on the image below.  The histogram besides is going to be our model histogram (which we know represents a sample of skin tonality).  You applied some mask to capture only the histogram of the skin area: By Dr. Xinwen Fu44

Test Image  Now, let’s imagine that you get another hand image (Test Image) like the one below: (with its respective histogram): By Dr. Xinwen Fu45

Back Projection By Dr. Xinwen Fu46  Use our model histogram (that we know represents a skin tonality) to detect skin areas in our Test Image.  Here are the steps In each pixel of our Test Image (i.e. p(i, j)), collect the data and find the correspondent bin location for that pixel (i.e. (h i,j, s i,j )). Lookup the model histogram in the correspondent bin - (h i,j, s i,j ) - and read the bin value. Store this bin value in a new image (BackProjection). Also, you may consider to normalize the model histogram first, so the output for the Test Image can be visible for you. Applying the steps above, we get the following BackProjection image for our Test Image:

What it means?  In terms of statistics, the values stored in BackProjection represent the probability that a pixel in Test Image belongs to a skin area, based on the model histogram that we use.  For instance in our Test image, the brighter areas are more probable to be skin area (as they actually are), whereas the darker areas have less probability (notice that these “dark” areas belong to surfaces that have some shadow on it, which in turns affects the detection). By Dr. Xinwen Fu47

Example Code  Loads an image  Convert the original to HSV format and separate only Hue channel to be used for the Histogram (using the OpenCV function mixChannels)mixChannels  Let the user to enter the number of bins to be used in the calculation of the histogram.  Calculate the histogram (and update it if the bins change) and the backprojection of the same image.  Display the backprojection and the histogram in windows. By Dr. Xinwen Fu48

Outline By Dr. Xinwen Fu49  3.15 Affine Transformations  3.16 Histogram Equalization  3.17 Histogram Calculation  3.18 Histogram Comparison  3.19 Back Projection  3.20 Template Matching  3.21 Finding contours in your image  3.22 Convex Hull  3.23 Creating Bounding boxes and circles for contours  3.24 Creating Bounding rotated boxes and ellipses for contours  3.25 Image Moments  3.26 Point Polygon Test

What is template matching?  Template matching is a technique for finding areas of an image that match (are similar) to a template image (patch). By Dr. Xinwen Fu50

How does it work?  We need two primary components:  Source image (I): The image in which we expect to find a match to the template image  Template image (T): The patch image which will be compared to the source image  Our goal is to detect the highest matching area: By Dr. Xinwen Fu51

Sliding Strategy  To identify the matching area, we have to compare the template image against the source image by sliding it:  By sliding, we mean moving the patch one pixel at a time (left to right, up to down). At each location, a metric is calculated so it represents how “good” or “bad” the match at that location is (or how similar the patch is to that particular area of the source image). By Dr. Xinwen Fu52

Sliding Example  For each location of T over I, you store the metric in the result matrix (R). Each location (x, y) in R contains the match metric.  The image below is the result R of sliding the patch with a metric TM_CCORR_NORMED. The brightest locations indicate the highest matches. As you can see, the location marked by the red circle is probably the one with the highest value, so that location (the rectangle formed by that point as a corner and width and height equal to the patch image) is considered the match. By Dr. Xinwen Fu53

Find the Highest Value  In practice, we use the function minMaxLoc to locate the highest value (or lower, depending of the type of matching method) in the R matrix. minMaxLoc By Dr. Xinwen Fu54

Matching methods available in OpenCV?  Good question. OpenCV implements Template matching in the function matchTemplate. matchTemplate  6 available methods: method=CV_TM_SQDIFF method=CV_TM_SQDIFF_NORMED By Dr. Xinwen Fu55

6 available methods (Cont’d)  method=CV_TM_CCORR  method=CV_TM_CCORR_NORMED By Dr. Xinwen Fu56

6 available methods (Cont’d)  method=CV_TM_CCOEFF  Where  method=CV_TM_CCOEFF_NORMED By Dr. Xinwen Fu57

Example Code  Loads an input image and a image patch (template)  Perform a template matching procedure by using the OpenCV function matchTemplate with any of the 6 matching methods described before. The user can choose the method by entering its selection in the Trackbar.matchTemplate  Normalize the output of the matching procedure  Localize the location with higher matching probability  Draw a rectangle around the area corresponding to the highest match By Dr. Xinwen Fu58

Outline By Dr. Xinwen Fu59  3.15 Affine Transformations  3.16 Histogram Equalization  3.17 Histogram Calculation  3.18 Histogram Comparison  3.19 Back Projection  3.20 Template Matching  3.21 Finding contours in your image  3.22 Convex Hull  3.23 Creating Bounding boxes and circles for contours  3.24 Creating Bounding rotated boxes and ellipses for contours  3.25 Image Moments  3.26 Point Polygon Test

Skipped By Dr. Xinwen Fu60

Outline By Dr. Xinwen Fu61  3.15 Affine Transformations  3.16 Histogram Equalization  3.17 Histogram Calculation  3.18 Histogram Comparison  3.19 Back Projection  3.20 Template Matching  3.21 Finding contours in your image  3.22 Convex Hull  3.23 Creating Bounding boxes and circles for contours  3.24 Creating Bounding rotated boxes and ellipses for contours  3.25 Image Moments  3.26 Point Polygon Test

Skipped By Dr. Xinwen Fu62

Outline By Dr. Xinwen Fu63  3.15 Affine Transformations  3.16 Histogram Equalization  3.17 Histogram Calculation  3.18 Histogram Comparison  3.19 Back Projection  3.20 Template Matching  3.21 Finding contours in your image  3.22 Convex Hull  3.23 Creating Bounding boxes and circles for contours  3.24 Creating Bounding rotated boxes and ellipses for contours  3.25 Image Moments  3.26 Point Polygon Test

Skipped By Dr. Xinwen Fu64

Outline By Dr. Xinwen Fu65  3.15 Affine Transformations  3.16 Histogram Equalization  3.17 Histogram Calculation  3.18 Histogram Comparison  3.19 Back Projection  3.20 Template Matching  3.21 Finding contours in your image  3.22 Convex Hull  3.23 Creating Bounding boxes and circles for contours  3.24 Creating Bounding rotated boxes and ellipses for contours  3.25 Image Moments  3.26 Point Polygon Test

Skipped By Dr. Xinwen Fu66

Outline By Dr. Xinwen Fu67  3.15 Affine Transformations  3.16 Histogram Equalization  3.17 Histogram Calculation  3.18 Histogram Comparison  3.19 Back Projection  3.20 Template Matching  3.21 Finding contours in your image  3.22 Convex Hull  3.23 Creating Bounding boxes and circles for contours  3.24 Creating Bounding rotated boxes and ellipses for contours  3.25 Image Moments  3.26 Point Polygon Test

Skipped By Dr. Xinwen Fu68

Outline By Dr. Xinwen Fu69  3.15 Affine Transformations  3.16 Histogram Equalization  3.17 Histogram Calculation  3.18 Histogram Comparison  3.19 Back Projection  3.20 Template Matching  3.21 Finding contours in your image  3.22 Convex Hull  3.23 Creating Bounding boxes and circles for contours  3.24 Creating Bounding rotated boxes and ellipses for contours  3.25 Image Moments  3.26 Point Polygon Test

Skipped By Dr. Xinwen Fu70