图像读取
传统读取像素和修改像素值
.
import cv2
img = cv2.imread("picture.bmp", cv2.IMREAD_UNCHANGED)
p = img[88, 142]
print(p)
cv2.imshow("Demo", img)
img[78, 125, 0] = 255
img[78, 125, 1] = 255
img[78, 125, 2] =255
print(img[78, 125])
img[100:150, 400:500] = [255, 255, 0]
k=cv2.waitKey(0)
if k==27:
cv2.destroyAllWindows()
cv2.imwrite("testyxz.jpg", img)
Numpy读取和修改像素
import cv2
import numpy
img = cv2.imread("test.jpg", cv2.IMREAD_UNCHANGED)
blue = img.item(78, 100, 0)
green = img.item(78, 100, 1)
red = img.item(78, 100, 2)
img.itemset((78, 100, 0), 100)
img.itemset((78, 100, 1), 100)
img.itemset((78, 100, 2), 100)
cv2.imshow("Demo", img)
k=cv2.waitKey(0)
if k==27:
cv2.destroyAllWindows()
获取图像属性
import cv2
import numpy
img = cv2.imread("test.jpg", cv2.IMREAD_UNCHANGED)
print(img.shape)
print(img.size)
print(img.dtype)
图像融合实验
import cv2
import numpy as np
img = cv2.imread("test.jpg", cv2.IMREAD_UNCHANGED)
face = np.ones((100, 100, 3))
face = img[200:300, 250:450]
img[300:400,400:600] = face
cv2.imshow("face", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/06286dc8349fb08a38f0c25d4cf3474e.png#pic_center)
通道分离合并
import cv2
import numpy as np
img = cv2.imread("test.jpg", cv2.IMREAD_UNCHANGED)
rows, cols, chn = img.shape
b, g, r = cv2.split(img)
cv2.imshow("B", b)
cv2.imshow("G", g)
cv2.imshow("R", r)
m = cv2.merge([b, g, r])
m_show = cv2.merge([r, g, b])
g1 = np.zeros((rows,cols),dtype=img.dtype)
r1 = np.zeros((rows,cols),dtype=img.dtype)
m = cv2.merge([b, g1, r1])
htich2 = np.hstack((img,m_show,m1))
cv2.imwrite('rs1.png',htich2)
cv2.waitKey(0)
cv2.destroyAllWindows()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f7a84577d247f8dd335511659a356649.png#pic_center)