Download OpenCV-Python Tutorials Documentation PDF

TitleOpenCV-Python Tutorials Documentation
File Size4.6 MB
Total Pages273
Table of Contents
                            OpenCV-Python Tutorials
	Introduction to OpenCV
	Gui Features in OpenCV
	Core Operations
	Image Processing in OpenCV
	Feature Detection and Description
	Video Analysis
	Camera Calibration and 3D Reconstruction
	Machine Learning
	Computational Photography
	Object Detection
	OpenCV-Python Bindings
Indices and tables
Document Text Contents
Page 1

OpenCV-Python Tutorials

Release beta


Nov 09, 2017

Page 136

OpenCV-Python Tutorials Documentation, Release beta

# sobel in y direction
sobel_y= np.array([[-1,-2,-1],

[0, 0, 0],
[1, 2, 1]])

# laplacian
laplacian=np.array([[0, 1, 0],

[1,-4, 1],
[0, 1, 0]])

filters = [mean_filter, gaussian, laplacian, sobel_x, sobel_y, scharr]
filter_name = ['mean_filter', 'gaussian','laplacian', 'sobel_x', \

'sobel_y', 'scharr_x']
fft_filters = [np.fft.fft2(x) for x in filters]
fft_shift = [np.fft.fftshift(y) for y in fft_filters]
mag_spectrum = [np.log(np.abs(z)+1) for z in fft_shift]

for i in xrange(6):
plt.subplot(2,3,i+1),plt.imshow(mag_spectrum[i],cmap = 'gray')
plt.title(filter_name[i]), plt.xticks([]), plt.yticks([])

See the result:

From image, you can see what frequency region each kernel blocks, and what region it passes. From that information,
we can say why each kernel is a HPF or a LPF

132 Chapter 1. OpenCV-Python Tutorials

Page 137

OpenCV-Python Tutorials Documentation, Release beta

Additional Resources

1. An Intuitive Explanation of Fourier Theory by Steven Lehar

2. Fourier Transform at HIPR

3. What does frequency domain denote in case of images?


1.4.12 Template Matching


In this chapter, you will learn

• To find objects in an image using Template Matching

• You will see these functions : cv2.matchTemplate(), cv2.minMaxLoc()


Template Matching is a method for searching and finding the location of a template image in a larger image. OpenCV
comes with a function cv2.matchTemplate() for this purpose. It simply slides the template image over the input
image (as in 2D convolution) and compares the template and patch of input image under the template image. Several
comparison methods are implemented in OpenCV. (You can check docs for more details). It returns a grayscale image,
where each pixel denotes how much does the neighbourhood of that pixel match with template.

If input image is of size (WxH) and template image is of size (wxh), output image will have a size of (W-w+1, H-h+1).
Once you got the result, you can use cv2.minMaxLoc() function to find where is the maximum/minimum value. Take
it as the top-left corner of rectangle and take (w,h) as width and height of the rectangle. That rectangle is your region
of template.

Note: If you are using cv2.TM_SQDIFF as comparison method, minimum value gives the best match.

Template Matching in OpenCV

Here, as an example, we will search for Messi’s face in his photo. So I created a template as below:

We will try all the comparison methods so that we can see how their results look like:

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('messi5.jpg',0)
img2 = img.copy()
template = cv2.imread('template.jpg',0)

1.4. Image Processing in OpenCV 133

Page 272

OpenCV-Python Tutorials Documentation, Release beta

CV_PROP_RW int queryIdx; // query descriptor index
CV_PROP_RW int trainIdx; // train descriptor index
CV_PROP_RW int imgIdx; // train image index

CV_PROP_RW float distance;

Some other small classes/structs can be exported usingCV_EXPORTS_W_MAP where it is exported to a Python native
dictionary. Moments() is an example of it.

class CV_EXPORTS_W_MAP Moments

//! spatial moments
CV_PROP_RW double m00, m10, m01, m20, m11, m02, m30, m21, m12, m03;
//! central moments
CV_PROP_RW double mu20, mu11, mu02, mu30, mu21, mu12, mu03;
//! central normalized moments
CV_PROP_RW double nu20, nu11, nu02, nu30, nu21, nu12, nu03;


So these are the major extension macros available in OpenCV. Typically, a developer has to put proper macros in
their appropriate positions. Rest is done by generator scripts. Sometimes, there may be an exceptional cases where
generator scripts cannot create the wrappers. Such functions need to be handled manually. But most of the time, a
code written according to OpenCV coding guidelines will be automatically wrapped by generator scripts.

268 Chapter 1. OpenCV-Python Tutorials

Page 273


Indices and tables

• genindex

• modindex

• search


Similer Documents