#File Name : 第二课.py
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 i in range(height):
for k in range(width):
for j in range(channels):
cv1 = image[i,k,j]
# 获取每个点的像素值
# 改变每个像素的点值
image[i,k,j] = 255-cv1
cv.imshow('new',image)
def creat_image():
#创建一幅图像
img = np.zeros([400,400,3],np.uint8)
img[:,:,2] = np.ones([400,400])*255
#0 蓝 1 绿 2 红
cv.imshow('new1',img)
# cv.imwrite('路径')
#creat_image()
def creat1_image():
m1 = np.ones([3,3],np.uint8)
#opencv 用的就是 uint8
m1.fill(11222.88)
# 输出214 因为溢出了
#cv.convertScaleAbs()
print(m1)
m2 = m1.reshape([1,9])
print(m2)
#creat1_image()
def inverse(image):
dst = cv.bitwise_not(image)
#bitwise_not是对二进制数据进行“非”操作,
# 即对图像(灰度图像或彩色图像均可)
# 每个像素值进行二进制“非”操作,~1=0,~0=1
# 与255-image 效果相同,但时间更短
cv.imshow('inverse',dst)
src = cv.imread('C:/Users/Administrator/Desktop/ps/11.jpg')
inverse(src)
cv.namedWindow('input image',cv.WINDOW_AUTOSIZE)
#cv.imshow('input image',src)
t1 = cv.getTickCount()
#access_pixels(src)
t2 = cv.getTickCount()
times = (t2-t1)/cv.getTickFrequency()
#getTickCount():用于返回从操作系统启动到当前所经的计时周期数,
# 看名字也很好理解,get Tick Count(s)。
#getTickFrequency():用于返回CPU的频率。get Tick Frequency。
# 这里的单位是秒,也就是一秒内重复的次数。
# #所以剩下的就很清晰了:
#总次数/一秒内重复的次数 = 时间(s)
#1000 *总次数/一秒内重复的次数= 时间(ms)
#print('times : %f ms'%(times*1000))
cv.waitKey(0)
cv.destroyAllWindows()
m3 = np.array([[2,3,4],[4,5,6],[7,8,9]])
m3.fill(9)
print(m3)