show | version | enable_checker |
---|---|---|
step |
1.0 |
true |
- 上次研究了 numpy.ndarray的
- 索引和切片
- 基础索引和切片
- 和 列表一致
- 多维索引和切片
- 在大括号中实现
- 不同维度 通过逗号划分
import numpy as np
import cv2
array = np.zeros((50,50),np.uint8)
print(array)
cv2.imshow("image",array)
cv2.waitKey()
cv2.destroyAllWindows()
- 结果
- 确实是50 * 50的纯黑图
- 可以创建非纯0的图吗?
import numpy as np
import cv2
array = np.ones((50,50),np.uint8)
print(array)
cv2.imshow("image",array)
cv2.waitKey()
cv2.destroyAllWindows()
- 结果
- 看起来还是纯黑
- 但是具体数值为1
- 可以创建纯白的图吗?
import numpy as np
import cv2
array = np.ones((200,100),np.uint8)
array[:,:] = 255
print(array)
cv2.imshow("image",array)
cv2.waitKey()
cv2.destroyAllWindows()
- 纯白的颜色是255
- 可以黑中带白吗?
import cv2
import numpy as np
width = 400
height = 300
img = np.zeros((height, width), np.uint8)
img[100:200,250:300] = 255 #纵坐标范围100,200,横坐标范围250,300
cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()
- 绘画结果
- 分析
- 可以反过来吗?
- 在 白色画布上绘制黑色
import cv2
import numpy as np
width = 400
height = 300
img = np.zeros ((height, width), np.uint8)
for i in range(0,width,40):
img[ : , i:(i + 20)] = 255# 纵坐标缺省就是从顶到底
cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()
- 结果
- 看起来似乎可以做个钢琴卷帘
- 图像的像素矩阵可以复制吗?
- 使用索引
- 控制像素点的颜色
import cv2
image = cv2.imread("/home/shiyanlou/gear.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow("original",image)
height, width = image.shape
print(image)
image[6,25] = 0
cv2.imshow("black pixel",image)
image[6,25] = 255
cv2.imshow("white pixel",image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 注意观察
- (y=6,x=25)点处的颜色
- 可以使用矩阵批量设置颜色吗?
import cv2
image = cv2.imread("/home/shiyanlou/gear.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow("original",image)
height, width = image.shape
print(image)
image[38:75,184:231] = 0
cv2.imshow("black rectangle",image)
image[38:75,184:231] = 128
cv2.imshow("gray rectangle",image)
image[38:75,184:231] = 255
cv2.imshow("white rectangle",image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 在矩阵范围内的像素
- 统一被设置为一个具体颜色
- 如果忽略起点或者终点会如何呢?
import cv2
image = cv2.imread("/home/shiyanlou/gear.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow("original",image)
height, width = image.shape
print(image)
image[:,184:231] = 255
cv2.imshow("omit x",image)
image[38:75,:] = 255
cv2.imshow("omit y",image)
image[:,:] = 255
cv2.imshow("omit x and y",image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 结果
import cv2
image = cv2.imread("/home/shiyanlou/gear.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow("original",image)
height, width = image.shape
print(image)
image[38:204,300:] = 255
cv2.imshow("omit x max",image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 结果
- x相当于从300到最大
- 忽略y的最小值又会如何呢?
import cv2
image = cv2.imread("/home/shiyanlou/gear.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow("original",image)
height, width = image.shape
print(image)
image[:80,150:300] = 255
cv2.imshow("omit y min",image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 结果
- y相当于从0到80
- gear.jpg
- 应该已经存在于/home/shiyanlou
import cv2
import numpy as np
image = cv2.imread("/home/shiyanlou/gear.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow("original",image)
height, width = image.shape
print(image)
y2 = 120
y1 = 84
x2 = 260
x1 = 180
width = x2 - x1
height = y2 - y1
img_dest = np.zeros((height, width), np.uint8)
img_dest[:,:] = image[y1:y2,x1:x2]
cv2.imshow("copy rectangle selection",img_dest )
cv2.imwrite("new_image.jpg",img_dest)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 结果
import cv2
image = cv2.imread("/home/shiyanlou/gear.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow("original",image)
height, width = image.shape
print(image)
y2 = 187
y1 = 84
x2 = 300
x1 = 150
image[0:(y2-y1),0:(x2-x1)] = image[y1:y2,x1:x2]
cv2.imshow("copy rectangle selection",image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 完成复制矩阵区域像素
import cv2
image = cv2.imread("/home/shiyanlou/gear.jpg",cv2.IMREAD_GRAYSCALE)
cv2.imshow("original",image)
height, width = image.shape
print(image)
y2 = 120
y1 = 84
x2 = 260
x1 = 180
for y_num in range(2):
for x_num in range(3):
print(y_num,x_num)
print((y_num+1)*(y2-y1))
print((x_num+1)*(x2-x1))
image[y_num * (y2-y1):(y_num+1)*(y2-y1),x_num*(x2-x1):(x_num+1)*(x2-x1)] = image[y1:y2,x1:x2]
cv2.imshow("copy rectangle selection",image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 复制了一个2 * 3矩阵的像素内容
- 我可以把合成图拆成一帧帧的图像吗?
- 上图horse.png应该在~
- 如果没有可以刷新蓝桥环境
- 将4 * 4 的跑马图
- 都变成同一张的图像
import cv2
image = cv2.imread("/home/shiyanlou/horse.png",cv2.IMREAD_GRAYSCALE)
cv2.imshow("original",image)
height, width = image.shape
y2 = int(height / 4)
y1 = 0
x2 = int(width / 4)
x1 = 0
for y_num in range(4):
for x_num in range(4):
print(y_num,x_num)
print((y_num+1)*(y2-y1))
print((x_num+1)*(x2-x1))
image[y_num * (y2-y1):(y_num+1)*(y2-y1),x_num*(x2-x1):(x_num+1)*(x2-x1)] = image[y1:y2,x1:x2]
cv2.imshow("copy rectangle selection",image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 结果
- 能否把这些图像像素复制到新图像上呢?
import cv2
import numpy as np
image = cv2.imread("/home/shiyanlou/horse.png",cv2.IMREAD_GRAYSCALE)
cv2.imshow("original",image)
height, width = image.shape
y2 = int(height / 4)
y1 = 0
x2 = int(width / 4)
x1 = 0
for y_num in range(4):
for x_num in range(4):
print(y_num,x_num)
print((y_num+1)*(y2-y1))
print((x_num+1)*(x2-x1))
num = y_num * 4 + x_num
new_image = np.zeros((y2-y1,x2-x1),np.uint8)
new_image[:] = image[y_num * (y2-y1):(y_num+1)*(y2-y1),x_num*(x2-x1):(x_num+1)*(x2-x1)]
new_image_name = "horse_" + str(num) + ".jpg"
cv2.imwrite(new_image_name,new_image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 进入Code文件夹运行文件
- 成功完成图片切分
- 总结一下吧
- 这次了解了基础图像的构成
- 黑底
- 白底
- 黑底白块
- 白底黑块等
- 可以复制图像中的一部分像素矩阵
- 也可以将图像按照某种方式切开
- 就像切分九宫格那样
- 不过目前玩的都是灰度色彩模式
- 能否操作彩色图呢?
- 我们下次再说 👋