修改图像中的一些数据,使图像发生一些变化
# @Software : Window10 + Python3.6 + PyCharm
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 h in range(channels):
pv = image[row, col, h]
image[row, col, h] = 255 - pv
cv.imshow("pixels_demo", image) # 生成一个修改后的图像
def create_image():
"""
# 多通道的情况
img = np.zeros([400, 400, 3], np.uint8)
img[: , : , 0] = np.ones([400, 400])*255
cv.imshow("new image", img)
# 单通道
# img = np.zeros([400, 400, 1], np.uint8) # 第一种zeros方法
# img[ : , : , 0] = np.ones([400,400],)*127
img = np.ones([400, 400, 1], np.uint8) # 第二种ones方法
img = img * 0
cv.imshow("new image", img)
# cv.imwrite("./2.png", 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("./1.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
t1 = cv.getTickCount()
# access_pixels(src)
create_image()
t2 = cv.getTickCount()
time = (t2-t1)/cv.getTickFrequency()
print("time: %s s" % time) # 计算运算时间
cv.waitKey(0)
cv.destroyAllWindows()
运行截图(部分):