先上代码
1 importcv2 as cv2 importnumpy as np3
4
5 defaccess_pixels(image):6 print(image.shape)7 height =image.shape[0]8 width = image.shape[1]9 channels = image.shape[2]10 print("width:%s,height:%s,channels:%s", width, height, channels)11 for row inrange(height):12 for col inrange(width):13 for c inrange(channels):14 pv =image[row, col, c]15 image[row, col, c] = 255 -pv16 cv.imshow("pixels_demo", image)17
18
19 #等同于上面的access_pixels()函数
20 definverse(image):21 dst =cv.bitwise_not(image)22 cv.imshow("inverse demo", dst)23
24
25 defcreate_image():26
27 """
28 img = np.zeros([400, 400, 3], np.uint8)29 img[:, :, 0] = np.ones([400, 400]) * 25530 cv.imshow("nwe image_1", img)31 """
32
33 """
34 img = np.zeros([400, 400, 1], np.uint8) # 把矩阵全部置零35 img[:, :, 0] = np.ones([400, 400]) * 12736 # img = img * 037 cv.imshow("nwe image_2", img)38 cv.imwrite("MyImage.png", img)39 """
40
41 m1 = np.ones([3, 3], np.float) #类型要选对
42 m1.fill(10.8)43 print("m1=", m1)44 m2 = m1.reshape([1, 9])45 print("m2=", m2)46 m3 = m1.reshape([9, 1])47 print("m3=", m3)48
49
50 src = cv.imread("1.JPG")51 cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)52 cv.imshow("input image", src)53 t1 =cv.getTickCount()54 inverse(src)55 t2 =cv.getTickCount()56 time = (t2 - t1) /cv.getTickFrequency()57 print("time : %s ms\n" % (time * 1000))58 create_image()59 cv.waitKey(0)60
61 cv.destroyAllWindows()
首先是像素值反转
255 - 原来的像素值
一种是把每个像素点都做255-原来的像素值,代码如下
defaccess_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)for row inrange(height):for col inrange(width):for c inrange(channels):
pv=image[row, col, c]
image[row, col, c]= 255 -pv
cv.imshow("pixels_demo", image)
for循环可以遍历任何序列的项目,如一个列表或者一个字符串
如果你需要遍历数字序列,可以使用内置range()函数。它会生成数列
for i in range(5):print(i)
输出:
01
2
3
4
上面就遍历了每一个像素点,但是运行速度相对比较慢
可以用另一种方法 用cvbitwise_not 对像素值进行二进制的“非”操作,可以达到同样的效果
definverse(image):
dst=cv.bitwise_not(image)
cv.imshow("inverse demo", dst)
接下来就是创建一张图片,然后把它输出来
img = np.zeros([400, 400, 3], np.uint8)
img[:, :, 0]= np.ones([400, 400]) * 255cv.imshow("nwe image-1", img)
如果没有第二行操作,输出的则是一张全黑的图
第二行把通道0的值全改为255,则输出的是一张蓝色的图
通道0是blue,通道1是green,通道2是red
zeros()返回一个全0的n维数组,一共有三个参数:shape(用来指定返回数组的大小)、dtype(数组元素的类型)、order(是否以内存中的C或Fortran连续(行或列)顺序存储多维数据)。
后两个参数都是可选的,一般只需设定第一个参数。
ones()返回一个全1的n维数组,同样也有三个参数:shape(用来指定返回数组的大小)、dtype(数组元素的类型)、order(是否以内存中的C或Fortran连续(行或列)顺序存储多维数据)。
后两个参数都是可选的,一般只需设定第一个参数。和zeros一样
还可以创建一张单通道的图像
img = np.zeros([400, 400, 1], np.uint8) # 把矩阵全部置零
img[:, :, 0] = np.ones([400, 400]) * 127
cv.imshow("nwe image_2", img)
接着可以通过cv.imwrite保存起来
数组的变换(矩阵)
m1 = np.ones([3, 3], np.float) #类型要选对
m1.fill(10.8)print("m1=", m1)
m2= m1.reshape([1, 9])print("m2=", m2)
m3= m1.reshape([9, 1])print("m3=", m3)
通过reshape变换数组,直接看结果就知道了
3*3 → 1*9 → 9*1
也可理解为矩阵,第一个数是行,第二个数是列
最后一个就是getTickCount和getTickFrequency
t1 =cv.getTickCount()
--执行的操作--
t2=cv.getTickCount()
time= (t2 - t1) / cv.getTickFrequency()
这样子就能算出执行的操作所用的时间里,算出来的单位是秒
getTickCount():用于返回从操作系统启动到当前所经的计时周期数
getTickFrequency():用于返回CPU的频率。get Tick Frequency。这里的单位是秒,也就是一秒内重复的次数。
总次数/一秒内重复的次数*1000 = 时间(ms)
总次数/一秒内重复的次数*1000000 = 时间(s)