31
Intelligent Control Systems Image Processing (2) — Filtering, Geometric Transforms and Colors — Shingo Kagami Graduate School of Information Sciences, Tohoku University swk(at)ic.is.tohoku.ac.jp http://www.ic.is.tohoku.ac.jp/ja/swk/

Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

Intelligent Control Systems

Image Processing (2)— Filtering, Geometric Transforms and Colors —

Shingo KagamiGraduate School of Information Sciences,

Tohoku Universityswk(at)ic.is.tohoku.ac.jp

http://www.ic.is.tohoku.ac.jp/ja/swk/

Page 2: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

Setup (for Windows, updated)

2Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

A portable package for this class is available (prepared in USB memories). It requires 1 GB disk space.

• Copy Miniconda3.zip to your PC

• Unzip the contents into an arbitrary forlder, say, C:¥ic2018• This will generate C:¥ic2018¥Miniconda3 and

C:¥ic2018¥ic2018_python3.bat• Note: Using a 3rd-party unzipper (e.g. 7-zip in the USB

memory) is recommended. Windows’ unzipper may be slow

• Rewrite the path name C:¥ic2018 in ic2018_python3.bat to your own arbitrary folder name

• Unzip the sample codes and images into C:¥ic2018¥sample folder

Page 3: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

Running Codes

3Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

Within this Command Prompt, the installed version of python is active.

set MINICONDA_DIR=C:¥ic2018¥Miniconda3set PATH=%MINICONDA_DIR%;%MINICONDA_DIR%¥Scripts;%MINICONDA_DIR%¥Library¥bin;%PATH%

Run env_variable.bat to open a Command Prompt.(Or, open Command Prompt (cmd.exe) and execute the following commands:

cd C:¥ic2018¥samplepython thresh.py

start spyder3

If you want to change the language of Spyder, open in the Spyder menu: Tools -> Preferences -> General -> Advanced Settings -> Language

)

Note: not spyder but spyder3

Page 4: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

FYI: How this package is prepared

4Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

Miniconda3 with Python 3.6 for Windows (64 bit)https://www.anaconda.com/• Install for “just me”• Destination: C:¥ic2018¥Miniconda3• Uncheck all the Advanced Options

In Command Prompt with PATH variable set (see previous page):

pip install opencv-pythonpip install opencv-contrib-pythonpip install numbapip install matplotlibpip install scipypip install spyder

arbitrary folder of your choice

Page 5: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

Taxonomy

5Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

input output

imageimage image-to-image conversion(2-D data)

1-D data projection, histogram

scalar values position, object label

example

Page 6: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

Image to Image

6Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

image image

{ Fx,y } { Gx,y }

point operationGi,j depends only on Fi,j

local operation / neighboring operationGi,j depends on pixels within some neighborhood of Fi,j

global operationGi,j depends on almost all the pixels in { Fi,j }

(thresholding, pixel value conversion, …)

Page 7: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

7Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (3)

Local operation example: Spatial Filter

{ Fx,y } { Gx,y }

Gx,y{ Fi,j }, (i, j) ∈ Neighbor(x,y)

Gx,y depends on some neighborhood (e.g. 3£ 3, 5£ 5 pixels, etc.) of the point of interest (x,y)

Typical examples: smoothing, edge detection

Page 8: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

8Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (3)

Important Example: Smoothing• Output at (x, y): some representative value of the set of neighbor

pixels around (x, y), e.g. mean, weighted mean, median• Used for: e.g. noise reduction, scale-space processing

Gx,y{ Fi,j }, (i, j) ∈ Neighbor(x,y)

1/16 1/8 1/16

1/8 1/4 1/8

1/16 1/8 1/16

1/9 1/9 1/9

1/9 1/9 1/9

1/9 1/9 1/9

(mean) (weighted mean)

Page 9: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

9Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (3)

Linear Spatial Filtering• Smoothing with (weighted) mean is an example of linear

spatial filtering (while smoothing with median is nonlinear)• Computed by convolving a weight matrix (filter coefficients,

filter kernel, or mask) to input image

{ Fx,y } { Gx,y }

