from cv2 import cv2 as cv
import numpy as np
def access_pixels(image): #属性读取
print(image.shape)
height = image.shape[0]
width = image.shape[1]
chanmels = image.shape[2]
print('width:%s,height:%s,chanmels:%s'%(width,height,chanmels))
#每个像素点进行处理 # 应该是像素反转
for row in range(height):
for col in range(width):
for c in range(chanmels):
pv = image[row,col,c]
image[row,col,c] = 255 - pv
cv.imshow('pixels_demo',image)
# 利用内置的模块反转像素
def inverse(image):
dst = cv.bitwise_not(image)
cv.imshow('inverse demo',dst)
# 创建图
def creat_image():
'''
# 多通道
img = np.zeros([400,400,3],np.uint8)
img[:,:,0] = np.ones([400,400])*255 # blue green red #得到的是一张蓝色的图
cv.imshow('new image',img)
'''
# 单通道
img = np.ones([400,400,1],np.uint8) # 全部初始化为1
img = img * 0 # *0 为黑色 *127为灰色 *255为白色
cv.imshow('new image',img)
filepath = "C:\\pictures\\0.jpg"
img = cv.imread(filepath) # blue green red
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image",img)
# t1 和 t2是用来测试时间的
t1 = cv.getTickCount()
# creat_image()
# access_pixels(img)
inverse(img)
t2 = cv.getTickCount()
time = (t2-t1)/cv.getTickFrequency()
print('time:%s ms'%(time*1000))
cv.waitKey(0)
cv.destroyAllWindows()