Geometry Operations
- Shin Yoonah, Yoonah
- 2022년 7월 19일
- 2분 분량
최종 수정일: 2022년 8월 8일
Types of geometry operations
1.Scaling
2.Translation
3.Rotation
To begin with, this is a basic channel of an image = f(y,x)

y = vertical direction
x = horizontal direction
In geometric transformation, we change the coordinates of the image x and y
f(y,x) => geometric transformation => g(y',x')
g(y',x') = the new image changed by the geometric transformation
Subset of geometric transformation called Affine Transformation
1.Scaling
= reshape the image, we can shrink or expand the image in a horizontal and or vertical direction
Shrinking the image or making the image larger
Scaling: x' = ax
a = scaling factor
ex) scale the image by 2
x' = 2x
y' = y
g(y,2x) = f(y,x)
g(y,2x) will look stretched relative to image f

Ex)

Several column's of g have no corresponding value in f
--> to determine the unknown pixel value, we use interpolation
Interpolation is where we used neighboring pixels to determine the value of an unknown pixel
*In this case, we use the nearest neighbors; the just assigns the value based on the nearest pixel*

Other methods that PIL and open cv use

Image is stretched in the horizontal direction
- use this method to make the image larger
- if the values of a or d are less than zero; the image will shrink
2.Translation
= shift the image
x' = x + tx
We can shift an image horizontally by adding the number of pixels "tx" then by mapping the new location x prime
x' = x + 2 (add two pixels)

Shift the image vertically by adding pixels ty, this shifts the image vertically
y' = y + ty
y' = y + 2

Represent a geometric transformation as a set of equation
x' = ax + tx
y' = dy + ty
Putting the equations in matrix form, we get the Affine Transformation

With open cv, you input this matrix as an array

3.Rotation
We can rotate an image by an angle theta, where the red lines represent the original orientation of the horizontal and vertical access

Use matrix to rotate an image

Rotation matrix will perform a counter-clockwise rotation
*Expression for the matrix*

Simply assuming the isotropic scale factor "r" is 1
PIL and open cv only remove the parameter theta
*PIL, y scale the image by specifying the integer number of pixel's using the method "resize"*
from PIL import image
image = Image.open("lenna.png")
width = 512
height = 512
new_width = 2*width
new_height = height
new_image = image.resize((new_width, new_height)) -----> can disable the width of an image, by
applying the method resize the
image is twice as wide
Rotate the image
image = Image.open("lenna.png")
theta = 45
new_image = image.rotate(theta) ----> the input is the angle we would like to rotate the image by
*open cv*
import cv2
image = cv2.imread("lenna.png")
new_image = cv2.resize(image, None, fx=2, fy=1, interpolation=cv2.INTER_CUBIC)
=> the result will make the image twice as wide
Translation requires the Affine Transformation matrix M
where tx is the number of pixels you shift the location in the horizontal direction
rows,cols,__ = image.shape
tx = 100
ty = 0
M = np.fbat32[[(1,0,tx),(0,1,ty)]]
ty is the number of pixels you shift in the vertical direction
We input the image and matrix into the function warpAffine, we also input the shape of the output image
new_image = cv2.warpAffine(image,M,(cols,rows))
*Bonus*
theta = 45.0
M = cv2.getRotationMatrix2D (center = (cols/2-1,rows/2-1), angle=theta, scale=1)
- obtain the matrix to rotate the image using getRotationMatrix2D, this will make the image by angle theta
- The parameter center is the center of the rotation in the source, scale will be set to one
new_image = cv2.warpAffine(image,M,(cols,rows))
Copyright Coursera All rights reserved
ความคิดเห็น