opencv入门3:图片操作-image transformations

如opencv开发前的准备工作中所说,此系列文章是在学习Practical Python and OpenCV(点击下载)这本书的一些记录,发出来的文章跳过了第三章对RGB,以及numpy简单操作等介绍,请大家下载原书查看,在原书中对一下段落已进行翻译注释。文章系列完整展示代码点击下载

Translation

平移translation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import numpy as np 
import cv2
import argparse
import imutils
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help ="Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("Original", image)
cv2.waitKey(0)

M = np.float32([[1,0,25],[0,1,50]])
#定义我们的平移矩阵M
#矩阵M被定义为一个浮点数组 - 这很重要,因为OpenCV期望这个矩阵是浮点类型的。
#矩阵的第一行是[1,0,tx],其中tx是像素的数量,我们将左右移动图像。
#tx的负值会将图像左移,正值会将图像向右移
#我们将矩阵的第二行定义为[0,1,ty],其中ty是我们将向上或向下移动图像的像素数量。
#ty的负值会使图像向上移动,正值会使图像向下移动。
shifted =cv2.warpAffine(image ,M ,(image.shape[1], image.shape[0]))
#warpAffine第一个参数是我们想要移动的图像,第二个参数是我们的平移矩阵M.最后,我们手动提供图像的尺寸(宽度和高度)作为第三个参数
cv2.imshow("Shifted Up and Left",shifted)

M = np.float32([[1,0,-50],[0,1,-90]])
shifted =cv2.warpAffine(image ,M ,(image.shape[1], image.shape[0]))
cv2.imshow("Shifted Up and Left",shifted)
cv2.waitKey(0)

以上多次使用warpAffine重复性很高而且 使用起来不方便,我们可以定义一个叫imutils.py的模块封装这个方法如下:

工具类imutils.py
1
2
3
4
5
6
7
import numpy as np
import cv2

def translate(image, x, y):
M = np.float32([[1, 0, x], [0, 1, y]])
shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
return shifted

再实现上面平移的动作:

平移translation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import numpy as np 
import cv2
import argparse
import imutils
#使用imutils中“convenience”方法来完成平移,旋转和调整大小等常见任务。
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help ="Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("Original", image)
cv2.waitKey(0)

shifted =imutils.translate(image,0,100)
cv2.imshow("Shifted Up and Left",shifted)

cv2.waitKey(0)

Rotation 旋转一个角度q的图像。

旋转rotate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np 
import argparse
import imutils
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",required =True,help ="Path to the image")
args =vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("Original",image)

(h,w) = image.shape[:2]
center = (w//2,h//2)

M = cv2.getRotationMatrix2D(center, 45 , 1.0)
#cv2.getRotationMatrix2D(center, degrees , scale)
#center为需要围绕旋转的点,当我们旋转图像时,我们需要指定我们要旋转的点。
#在大多数情况下,你会想要围绕图像的中心旋转;然而,
#OpenCV允许你指定你想旋转的任意点
# degrees 旋转的角度
# scale 比例 这里你可以指定一个浮点值,其中1.0意味着使用相同的图像转换。但是,如果您指定的值为2.0,则图像的大小将加倍。类似地,0.5的值将图像的大小减半。
#就像我们定义矩阵来翻译图像一样,我们也定义了一个矩阵来旋转图像。我们只需要调用cv2.getRotationMatrix2D方法,而不是使用NumPy手工构造矩阵
rotated = cv2.warpAffine(image, M,(w,h))
cv2.imshow("Rotated by 45 Degrees", rotated)


M = cv2.getRotationMatrix2D(center, -90,1.0)
rotated = cv2.warpAffine(image, M ,(w,h))
cv2.imshow("Rotated by -90 Degrees",rotated)


rotated = imutils.rotate(image,60,None,0.5)
cv2.imshow("Rotated by imutils",rotated)

cv2.waitKey(0)

封装rotate方法

工具类imutils.py
1
2
3
4
5
6
7
def rotate(image, angle ,center= None,scale = 1.0):
(h,w)= image.shape[:2]
if center is None:
center =(w /2,h/2)
M = cv2.getRotationMatrix2D(center,angle,scale)
rotated = cv2.warpAffine(image, M ,(w,h))
return rotated

调用方式:

1
rotated = imutils.rotate(image,60,None,0.5)

resize 调整大小

调整大小resize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import numpy as np 
import argparse
import imutils
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",required = True,help = "Path to the image")

args = vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("Original",image)

r = 150.0 /image.shape[1]
#定义新图片的宽度为150,为了计算新图片的高度,计算出新图片宽度和当前图片宽度的比例。
dim = (150,int(image.shape[0]*r))
#新图片的宽高
resized = cv2.resize(image , dim, interpolation = cv2.INTER_AREA)

#cv2.resize(image,dim,interpolation)
#image 需要调整的图片 dim 新图片的尺寸
#最后一个参数是我们的插值方法,它是在幕后处理实际图像大小调整的算法
#cv2.INTER_AREA,cv2.INTER_LINEAR,cv2.INTER_CUBIC,cv2.INTER_NEAREST
# interpolation 可选参数
# INTER_NEAREST - a nearest-neighbor interpolation
# INTER_LINEAR - a bilinear interpolation (used by default)
# 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_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood
# INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood
NTER_NEAREST - 最近邻居插值
INTER_LINEAR - 双线性插值(默认使用)
INTER_AREA - 使用像素区域关系重采样。这可能是图像抽取的首选方法,因为它可以产生无莫尔效应的结果。但是当图像放大时,它与INTER_NEAREST方法类似。
INTER_CUBIC - 4x4像素邻域上的双三次插值
INTER_LANCZOS4 - 8x8像素邻域上的Lanczos插值

cv2.imshow("resized(width)",resized)

cv2.waitKey(0)

Flipping 旋转

我们可以在x或y轴周围翻转图像,甚至可以翻转图像(We can flip an image around either the x or y axis, or even both.)

旋转flipping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",required = True,help ="Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("Original",image)

flipped = cv2.flip(image,1)
cv2.imshow("Flipped Horizontally",flipped)
#使用1的翻转代码值表示我们将水平地围绕y轴翻转图像。

flipped = cv2.flip(image,0)
# 指定一个0的翻转代码表示我们想要垂直翻转图像,围绕X轴
cv2.imshow("Flipped Vertically",flipped)


flipped = cv2.flip(image,-1)
# 使用负向翻转代码将图像翻转两个轴。
cv2.imshow("Flipped Horizontally&Vertically",flipped)

cv2.waitKey(0)

Cropping 裁剪

图片的裁剪使用NumPy数组切片来完成图像裁剪

裁剪cropping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import numpy as np 
import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",required =True, help="Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("Original",image)
#NumPy数组中高度在前面,宽度在后面
cropped = image[30:220 ,10:335]
#所以我们需要截取的区域值定义需要按照numpy的格式,如上[starty:endy,startx:endx]
# 1.Start y: The starting y coordinate. In this case, we
# start at y = 30.
# 2. End y: The ending y coordinate. We will end our crop
# at y = 220.
# 3. Start x: The starting x coordinate of the slice. We start
# the crop at x = 10.
# 4. End x: The ending x-axis coordinate of the slice. Our
# slice ends at x = 335.
cv2.imshow("update",cropped)
cv2.waitKey(0)

凡事往简单处想,往认真处行。
坚持原创技术分享,您的支持将鼓励我继续创作!