show | version | enable_checker |
---|---|---|
step |
1.0 |
true |
- 上次了解了基础图像的构成
- 黑底
- 白底
- 黑底白块
- 白底黑块等
- 可以复制图像中的一部分像素矩阵
- 也可以将图像按照某种方式切开
- 就像切分九宫格那样
- 不过目前玩的都是灰度色彩模式
- 能否操作彩色图呢?
BGR彩色模式下图像的矩阵结构
import cv2
image = cv2.imread("/home/shiyanlou/monkey.png")
cv2.imshow("image",image)
print(image)
print(image.shape)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destoryAllWindows()
- 图像中的像素点细节
import cv2
image = cv2.imread("/home/shiyanlou/monkey.png")
cv2.imshow("image",image)
height, width,channels = image.shape
print(image)
print("type(image)====", type(image))
print("image.ndim====", image.ndim)
print("image.dtype====", image.dtype)
print("height====",height)
print("width====",width)
print("channel====",channels)
print("image.size====", image.size)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 注意shape中
- 先得到的是高度值
- 后得到的是宽度值
- 最后是通道数
- 可以得到具体的像素点信息吗?
import cv2
image = cv2.imread("/home/shiyanlou/monkey.png")
cv2.imshow("image",image)
height, width,channels = image.shape
print(image)
print("image[0,0]=======",image[0][0])
print("image[0,1]=======",image[0][1])
print("image[1,0]=======",image[1][0])
print("image[1,1]=======",image[1][1])
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 查看图像时
- 可以用手触控板捏合或放开这个图像位置
- 第一维度是纵坐标y
- 第二维度是横坐标x
- 颜色元组中的次序是BGR
- 可以做一个单一像素的图像吗?
import cv2
import numpy as np
one_pixel = np.zeros((1,1,3),np.uint8)
print("one_pixel=======",one_pixel)
print("one_pixel[0]====",one_pixel[0])
print("one_pixel[0][0]====",one_pixel[0][0])
cv2.imshow("one_pixel",one_pixel)
one_pixel[0,0] = 255
print("one_pixel=======",one_pixel)
print("one_pixel[0]====",one_pixel[0])
print("one_pixel[0][0]====",one_pixel[0][0])
cv2.imshow("one_pixel_2",one_pixel)
cv2.waitKey()
cv2.destroyAllWindows()
- 得到两个单像素图像
import cv2
import numpy as np
vector = np.zeros((3,1,3),np.uint8)
print("vector=======",vector)
print("vector[0]====",vector[0])
print("vector[0][0]====",vector[0][0])
cv2.imshow("vector",vector)
vector[0] = (255,0,0)
vector[1] = (0,255,0)
vector[2] = (0,0,255)
print("vector=======",vector)
print("vector[0]====",vector[0])
print("vector[0][0]====",vector[0][0])
cv2.imshow("vector_2",vector)
cv2.waitKey()
cv2.destroyAllWindows()
- 结果
- 可以设置一个2*2矩阵吗?
import cv2
import numpy as np
matrix = np.zeros((2,2,3),np.uint8)
print(matrix)
cv2.imshow("matrix",matrix)
for i in range(2):
for j in range(2):
matrix[0,1] = (255,0,0)
matrix[1,0] = (0,255,0)
matrix[1,1] = (0,0,255)
print(matrix)
cv2.imshow("matrix_2",matrix)
cv2.waitKey()
cv2.destroyAllWindows()
- 结果
import numpy as np
import cv2
array = np.random.randint(256, size=(200,200,3),dtype=np.uint8)
print(array)
cv2.imshow("image",array)
cv2.waitKey()
cv2.destroyAllWindows()
- 彩色雪花
- 可以对图像通道进行拆分和拼合吗?🤔
- 可以设置具体像素点的颜色值吗?
import cv2
image = cv2.imread("/home/shiyanlou/monkey.png")
cv2.imshow("before",image)
height, width,channels = image.shape
image[108,235] = (0,0,255)
cv2.imshow("after",image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 金箍棒上有一点红色
- 可以设置某个矩阵空间区域的颜色吗?
import cv2
image = cv2.imread("/home/shiyanlou/monkey.png")
cv2.imshow("before",image)
height, width,channels = image.shape
image[100:110,220:240] = (0,0,255)
cv2.imshow("after",image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 首先选定区域
- 然后设定颜色
- 忽略x
import cv2
image = cv2.imread("/home/shiyanlou/monkey.png")
cv2.imshow("before",image)
height, width,channels = image.shape
image[100:110,:] = (0,255,0)
cv2.imshow("after",image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
import cv2
image = cv2.imread("/home/shiyanlou/monkey.png")
cv2.imshow("before",image)
height, width,channels = image.shape
image[:,120:140] = (0,255,0)
cv2.imshow("after",image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 结果
import cv2
image = cv2.imread("/home/shiyanlou/monkey.png")
cv2.imshow("before",image)
height, width,channels = image.shape
image[100:,120:] = (255,255,0)
cv2.imshow("after",image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 结果
- 可以复制像素吗?
import cv2
image = cv2.imread("/home/shiyanlou/monkey.png")
cv2.imshow("before",image)
y2 = 16
x2 = 220
y1 = 55
x1 = 300
image[0:abs(y2-y1), 0:abs(x2-x1)] = image[y2:y1,x2:x1]
cv2.imshow("before",image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 注意左上角
- 可以多复制出来几个吗?
import cv2
image = cv2.imread("/home/shiyanlou/monkey.png")
cv2.imshow("before",image)
y2 = 16
x2 = 220
y1 = 55
x1 = 300
for y_num in range(3):
for x_num in range(2):
image[y_num*abs(y1-y2):(y_num+1)*abs(y2-y1), x_num*abs(x2-x1):(x_num+1)*abs(x2-x1)] = image[y2:y1,x2:x1]
cv2.imshow("before",image)
key = cv2.waitKey()
if key & 0xFF == ord("q"):
cv2.destroyAllWindows()
- 结果
- 尝试把sword.jpg
- 下面的高亮阴影去掉
- 这次我们研究了彩色图片的数据结构
- 三维矩阵
- 高度
- 宽度
- BGR三色元组
- 三维矩阵
- 可以自己定制某种特殊的图像
- 也可以复制现有图像的某个矩阵范围
- 可以对矩阵进行裁切操作吗?
- 可以直接设置高度和宽度吗?🤔
- 我们下次再说 👋