代码
解释都在注释里啦
import cv2 as cv
import numpy as nm
#定义一张图片,修改颜色通道后输出
def change(img):#numpy数组操作
print(img.shape)
height=img.shape[0]
width=img.shape[1]
channel=img.shape[2]
print("height:%s,width:%s,channel:%s"%(height,width,channel))
for x in range(height):
for y in range(width):
for z in range(channel):
temp=img[x,y,z]
img[x,y,z]=255-temp
cv.namedWindow("After",1)
cv.imshow("After",img)
def testChange():
src = cv.imread("me.jpg")
cv.namedWindow("Before", cv.WINDOW_NORMAL)
cv.imshow("Before", src)
t1 = cv.getTickCount()
# 毫秒级别的计时函数,记录了系统启动以来的时间毫秒
change(src)
t2 = cv.getTickCount()
time = (t2 - t1)*1000/cv.getTickFrequency()
# getTickFrequency用于返回CPU的频率,就是每秒的计时周期数
print("time: %s" % time)
# 输出运行的时间
#定义多通道图片
def create1mul():
img=nm.zeros([300,300,3],nm.uint8)
#zeros:double类的全零矩阵,形成300*300的三个渠道的矩阵图像,参数classname为unit8
img[:,:,0]=nm.ones([300,300])*250
#将全一矩阵赋值给img的第一维度,并且乘以250
img[:,:,1]=nm.ones([300,300])*250
img[:,:,2]=nm.ones([300,300])*250
cv.namedWindow("Create1",1)
cv.imshow("Create1",img)
def create2mul():
img=nm.ones([300,300,3],nm.uint8)
img[:, :, 0] = img[:, :, 0]*250
img[:, :, 1] = img[:, :, 0]*250
img[:, :, 2] = img[:, :, 0]*250
cv.namedWindow("Create2", 1)
cv.imshow("Create2", img)
#定义单通道图片
def create3sin():
img=nm.ones([400,400,1],nm.uint8)
img=img*255
cv.namedWindow("Create3", 1)
cv.imshow("Create3", img)
#调用库函数实现像素取反
def reverse(img):
arr=cv.bitwise_not(img)
cv.namedWindow("reverse", 1)
cv.imshow("reverse", arr)
def teatReverse():
src = cv.imread("me.jpg")
cv.namedWindow("Before", cv.WINDOW_NORMAL)
cv.imshow("Before", src)
t1 = cv.getTickCount()
reverse(src)
t2 = cv.getTickCount()
time = (t2 - t1)*1000/cv.getTickFrequency()
print("time: %s" % time)
testChange()
create1mul()
create2mul()
create3sin()
teatReverse()
cv.waitKey(0)
cv.destroyAllWindows()