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("height : %s, width : %s, channels : %s"%(height, width, channels))
for row in range(height):
for col in range(width):
for c in range(channels):
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 create_image():
img = np.zeros([400, 400, 3], np.uint8)
img[:, :, 0] = np.ones([400, 400])*255
img[:, :, 2] = np.ones([400, 400])*255
cv.imshow("New Image", img)
img = np.ones([400, 400, 1], np.uint8)
img = img * 127
cv.imshow("new image", img)
m1 = np.ones([3, 3], np.float32)
m1.fill(122.388)
print(m1)
m2 = m1.reshape([1, 9])
print(m2)
print("--------- Hello Python ---------")
src = cv.imread("E:/Users/tu.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
t1 = cv.getTickCount()
create_image()
t2 = cv.getTickCount()
time = (t2-t1)/cv.getTickFrequency()
print("time = %s ms"%(time * 1000))
cv.waitKey(0)
cv.destroyAllWindows()
注意print的格式化输出:
print("height : %s, width : %s, channels : %s"%(height, width, channels))
利用numpy可以创建某些特定数组,并且在创建时要注意其类型,以免在赋值时超出范围:
img = np.zeros([400, 400, 3], np.uint8)
img[:, :, 0] = np.ones([400, 400])*255
reshape()可以对可以改变数组维数,但不能改变数组大小,如23矩阵可以改成16,但不能改成1*5
cv.getTickCount()返回从操作系统启动到当前所经的计时周期数
cv.getTickFrequency()返回每秒的计时周期数
因此,计时周期数之差/每秒的计时周期数 = 时间(因以ms为计算频率,所以要乘上1000)
对像素取反也可用bitwise,并且速度非常快
def inverse(image):
dst = cv.bitwise_not(image)
cv.imshow("inverse demo", dst)