Gx,y{ Fi,j }, (i, j) ∈ Neighbor(x,y)

weight matrix

pixel-wise multiplicationand summation

Page 10: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

10Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (3)

Examples of 3x3 smoothing weight matrices

1 1 1

1 2 1

1 1 1

0 1 0

1 4 1

0 1 0

1 1 1

1 1 1

1 1 11/9 1/10 1/8

1/10 1/10 1/10

1/10 1/5 1/10

1/10 1/10 1/10

0 1/8 0

1/8 1/2 1/8

0 1/8 0

1/9 1/9 1/9

1/9 1/9 1/9

1/9 1/9 1/9

= = =

Page 11: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

Implementation of 3x3 filtering

11Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

weight = 1.0/8 * np.array([[0, 1, 0],[1, 4, 1], [0, 1, 0]])

for j in range(1, height - 1):for i in range(1, width - 1):

sum = 0.0for n in range(3):

for m in range(3):sum += weight[n, m] * src[j + n - 1, i + m - 1]

dest[j, i] = int(saturate(sum))

Generates [1, 2, …, height - 2](a lazy way of boundary handling)

Unlike the mathematical definition, the center coordinate of weight is not (0, 0) but (1, 1)

Page 12: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

OpenCV functions for common filters

12Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

cv2.filter2D()

cv2.GaussianBlur()cv2.Sobel()cv2.Laplacian()…

cv2.medianBlur()cv2.dilate()cv2.erode()…

Page 13: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

13Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (3)

Gaussian: most widely used smoothing kernel

• Discretized in space for computation

• Coefficient values are sometimes rounded to integer (for efficiency)

• Amount of smoothing can be controlled by parameter σ(large σ requires large matrix size)

separable in x and y

Page 14: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

14Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (3)

Frequency-domain understanding

: 2-D discrete Fourier transform

0 1 0

1 4 1

0 1 0

Recall: Fourier transform of Gaussian function is Gaussian

(zero-paddedto 256x256 and)

Page 15: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

15Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (3)

Edge Detection

• Spatial differentiation (approximated by finite difference)0 0 0

-1 0 1

0 0 0

0 -1 0

0 0 0

0 1 0

1st order diff. in x direction 1st order diff. in y direction

-1 0 1

-2 0 2

-1 0 1

-1 -2 -1

0 0 0

1 2 1

• Often combined with smoothing:

Sobel filter in x direction Sobel filter in y direction

Page 16: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

16Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (3)

Edge detection by 2nd order derivative

• Edge = zero crossing of 2nd order derivative

• Laplacian ∂2/∂x2 + ∂2/∂y2 is the lowest-order isotropic differential operator

• does not depend on direction of edges

• Laplacian operator is realized by adding 2nd order differentials fi+1 – 2 fi + fi – 1 of x and y directions

0 1 0

1 –4 1

0 1 0

Page 17: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

17Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (3)

Sharpening

Subtract the Laplacian image from the original image to yield an edge-enhanced image

0 -1 0

-1 5 -1

0 -1 0

0 0 0

0 1 0

0 0 0

0 1 0

1 –4 1

0 1 0– =

Page 18: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

18Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (3)

Frequency-domain visualization

1 2 1

0 0 0

-1 -2 -1

0 1 0

1 -4 1

0 1 0

DC in y direction

highest frequencyin y direction

Q: Why is Sobel a band-pass filter instead of high-pass?

Laplacian:

Sobel:

Page 19: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

Deep Convolutional Neural Networks

19Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

Alex Krizhevsky, Ilya Sutskever, Geoffrey E. Hinton, ImageNet Classification with Deep Convolutional Neural Networks, NIPS 2012.

Page 20: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

Global operation example: Warping

20Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

{ Fx,y } { Gx,y }

• Gx,y is sampled from Fx’,y’ where (x’, y’) is determined from (x, y)

Page 21: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

Important Geometric Transforms

21Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

Translation

Similarity Transform

Affine Transform

Homography Transform(Perspective Transform)

(Collineation)

Page 22: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

Understanding Homography (1/3)

22Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

x

y

Z

camera’soptical center

O

object point (X, Y, Z)T

image point(x, y)T

X

Y

