图像的读取、访问与修改
#生成二维数组模拟黑色图像
import cv2
import numpy as np
img = np.zeros((8,8),dtype = np.uint8)
print('img = \n',img)
cv2.imshow('one', img)
print('读取像素点 img[0,3] = ', img[0,3])
img[0,3] = 255
print('修改后 img = \n', img)
print('读取修改后像素点 img[0,3] = ', img[0,3])
cv2.imshow('two', img)
#读取lena灰度图
import cv2
img = cv2.imread('lena.bmp',0)
cv2.imshow('before', img)
for i in range(10,100):
for j in range(80,100):
img[i,j] = 255
cv2.imshow('after', img)
cv2.waitKey()
cv2.destoryAllWindows()
#分别建立BGR三通道图像
import numpy as np
import cv2
blue = np.zeros((300,300,3), dtype = np.uint8)
blue[:,:,0] = 255
print('blue = \n', blue)
cv2.imshow('blue', blue)
green = np.zeros((300,300,3), dtype = np.uint8)
green[:,:,1] = 255
print('green = \n', green)
cv2.imshow('green', green)
red = np.zeros((300,300,3), dtype = np.uint8)
red[:,:,2] = 255
print('red = \n', red)
cv2.imshow('red', red)
**需要注意的是,RGB模式的彩色图像在读入opencv内进行处理时,会按照行方向依次读取该RGB图像的B、G、R通道的像素点,并将像素点以行为单位存储在ndarray的列中,**示例如下:
#模拟彩色图像
import numpy as np
import cv2
img = np.zeros((2,4,3), dtype = np.uint8)
print("img = \n", img)
print('读取像素点img[0,3]=', img[0,3])
print('读取像素点img[1,2,2]=', img[1,2,2])
img[0,3] = 255
img[0,0] = [66,77,88]
img[1,1,1] = 3
img[1,2,2] = 4
img[0,2,0] = 5
print('修改后img = \n', img)
print('读取修改后像素点img[1,2,2]=', img[1,2,2])
运行该代码的输出结果是:
img =
[[[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]]]
读取像素点img[0,3]= [0 0 0]
读取像素点img[1,2,2]= 0
修改后img =
[[[ 66 77 88]
[ 0 0 0]
[ 5 0 0]
[255 255 255]]
[[ 0 0 0]
[ 0 3 0]
[ 0 0 4]
[ 0 0 0]]]
读取修改后像素点img[1,2,2]= 4
以上述修改后的img值为例,它的第0行为:
[[ 66 77 88]
[ 0 0 0]
[ 5 0 0]
[255 255 255]]
它的第1行为:
[[ 0 0 0]
[ 0 3 0]
[ 0 0 4]
[ 0 0 0]]
以第0行为基准,它的第0列为:
[ 66 77 88]
第1列为:
[ 0 0 0]
第2列为:
[ 5 0 0]
第3列为:
[255 255 255]
三通道是指每列中都有三个数值,分别对应三个通道,即为2行4列3通道的图像
因此不难理解img[1,2,2]的值为4
#访问修改彩图的像素值
import cv2
img = cv2.imread('lena512color.png')
cv2.imshow('before',img)
print('访问img[0,0]', img[0,0])
print('访问img[0,0,0]', img[0,0,0])
print('访问img[0,0,1]', img[0,0,1])
print('访问img[0,0,2]', img[0,0,2])
print('访问img[50,0]', img[50,0])
print('访问img[100,0]', img[100,0])
for i in range(0,50):
for j in range(0,100):
for k in range(0,3):
img[i,j,k] = 255 #白色
for i in range(50,100):
for j in range(0,100):
img[i,j] = [128,128,128] #灰色
for i in range(100,150):
for j in range(0,100):
img[i,j] = 0 #黑色
cv2.imshow('after', img)
cv2.waitKey()
cv2.destoryAllWindows()
#生成灰度图像,使用item()和itemset()进行像素点的访问和修改
import numpy as np
img = np.random.randint(10,99,size=(5,5), dtype = np.uint8)
print('img = \n',img)
print('读取像素点img.item(3,2)=', img.item(3,2))
img.itemset((3,2),255)
print('修改后img= \n', img)
print('修改后像素点img.item(3,2)=', img.item(3,2))
#读取lena灰度图,对其像素进行访问、修改
import cv2
img = cv2.imread('lena.bmp',0)
print('读取像素点img.item(3,2) = ', img.item(3,2))
img.itemset((3,2),255)
print('修改后像素点img.item(3,2)=', img.item(3,2))
cv2.imshow('before',img)
for i in range(10,100):
for j in range(80,100):
img.itemset((i,j),255)
cv2.imshow('after', img)
cv2.waitKey()
cv2.destoryAllWindows()
#生成彩色图像,使用item()和itemset()进行像素点的访问和修改
import numpy as np
img = np.random.randint(10,99,size=[2,4,3], dtype = np.uint8)
print('img = \n', img)
print('读取像素点img[1,2,0] = ', img.item(1,2,0))
print('读取像素点img[0,2,1] = ', img.item(0,2,1))
print('读取像素点img[1,0,2] = ', img.item(1,0,2))
img.itemset((1,2,0),255)
img.itemset((0,2,1),255)
img.itemset((1,0,2),255)
print('修改后img = \n', img)
print('修改后像素点img[1,2,0] = ', img.item(1,2,0))
print('修改后像素点img[0,2,1] = ', img.item(0,2,1))
print('修改后像素点img[1,0,2] = ', img.item(1,0,2))
#读取lena彩色图,对其像素进行访问、修改
import cv2
import numpy as np
img = cv2.imread('lena512color.png')
cv2.imshow('before', img)
print('读取像素点img[0,0,0] = ', img.item(0,0,0))
print('读取像素点img[0,0,1] = ', img.item(0,0,1))
print('读取像素点img[0,0,2] = ', img.item(0,0,2))
for i in range(0,50):
for j in range(0,100):
for k in range(0,3):
img.itemset((i,j,k),255)
cv2.imshow('after', img)
print('修改后像素点img[0,0,0] = ', img.item(0,0,0))
print('修改后像素点img[0,0,1] = ', img.item(0,0,1))
print('修改后像素点img[0,0,2] = ', img.item(0,0,2))
cv2.waitKey()
cv2.destoryAllWindows()
#获取ROI
import cv2
a = cv2.imread('lena512color.png', -1)
face = a[220:400,250:350]
cv2.imshow('original',a)
cv2.imshow('face',face)
cv2.waitKey()
cv2.destroyAllWindows()
#对lena的脸部进行打码
import cv2
import numpy as np
a = cv2.imread('lena512color.png',-1)
cv2.imshow('original',a)
face = np.random.randint(0,256,size=(180,100,3))
a[220:400,250:350] = face
cv2.imshow('result',a)
cv2.waitKey()
cv2.destroyAllWindows()
#将一幅图像的ROI复制到另一幅图像
import cv2
lena = cv2.imread('lena.bmp',-1)
dollar = cv2.imread('dollar.bmp',-1)
cv2.imshow('lena',lena)
cv2.imshow('dollar',dollar)
face = lena[220:400,250:350]
dollar[160:340,200:300] = face
cv2.imshow('result',dollar)
cv2.waitKey()
cv2.destroyAllWindows()
#通道拆分之索引拆分
import cv2
lena = cv2.imread('lena512color.png')
cv2.imshow('lena1',lena)
b = lena[:,:,0]
g = lena[:,:,1]
r = lena[:,:,2]
cv2.imshow('b',b)
cv2.imshow('g',g)
cv2.imshow('r',r)
lena[:,:,0] = 0
cv2.imshow('lenab0',lena)
lena[:,:,1] = 0
cv2.imshow('lenab0g0',lena)
cv2.waitKey()
cv2.destroyAllWindows()
#通道拆分之函数拆分
import cv2
lena = cv2.imread('lena512color.png')
b,g,r = cv2.split(lena)
cv2.imshow('B',b)
cv2.imshow('G',g)
cv2.imshow('R',r)
cv2.waitKey()
cv2.destroyAllWindows()
#通道合并
import cv2
lena = cv2.imread('lena512color.png')
b,g,r = cv2.split(lena)
bgr = cv2.merge([b,g,r])
rgb = cv2.merge([r,g,b])
cv2.imshow('lena',lena)
cv2.imshow('bgr',bgr)
cv2.imshow('rgb',rgb)
cv2.waitKey()
cv2.destroyAllWindows()
#获取图像属性
import cv2
gray = cv2.imread('lena.bmp',0)
color = cv2.imread('lena512color.png',1)
print('图像gray属性: ')
print('gray.shape=',gray.shape)
print('gray.size=',gray.size)
print('gray.dtype=',gray.dtype)
print('图像color属性: ')
print('color.shape=',color.shape)
print('color.size=',color.size)
print('color.dtype=',color.dtype)
参考书籍:opencv轻松入门–李立宗