Opencv实现对图片的处理和修改
目录
灰度图
读取灰度图
- imread('地址',cv2.IMREAD_GRAYSCALE)
灰度图 - imread('地址',0)
灰度图 - imwrite('地址',图片变量)
图片保存
代码展示:
python
b = cv2.imread('at1.png',cv2.IMREAD_GRAYSCALE)
cv2.imshow('gry',b)
w = cv2.waitKey(100000)
c= cv2.imread('at1.png',0)
cv2.imshow('gry_0',c)
cv2.waitKey(100000)
cv2.imwrite('at1_gry.png',b)
运行结果:
转换灰度图
- cv2.CvtColor(图像变量,cv2.COLOR_BGR2GRAY)
灰度图
代码展示:
python
a = cv2.imread('at1.png')
cv2.imshow('t1',a)
cv2.waitKey(100000)
b = cv2.cvtColor(a,cv2.COLOR_BGR2GRAY)
cv2.imshow('t2',b)
cv2.waitKey(100000)
运行结果:
RBG图
单通道图
单通道图不表现颜色,表现为灰色
方法一
- 图片变量名[:,:,n]
n值为0、1、2,分别是B、G、R单通道图
代码展示:
python
a = cv2.imread('at1.png')
a_b = a[:,:,0]
cv2.imshow('a_b',a_b)
cv2.waitKey(100000)
a_g = a[:,:,1]
cv2.imshow('a_g',a_g)
cv2.waitKey(100000)
a_r = a[:,:,2]
cv2.imshow('a_r',a_r)
cv2.waitKey(100000)
运行结果:
方法二
b,g,r = 图像变量.split()
代码展示:
python
a = cv2.imread('at1.png')
b,g,r = cv2.split(a)
cv2.imshow('r',r)
cv2.waitKey(10000)
运行结果:
单通道图显色
图片变量名[:,:,n] = 0
运行代码:
python
a = cv2.imread('at1.png')
a_new = a.copy()
a_new[:,:,2] = 0
cv2.imshow('a_new',a_new)
cv2.waitKey(10000)
运行结果:
合并单通道图
merge((b,g,r))
代码展示:
python
a = cv2.imread('at1.png')
b,g,r = cv2.split(a)
cv2.imshow('r',r)
cv2.waitKey(10000)
m = cv2.merge((b,g,r))
cv2.imshow('b_g_r',m)
cv2.waitKey(10000)
运行结果:
图片截取
a[m:n,x:y]高,宽
截取部分图像
代码展示:
python
a = cv2.imread('at1.png')
cv2.imshow('c',a)
cv2.waitKey(10000)
b = a[100:300,100:300]
cv2.imshow('a_m',b)
cv2.waitKey(10000)
运行结果:
图片打码
需要导入numpy
- a[m:n,x:y] = np.random.randint(0,256,(m-n,x-y,3))
-
0-256,256不取,实为0-255,
代码展示:
python
a = cv2.imread('at1.png')
a[100:200,100:200] = np.random.randint(0,256,(100,100,3))
cv2.imshow('c',a)
cv2.waitKey(10000)
运行结果:
图片组合
- b[m:n,x:y] = a[m:n,x:y]
a在b中,大小要一致
代码展示:
python
a = cv2.imread('at1.png')
b = cv2.imread('at1_gry.png')
b[100:200,100:300] = a[100:200,100:300]
cv2.imshow('a_b',b)
cv2.waitKey(10000)
运行结果:
缩放
格式1
- resize(a,dsize=None,fx=m,fy=n)
fx,fy表示等比如0.5对宽高缩放
格式2
cv2.resize(a,(m,n))
图像大小为m宽,n高,
代码展示:
python
a = cv2.imread('at1.png')
cv2.imshow('a',a)
cv2.waitKey(10000)
a_small = cv2.resize(a,dsize=None,fx=0.5,fy=0.5)
cv2.imshow('a_small',a_small)
cv2.waitKey(10000)
b_small = cv2.resize(a,(250,300))
cv2.imshow('b_small',b_small)
cv2.waitKey(10000)
cv2.destroyAllWindows()
运行结果:
图像运算
图像大小都要一致
+
图像+m
m为数组,当像素值大于255时,该值-256为最终值
a[m:n,x:y]+b[m1:n1,x1:y1]
间隔大小要相同,当像素值大于255时,该值-256为最终值
代码展示:
python
a = cv2.imread('at1.png')
b = cv2.imread('bt1.jpg')
c = a + 10
cv2.imshow('a',a)
cv2.waitKey(10000)
cv2.imshow('b',b)
cv2.waitKey(10000)
cv2.imshow('a+10',c)
cv2.waitKey(10000)
d = a[100:300,100:300] + b[100:300,100:300]
cv2.imshow('a+b',d)
cv2.waitKey(10000)
运行结果:
add
add(a,b)
a,b大小要相同
当a,b的像素值相加,像素值大于255时,该值为255
代码展示:
python
a = cv2.imread('at1.png')
b = cv2.imread('bt1.jpg')
a = cv2.resize(a,(400,400))
b = cv2.resize(b,(400,400))
e = cv2.add(a,b)
cv2.imshow('a_add_b',e)
cv2.waitKey(10000)
运行结果:
加权运算
addWeighted(a,占比,b,占比,0)
a,b大小相同,按占比显示
代码展示:
python
a = cv2.imread('at1.png')
b = cv2.imread('bt1.jpg')
a = cv2.resize(a,(400,400))
b = cv2.resize(b,(400,400))
f = cv2.addWeighted(a,0.5,b,0.5,0)
cv2.imshow('a_addweighted_b',f)
cv2.waitKey(10000)
cv2.destroyAllWindows()
运行结果: