1, 图像的基本操作
1.1 arr.item((x,y,channel))
获取值
1.2 arr.itemset((x,y.channel), val)
赋值
1.3 arr.shape
获取图像的形状, 返回值是行, 列, 颜色通道的元组
1.4 img.size
获取图像的像素点个数
1.5 img.dtype
图像的数据类型
# coding:utf-8
import cv2
import numpy as np
def show(img):
# cv2.namedWindow('aa', cv2.WINDOW_NORMAL)
cv2.imshow('aa', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('./001_720x1080.jpg', cv2.IMREAD_UNCHANGED)
print(img[200, 200])
img[100,100] = [255, 255, 255]
arr = img.item((100, 100, 2))
img.itemset((100, 100, 2), 0)
print(img.shape)
print(img.size)
print(img.dtype)
show(img)
# [197 188 185]
# (1080, 720, 3)
# 2332800
# uint8
2, 图像ROI
# coding:utf-8
import cv2
import numpy as np
def show(img):
# cv2.namedWindow('aa', cv2.WINDOW_NORMAL)
cv2.imshow('aa', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('./001_720x1080.jpg', cv2.IMREAD_UNCHANGED)
leaf = img[200:400, 500:700]
img[400:600, 200:400] = leaf
show(img)
3, 拆封及合并图像通道
3.1 cv2.split(img)
把图像拆分通道
3.2 cv2.merge( (b,g,r))
合并通道
# coding:utf-8
import cv2
import numpy as np
def show(img):
# cv2.namedWindow('aa', cv2.WINDOW_NORMAL)
cv2.imshow('aa', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('./001_720x1080.jpg', cv2.IMREAD_UNCHANGED)
b,g,r = cv2.split(img)
# or
# b = img[:,:,0]
# g = img[:,:,1]
# r = img[:,:,2]
out = cv2.merge((b,g,r))
# 把囧红色通道变为0
out[:,:,2] = 0
show(out)
相对于索引, split 是比较耗时的操作
4, 为图像扩边(卷积边界)
copyMakeBorder(src, top, bottom, left, right, borderType, dst=None, value=None)
为图片添加外边框(margin)
参数 | 含义 |
---|---|
src | 图片 |
top | 上边距 |
bottom | 下边距 |
left | 左边距 |
right | 右边距 |
borderType | 外边框的类型 |
dst | |
value | 填充颜色, 仅当cv2.BORDER_CONSTANT有效 |
borderType
类型 | 含义 |
---|---|
BORDER_CONSTANT | 常量, 我们添加一个固定的颜色 |
BORDER_DEFAULT | |
BORDER_ISOLATED | |
BORDER_REFLECT | |
BORDER_REFLECT101 | |
BORDER_REFLECT_101 | |
BORDER_REPLICATE | |
BORDER_TRANSPARENT | |
BORDER_WRAP |
# coding:utf-8
import cv2
import numpy as np
def show(img):
cv2.namedWindow('aa', cv2.WINDOW_NORMAL)
cv2.imshow('aa', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
print([i for i in dir(cv2) if 'BORDER' in i])
img = cv2.imread('./001_720x1080.jpg', cv2.IMREAD_UNCHANGED)
out = cv2.copyMakeBorder(img, 100, 100, 100, 100, cv2.BORDER_WRAP, value=[255, 0, 0])
show(out)
5, 图像的算数运算
5.1 cv2.add() 和 np的加法
add(src1, src2, dst=None, mask=None, dtype=None)
参数 | 含义 |
---|---|
src1 | 图片1 |
src2 | 图片2 |
dst | |
mask | 8位的单通道, 有点类似之前讲解的遮罩层的概念 |
dtype | 类型 |
图像操作上用cv2.add比较好, 对于2张图片要求图片的宽和高要一样
二者区别:
# coding:utf-8
import cv2
import numpy as np
x = np.uint8([250])
y = np.uint8([10])
print(cv2.add(x,y)) # 250 + 10 > 255 --> 255
print(x + y) # 250 + 10 > 255 --> 260 % 255 = 4
图片demo:
# coding:utf-8
import cv2
import numpy as np
def show(img):
cv2.namedWindow('aa', cv2.WINDOW_NORMAL)
cv2.imshow('aa', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
print([i for i in dir(cv2) if 'BORDER' in i])
mask = np.zeros((1080, 720, 1), np.uint8)
mask.fill(255)
black = np.zeros((300, 300, 1), np.uint8)
mask[500:800, 300:600] = black
img = cv2.imread('./001_720x1080.jpg', cv2.IMREAD_UNCHANGED)
img1 = cv2.imread('./while_720x1080.jpg', cv2.IMREAD_UNCHANGED)
out = cv2.add(img, img, mask=mask)
show(out)
5.2 addWeighted(src1, alpha, src2, beta, gamma, dst=None, dtype=None)
参数 | 含义 |
---|---|
src1 | 图像1 |
alpha | 透明度? |
src2 | 图像2 |
beta | |
gamma | 伽马 |
dst | |
dtype |
带有权重的图片融合, 感觉就像2张都有透明度
# coding:utf-8
import cv2
import numpy as np
def show(img):
cv2.namedWindow('aa', cv2.WINDOW_NORMAL)
cv2.imshow('aa', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
print([i for i in dir(cv2) if 'BORDER' in i])
# mask = np.zeros((1080, 720, 1), np.uint8)
# mask.fill(255)
# black = np.zeros((300, 300, 1), np.uint8)
#
# mask[500:800, 300:600] = black
img = cv2.imread('./001_720x1080.jpg', cv2.IMREAD_UNCHANGED)
img1 = cv2.imread('./while_720x1080.jpg', cv2.IMREAD_UNCHANGED)
out = cv2.addWeighted(img, 0.7,img1,0.3, 0)
show(out)
5.3 bitwise_and, bitwise_not, bitwise_or, bitwise_xor
其实就是对图像的每一个像素二进制运算
# coding:utf-8
import cv2
import numpy as np
def show(winname, img):
# cv2.namedWindow(winname, cv2.WINDOW_NORMAL)
cv2.imshow(winname, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('./001_720x1080.jpg', cv2.IMREAD_UNCHANGED)
img1 = np.zeros((1080, 720, 3), np.uint8)
img1.fill(255)
cv2.circle(img1, (360, 360), 180, (0, 0, 0), -1)
and1 = cv2.bitwise_and(img, img1)
or1 = cv2.bitwise_or(img, img1)
not1 = cv2.bitwise_not(img)
xor1 = cv2.bitwise_xor(img, img1)
and2 = cv2.bitwise_and(img1, img)
or2 = cv2.bitwise_or(img1, img)
xor2 = cv2.bitwise_xor(img1, img)
show('and1',and1)
show('or1',or1)
show('not1',not1)
show('xor1',xor1)
show('and1',and2)
show('or1',or2)
show('xor1',xor2)