opencv-python- study notes nine (image geometry translation)

Providing two opencv transfer function, the image may be any conversion.

cv.warpAffine and cv.warpPerspective. The first 2 * 3 matrix taken as an input. The second take 3 * 3 matrix as input.

1. Zoom

function:

cv.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])

parameter:

src: input image

dsize: target size, plastic, non-zero

dst: the target image size. dsize, or a src.size () calculated from

fx: scaling the horizontal axis, the non-0

fy: scaling the vertical axis, the non-0

interpolation: interpolation algorithm, divided into the following

INTER_NEAREST 
Python: cv.INTER_NEAREST

nearest neighbor interpolation nearest neighbor interpolation

INTER_LINEAR 
Python: cv.INTER_LINEAR

bilinear interpolation bilinear interpolation

INTER_CUBIC 
Python: cv.INTER_CUBIC

bicubic interpolation bicubic

INTER_AREA 
Python: cv.INTER_AREA

resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire'-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.基于局部像素的重采样

INTER_LANCZOS4 
Python: cv.INTER_LANCZOS4

Lanczos interpolation over 8x8 neighborhood. Lanczos interpolation based on the 8x8 pixel neighborhood

INTER_LINEAR_EXACT 
Python: cv.INTER_LINEAR_EXACT

Bit exact bilinear interpolation. Bit precise bilinear interpolation

INTER_MAX 
Python: cv.INTER_MAX

mask for interpolation codes. interpolation code mask

WARP_FILL_OUTLIERS 
Python: cv.WARP_FILL_OUTLIERS

flag, fills all of the destination image pixels. If some of them correspond to outliers in the source image, they are set to zero

WARP_INVERSE_MAP 
Python: cv.WARP_INVERSE_MAP

flag, inverse transformation

For example, linearPolar or logPolar transforms:

  • flag is not set: dst(ρ,ϕ)=src(x,y)
  • flag is set: dst(x,y)=src(ρ,ϕ)

For example

import numpy as np
import cv2 as cv


the src = cv.imread ( ' 4.jpg ' )
 # Method. 1 
RES1 = cv.resize (the src, None, = 1.2 FX, FY = 1.2, interpolation = cv.INTER_CUBIC)
 # Method 2 disposed directly output dimensions 
height, width = src.shape [: 2]   # obtain full size 
RES2 = cv.resize (the src, (int (0.5 * width), int (0.5 * height)), interpolation = cv.INTER_CUBIC)
 the while (. 1 ):
    cv.imshow("src", src)
    cv.imshow("res1", res1)
    cv.imshow("res2", res2)
    if cv.waitKey(1) & 0xFF == 27:
        break
cv.destroyAllWindows()

 

Translation

Translational movement of the object is, if you know the coordinates of the object translation (tx, ty), the transformation matrix can be created as follows

The array type np.float32 placed in the assignment matrix M to  cv.warpAffine ()  function. Can be realized translation

function

dst=cv.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])

parameter:

src The input image
dst The output image of the same type src
M 2 × . 3  transformation matrix
dsize Output image size
flags Methods of interpolation Combination (See  InterpolationFlags ) The optional In Flag and  WARP_INVERSE_MAP  that means that M IS The inverse Transformation (  DST the src  ). interpolation matrix, previous chapter the table.
borderMode Boundary pixel mode, the default is BORDER_CONSTANT, by filling constant boundary
borderValue 边界填充值; 默认为0,所以默认情况下填充为黑色

特别注意:dsize参数是指输出图像的宽高,即对应图像的列,行。

举例

import numpy as np
import cv2 as cv


img = cv.imread('4.jpg', 0)
rows, cols = img.shape
M = np.float32([[1, 0, 300], [0, 1, 50]])
dst
= cv.warpAffine(img, M, (cols, rows)) cv.imshow('img', dst) cv.waitKey(0) cv.destroyAllWindows()

旋转

平移和旋转都是仿射变换的特例,所用函数都是cv2.warpAffine,只是转换矩阵M有所不同。

图像旋转θ度是由变换矩阵  M 得到的

 

但是opencv改进了这个矩阵,如下图。使得提供了缩放旋转与可调的旋转中心。

     

上述矩阵表示绕 center.x,center.y 旋转 θ度

 

 函数:

  retval=cv.getRotationMatrix2D(center, angle, scale)获取变换矩阵M

 参数:

center 旋转中心
angle Rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner).旋转角度,角度为正则表示逆时针旋转
scale 旋转后的缩放系数

举例:

import numpy as np
import cv2 as cv


src = cv.imread('4.jpg', 0)
rows, cols = src.shape
# 旋转中心  旋转角度  缩放系数
M = cv.getRotationMatrix2D(((cols-1) / 2.0,(rows-1)/2.0), 90,1)
# 原图像 变换矩阵 输出图像尺寸中心
dst = cv.warpAffine(src, M, (cols, rows))

while(1):
    cv.imshow('src', src)
    cv.imshow('dst', dst)
    if cv.waitKey(1) & 0xFF == 27:
        break
cv.destroyAllWindows()

 

仿射变换

在仿射变换中,原图像中的所有平行线在输出图像中仍然是平行的,直线仍然是直线,为了找到变换矩阵,我们需要从输入图像中选取三个点,以及它们在输出图像中的对应位置。利用 cv.getAffineTransform创建一个2*3 的变换矩阵,赋值给cv.warpAffine

函数

retval=cv.getAffineTransform(src, dst)

参数:

src Coordinates of triangle vertices in the source image.原图中3个点所组成的矩阵,数据类型为np.float32
dst Coordinates of the corresponding triangle vertices in the destination image.目标图中对应的3个点所组成的矩阵,数据类型为np.float32                                                         

举例

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt


img = cv.imread('4.jpg')
rows, cols, CH = img.shape 
matrix picture # 3 points
PTS1 = np.float32 ([[50, 50], [200 is, 50], [50, 200 is ]])

# 3 output points matrix
PTS2 = np.float32 ([[10, 100], [200, 50], [100, 250 ]])
M = cv.getAffineTransform (pts1, pts2) 
dst
= cv.warpAffine (img, M, (cols, rows))
plt.subplot (
121), plt.imshow (img), plt.title ( ' Input ' )
Plt. subplot (
122), plt.imshow (ff), plt.title ( ' Output ' )
plt.show ()

 

Guess you like

Origin www.cnblogs.com/blog-xyy/p/11243109.html