图像是由像素构成的
图像分类:
opencv中彩色图像的通道顺序是BGR
返回值 = 图像[位置参数]
p = img[88,142]
blue = img[18, 125, 0]
green = img[78, 125, 1]
red = img[78, 125, 2]
p = [78, 125] # 读取像素点[78, 125]三个通道的值
# 灰度图像
img[88, 99] = 255
# BGR图像
img[88, 99] = [255, 255, 255]
import cv2
gray = cv2.imread('image/gray.jpg', cv2.IMREAD_UNCHANGED)
print('gray[100, 100]:', gray[100, 100])
gray[100, 100] = 255
print('gray[100, 100]:', gray[100, 100])
color = cv2.imread('image/color.jpg', cv2.IMREAD_UNCHANGED)
print('color[100, 100]:', color[100, 100])
color[100, 100] = [255, 255, 255]
print('color[100, 100]:', color[100, 100])
cv2.imshow('color', color)
color[100:150, 100:150] = [255, 255, 255]
cv2.imshow('dst', color)
cv2.waitKey(0)
cv2.destroyAllWindows()
返回值 = 图像.item(位置参数)
pix = img.item(88, 142)
blue = img.item(78, 125, 0)
green = img.item(78, 125, 1)
red = img.item(78, 125, 2)
图像名.itemset(位置, 新值)
# 灰度图像
img.itemset((88, 99), 255)
# BGR图像
img.itemset((88, 99, 0), 255)
img.itemset((88, 99, 1), 255)
img.itemset((88, 99, 2), 255)
import cv2
import numpy as np
# 灰度图像
gray = cv2.imread('image/gray.jpg', cv2.IMREAD_UNCHANGED)
print('gray.item(100, 100):', gray.item(100, 100))
gray.itemset((100, 100), 255)
print('gray.item(100, 100):', gray.item(100, 100))
# BGR图像
color = cv2.imread('image/color.jpg', cv2.IMREAD_UNCHANGED)
print('color.item(100, 100, 0):', color.item(100, 100, 0))
color.itemset((100, 100, 0), 255)
print('color.item(100, 100, 0):', color.item(100, 100, 0))
print('color.item(100, 100, 1):', color.item(100, 100, 1))
color.itemset((100, 100, 1), 255)
print('color.item(100, 100, 1):', color.item(100, 100, 1))
print('color.item(100, 100, 2):', color.item(100, 100, 2))
color.itemset((100, 100, 2), 255)
print('color.item(100, 100, 2):', color.item(100, 100, 2))
shape可以获取图像的形状,返回包含行数,列数,通道数的元组
img.shape
size可以获取图像的像素数目
img.size
dtype可以获取图像的数据类型
img.dtype
import cv2
gray = cv2.imread('image/gray.jpg', cv2.IMREAD_UNCHANGED)
color = cv2.imread('image/color.jpg', cv2.IMREAD_UNCHANGED)
print('gary.shape:', gray.shape)
print('color.shape:', color.shape)
print('gray.size:', gray.size)
print('color.size:', color.size)
print('gray.dtype:', gray.dtype)
print('color.dtype:', color.dtype)
ROI(region of interest),感兴趣区域
从被处理的图像以方框、圆、椭圆、不规则多边形等方式勾勒出需要处理的区域
可以通过各种算子(Operator)和函数来求得感兴趣区域ROI,并进行图像的下一步处理
img[200:400, 200:400]
import cv2
import numpy as np
color = cv2.imread('image/color.jpg') # 读取一张图像
roi = color[50:200, 800:950] # 获取ROI区域像素值
cv2.imshow('roi', roi)
color[0:150, 0:150] = roi # 改变原图像像素值
cv2.imshow('color', color)
test = cv2.imread('image/test.jpg') # 读取另外一张图像
test[0:150, 0:150] = roi # 改变图像像素值
cv2.imshow('test',test)
cv2.waitKey(0)
cv2.destroyAllWindows()
b = img[:, :, 0]
g = img[:, :, 1]
r = img[:, :, 2]
b,g,r = cv2.split(img)
b = cv2.split(img)[0]
g = cv2.split(img)[1]
r = cv2.split(img)[2]
img = cv2.merge([b, g, r])
import cv2
import numpy as np
color = cv2.imread('image/color.jpg')
b, g, r = cv2.split(color) # 通道拆分
cv2.imshow('color', color)
cv2.imshow('b', b)
cv2.imshow('g', g)
cv2.imshow('r', r)
m = cv2.merge([b, g, r]) # 通道合并
cv2.imshow('merge', m)
weight, high = color.shape[0:2]
b = cv2.split(color)[0]
g = np.zeros((weight, high), color.dtype)
r = np.zeros((weight, high), color.dtype)
m = cv2.merge([b, g, r])
cv2.imshow('meger1',m)
cv2.waitKey(0)
cv2.destroyAllWindows()