OpenCV图像的八种变换

1. 图像放大、缩小

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

img = cv2.imread("images/1.jpg")
#img2 = cv2.imread("images/3.jpg")
#b,g,r = cv2.split(img2)
#image_rgb = cv2.merge((r,g,b))
#plt.imshow(image_rgb)

b, g ,r = cv2.split(img)
img_1 = cv2.merge((r,g,b))
plt.imshow(img_1)


# height, width, channel
height, width, channel = img.shape
print(height, width, channel)

1.1 放大

# cv2.resize() 放大
resized_img = cv2.resize(img, (width*2, height*2), interpolation=cv2.INTER_LINEAR)
plt.imshow(resized_img)

1.2 缩小

# cv2.resize() 缩小
small_img = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_LINEAR)
plt.imshow(small_img)

2. 图像平移

2.1 向右下平移

# 1 平移
height, width = img.shape[:2]
M1 = np.float32([[1,0,100],[0,1,50]])# 平移矩阵, 100 图像向右移动100个像素,向下移动50个像素
move_img = cv2.warpAffine(img, M1,(width, height))
plt.imshow(move_img)

2.1 向左上平移

M2 = np.float32([[1,0,-100],[0,1,-50]])# 平移矩阵, 100 图像向左移动100个像素,向上移动50个像素
move_img2 = cv2.warpAffine(img, M2,(width, height))
plt.imshow(move_img2)

3. 图像旋转

height, width = img.shape[:2]
center = (width // 2.0, height // 2.0)#旋转的中心
M3 = cv2.getRotationMatrix2D(center, 180, 1)# 1 表示旋转过程中没有缩放
rotation_img = cv2.warpAffine(img, M3, (width, height))
plt.imshow(rotation_img)

4. 图像仿射变换

# cv2.getAffineTransform(p1, p2)
p1 = np.float32([[120,35],[215,45],[135,120]])
p2 = np.float32([[135,45],[300,110],[130,230]])
M4 = cv2.getAffineTransform(p1, p2)# 计算一个变换矩阵
trans_img = cv2.warpAffine(img, M4, (width, height))
plt.imshow(trans_img)

5. 图像裁剪

crop_img = img[100:400, 100:400]
plt.imshow(crop_img)

6. 位运算(AND,OR,XOR)

通过画布画一个长方形

# 长方形
rectangle = np.zeros((300,300), dtype='uint8')
rect_img = cv2.rectangle(rectangle,(25, 25), (275,275), 255, -1)
plt.imshow(rect_img)

再画一个圆

rectangle = np.zeros((300,300),dtype="uint8")
circle_img = cv2.circle(rectangle,(150,150),150,255, -1 )
plt.imshow(circle_img)

6.1AND(与运算)

# 与运算:cv2.bitwise_and(), 00:0, 01:0, 10:0, 11:1
and_img = cv2.bitwise_and(rect_img, circle_img)
plt.imshow(and_img)

6.2 OR(或运算)

# OR 或运算, 01:1, 10:1, 00:0, 11:1
or_img = cv2.bitwise_or(rect_img, circle_img)
plt.imshow(or_img)```

## 6.3XOR (异或运算)

```python
#XOR 异或运算, 01:1,10:1, 00:0, 11:0
xor_img = cv2.bitwise_xor(rect_img, circle_img)
plt.imshow(xor_img)

7. 图像的分离和融合

7.1 分离

(B, G, R) = cv2.split(img) #分离
plt.imshow(B)
plt.imshow(G)
plt.imshow(R)

7.2 融合

# 2 融合
zeros = np.zeros(img.shape[:2],dtype="uint8")
plt.imshow(cv2.merge([zeros, zeros, R]))
plt.imshow(cv2.merge([B, zeros,zeros]))
plt.imshow(cv2.merge([zeros, G,zeros]))

8. 颜色空间

8.1 灰度

# 灰度
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.imshow(gray)

8.2 HSV(色度、饱和度、纯度)

#HSV(色度、饱和度、纯度)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
plt.imshow(hsv)

8.1 lab

# lab
lab =cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
plt.imshow(lab)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值