f: focal length

X-Y-Z: camera coordinate framex-y: (normalized) image coordinate frame

Of Z

Xx

u

v

u-v: image coordinate (pixel coordinate) frame

Page 23: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

Understanding Homography (2/3)

23Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

A: 3x3 camera intrinsic matrix

R: 3x3 rotation matrixt: 3D translation vector

By substituting into

For an arbitrary object coordinate frame X’-Y’-Z’,

Page 24: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

Understanding Homography (3/3)

24Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

Z

O

object point (X', Y', 0)T

X

Y

• H determines bijective mapping between (u, v) and (X’, Y’)• H is computed when n (n ≥ 4) corresponding points are given

u

v

When the object point is on a plane,its coordinate is assumed to be (X’, Y’, 0) without loss of generality

Page 25: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

Homography Warping by OpenCV

25Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

src_pnts = np.array([[100, 100], [200, 100], [200, 200], [100, 200]], np.float32)

dest_size = 256dest_pnts = np.array([[0, 0],

[dest_size - 1, 0], [dest_size - 1, dest_size - 1], [0, dest_size - 1]], np.float32)

H = cv2.getPerspectiveTransform(src_pnts, dest_pnts)

output = cv2.warpPerspective(input, H, (dest_size, dest_size))

4 points [X, Y]’s

4 points [X’, Y’]’s

Page 26: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

Color Image Representation

26Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

B0,0 B1,0

B0,1 B1,1

B0,N-1 B1,N-1

G0,0

G0,1

G0,N-1

R0,0

R0,1

R0,N-1

G1,0

G1,1

G1,N-1

R1,0

R1,1

R1,N-1

x axis

y axis

single pixel = consecutive 3 bytes

Page 27: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

Representation in OpenCV for Python (NumPy)

27Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

fruits = cv2.imread('fruits.jpg')fruits.shape

-> (480, 512, 3)

fruits[100, 100]-> array([ 52, 98, 116], dtype=uint8)

fruits[100, 100, 0]-> 52

(Y, X) array with 3 channels (or, (Y, X, 3) tensor) is used

Page 28: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

28Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (4)

RGB Color SpaceWhy R, G, and B?

• Our eyes have three types of wavelength-sensitive cells (cone cells)

• cf. rod cells• So, the color space we perceive is three-dimensional

http://commons.wikimedia.org/wiki/File:Cone-response.png

Page 29: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

29Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (4)

Other Color Spaces

XYZ, L*a*b, L*u*vdefined by CIE (Commission Internationale de l‘Eclairage)

YIQ, YUV, YCbCrused in video standards (NTSC, PAL, …)

HSV (HSI, HSL)based on Munsell color system

cf. CMY, CMYK (for printing; subtractive color mixture)

output = cv2.cvtColor(input, cv2.COLOR_BGR2HSV)

Page 30: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

30Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2016 (4)

HSV Color Space

value = 1

value = 2/3

value = 1/3

red

yellowgreen

cyan

blue magenta

SaturationHue

Value

Common Definition: 0 ≤ Hue ≤ 360, 0 ≤ Saturation ≤ 1, 0 ≤ Value ≤ 1OpenCV (uint8): 0 ≤ Hue ≤ 180, 0 ≤ Saturation ≤ 255, 0 ≤ Value ≤ 255

Page 31: Shingo Kagamiswk/lecture/ic2018/kagami_ic2018_02.pdfFYI: How this package is prepared Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2) 4 Miniconda3 with Python 3.6

References

31Shingo Kagami (Tohoku Univ.) Intelligent Control Systems 2018 (2)

Reference manuals for OpenCV and NumPy are in:• https://docs.opencv.org/3.4.1/• http://www.numpy.org/

• R. Szeliski: Computer Vision: Algorithms and Applications, Springer, 2010. (コンピュータビジョン,アルゴリズムと応用, 共立出版, 2013)

• A. Kaehler, G. Bradski: Learning OpenCV 3, O’Reilly, 2017. (詳解OpenCV 3, オライリー・ジャパン, 2018)

• ディジタル画像処理編集委員会, ディジタル画像処理, CG-ARTS協会, 2015.