代码
import cv2 as cv
import numpy as np
def access_pixels(image):
print(image.shape)
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
print("width: %s, height: %s, channels: %s"%(width, height, channels))
new_image = image.copy()
for row in range(height):
for col in range(width):
for c in range(channels):
pv = image[row, col, c]
new_image[row, col, c] = 256 - pv
cv.imshow('pixels_demo', new_image)
def create_image():
"""创建三维数组,0维为B,1维为G,2维为R"""
img = np.zeros([400, 400, 3], np.uint8)
img[:, :, 0] = np.ones([400, 400]) * 255
img2 = np.zeros([400,400,1],np.uint8)
img2[:, :, 0] = np.ones([400,400]) * 256
cv.imshow("new_image", img)
cv.imshow("new_image2",img2)
def inverse(image):
dst = cv.bitwise_not(image)
cv.imshow("inverse_demo",dst)
if __name__ == '__main__':
src = cv.imread("E:/jupyter-notebook/cat.jpg")
cv.namedWindow("Crystal Liu", cv.WINDOW_AUTOSIZE)
cv.imshow("Crystal Liu", src)
create_image()
t1 = cv.getTickCount()
access_pixels(src)
inverse(src)
t2 = cv.getTickCount()
time = (t2 - t1)/cv.getTickFrequency()
print("time: %s ms"%(time * 1000))
cv.waitKey(0)