show | version | enable_checker |
---|---|---|
step |
1.0 |
true |
- 上次我们研究了彩色图片的数据结构
- 三维矩阵
- 高度
- 宽度
- BGR三色元组
- 三维矩阵
- 可以自己定制某种特殊的图像
- 也可以复制现有图像的某个矩阵范围
- 可以对矩阵进行裁切操作吗?
import cv2
import numpy as np
image = cv2.imread("/home/shiyanlou/monkey.png")
height,width,_ = image.shape
new_image = np.zeros((100,60,3),np.uint8)
new_image[:,:] = image[238:338,60:120]
new_image[62:,44:] = (133,211,246)
cv2.imshow("before",new_image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 裁剪结果
- 切割这个孙悟空有什么说法吗?
- 图像ROI
- Region Of Interest
- 感兴趣的区域
- 能够非常轻松的通过numpy的切片操作实现
- 可以切分九宫格吗?
- 图像切割通用代码
import cv2
import numpy as np
image = cv2.imread("/home/shiyanlou/monkey.png")
height,width,_ = image.shape
cv2.imshow("before",image)
x_num = 3
y_num = 3
small_height = int(height/y_num)
small_width = int(width/x_num)
for j in range(y_num):
for i in range(x_num):
new_image = np.zeros((small_height,small_width,3),np.uint8)
new_image[:] = image[j*small_height:(j+1)*small_height, i*small_width:(i+1)*small_width]
num = j * x_num + i
new_image_name = "i_" + str(num) + ".jpg"
cv2.imwrite(new_image_name, new_image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 切割结果
import cv2
import numpy as np
width = 400
height = 300
img = np.zeros ((height, width, 3), np.uint8)
cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()
blue = img.copy()
blue[:,:,0] = 255 #所有像素0号通道,都是255
green = img.copy()
green[:,:,1] = 255 #所有像素1号通道,都是255
red = img.copy()
red[:,:,2] = 255 #所有像素0号通道,都是255
cv2.imshow("blue",blue)
cv2.imshow("green",green)
cv2.imshow("red",red)
cv2.waitKey()
cv2.destroyAllWindows()
- 最开始的是黑色
- 后来在黑色基础上出现了蓝绿红色
- 可以把这些图像拼合起来吗?
import cv2
import numpy as np
width = 400
height = 300
img = np.zeros ((height, width, 3), np.uint8)
blue = img.copy()
blue[:,:,0] = 255 #所有像素0号通道,都是255
green = img.copy()
green[:,:,1] = 255 #所有像素1号通道,都是255
hs = np.hstack((blue,green))
cv2.imshow("hstack",hs)
cv2.waitKey()
cv2.destroyAllWindows()
- hstack
- 可以垂直拼合吗?
import cv2
import numpy as np
width = 400
height = 300
img = np.zeros ((height, width, 3), np.uint8)
blue = img.copy()
blue[:,:,0] = 255 #所有像素0号通道,都是255
green = img.copy()
green[:,:,1] = 255 #所有像素1号通道,都是255
vs = np.vstack((blue,green))
cv2.imshow("vstack",vs)
cv2.waitKey()
cv2.destroyAllWindows()
- vstack
- 可以先平行拼合
- 再垂直拼合吗?
import cv2
import numpy as np
width = 400
height = 300
black = np.zeros ((height, width, 3), np.uint8)
blue = black.copy()
blue[:,:,0] = 255 #所有像素0号通道,都是255
green = black.copy()
green[:,:,1] = 255 #所有像素1号通道,都是255
red = black.copy()
red[:,:,2] = 255 #所有像素0号通道,都是255
vs1 = np.hstack((blue,green))
vs2 = np.hstack((red,black))
hs = np.vstack((vs1,vs2))
cv2.imshow("hs",hs)
cv2.waitKey()
cv2.destroyAllWindows()
- 拼合结果
- 首先
- 我们研究了图像的裁剪
- 对于矩阵的裁剪就是对图像的裁剪
- 然后
- 我们切分了九宫格
- 最后
- 构成了纯色图像
- 并且对于纯色图像进行了
- 水平拼合
- 垂直拼合
- 纵横拼合
- 除了对于像素点位置的裁剪和拼合之外
- 可以对图像通道进行拆分和拼合吗?🤔
- 我们下次再说 👋