python有关于图像的深度和通道

目录:
(一)图像的深度和图像的通道
 (1)图像的深度
 (2)图像的通道
(二)自定义一张多通道的图片
(1)zeros 函数
(2)ones  函数
(三)自定义一张单通道的图片
(四)像素操作
(1)numpy操作数组
(2)调用库函数
(五)opnecv 利用getTickCount()和getTickFrequency()计算执行时间
 
 
 
 
 正文:
(一)图像的深度和图像的通道
      (1)图像的深度
               图像中像素点占得bit位数,就是图像的深度,比如:
二值图像:图像的像素点不是0 就是1 (图像不是黑色就是白色),图像像素点占的位数就是 1 位,图像的深度就是1,也称作位图。
灰度图像:图像的像素点位于0-255之间,(0:全黑,255代表:全白,在0-255之间插入了255个等级的灰度)。2^8=255,图像的深度是8。
依次轮推,我们把计算机中存储单个像素点所用的 bit 位称为图像的深度。
     (2)图像的通道
             有了图像深度的概念,我们知道如果是24位的图像,则这个像素点的颜色的取值范围是:从0到2^24。这个范围特别大,如果我们知道了某店的像素值怎么判断像素点的颜色呢?我们知道 RGB是基本的三原色,如果我们用8位代表一种颜色,每种颜色最大是255,这样每个像素点的颜色值的范围就是(0-255,0-255,0-255)。这样图像的通道就是3。
灰度图的图像存储模型
 
灰度图像像素点的存储就是对应的原图从左到右,从上到下,依次排列,每个点的值就是就是像素点的值,每个点的地址就是像素像素点的地址。
RGB图的图像存储模型
RGB彩色图像和灰度图相比,每个像素点都有3个通道。每个通道占的内存空间都是8位。在内存中,RGB 图像的存储是以二维数组的形式。
学习图像的存储就是为了理解图像中像素点的存储情况,有助于我们对每个像素点的操作。
(二)自定义一张多通道的图片-----用到函数:zeros和ones
(1)zeros 函数
 1 # -*- coding=GBK -*-
 2 import cv2 as cv
 3 import numpy as np
 4  
 5  
 6 def create_image():
 7     img = np.zeros([400, 400, 3], np.uint8)#zeros:double类零矩阵  创建400*400 3个通道的矩阵图像 参数时classname为uint8
 8     img[:, :, 0] = np.ones([400, 400])*255#ones([400, 400])是创建一个400*400的全1矩阵,*255即是全255矩阵 并将这个矩阵的值赋给img的第一维
 9     img[:, :, 1] = np.ones([400, 400])*255#第二维全是255
10     img[:, :, 2] = np.ones([400, 400])*255#第三维全是255
11     cv.imshow("自制图片", img)#输出一张400*400的白色图片(255 255 255):蓝(B)、绿(G)、红(R)
12  
13 create_image()
14 cv.waitKey(0)
15 cv.destroyAllWindows()

(2)ones  函数

 1 # -*- coding=GBK -*-
 2 import cv2 as cv
 3 import numpy as np
 4  
 5  
 6 def create_image():
 7     img = np.ones([400, 400, 3], np.uint8)
 8     img[:, :, 0] = img[:, :, 0]*255
 9     img[:, :, 1] = img[:, :, 1]*255
10     img[:, :, 2] = img[:, :, 2]*255
11     cv.imshow("自制图片", img)
12  
13 create_image()
14 cv.waitKey(0)
15 cv.destroyAllWindows()

第8,9,10行换成

image[:, :, 0] = np.ones([400, 400]) * 255
image[:, :, 1] = np.ones([400, 400]) * 255
image[:, :, 2] = np.ones([400, 400]) * 255
建议 img[:, :, 2] = np.ones([400, 400])*255 这样赋值

(3)补充

 1 >>>from numpy import *
 2 >>> a=zeros((3,4))
 3 >>> a
 4 array([[ 0.,  0.,  0.,  0.],
 5        [ 0.,  0.,  0.,  0.],
 6        [ 0.,  0.,  0.,  0.]])
 7 >>> from numpy import *
 8 >>> a=ones((3,4))
 9 >>> a
10 array([[ 1.,  1.,  1.,  1.],
11        [ 1.,  1.,  1.,  1.],
12        [ 1.,  1.,  1.,  1.]])
13 >>> from numpy import *
14 >>> a=eye(3)
15 >>> a
16 array([[ 1.,  0.,  0.],
17        [ 0.,  1.,  0.],
18        [ 0.,  0.,  1.]])

(三)自定义一张单通道的图片

 1 # -*- coding=GBK -*-
 2 import cv2 as cv
 3 import numpy as np
 4  
 5  
 6 def create_image():
 7     img = np.ones([400, 400, 1], np.uint8)
 8     img = img * 127
 9     cv.imshow("自制图片", img)
10  
11 create_image()
12 cv.waitKey(0)
13 cv.destroyAllWindows()
(四)像素操作
(1)numpy操作数组

读取一张图片,修改颜色通道后输出,可以得到图像的:行数,列数,通道数的矩阵,对矩阵进行操作可改变图像像素

 1 # -*- coding=GBK -*-
 2 import cv2 as cv
 3 import numpy as np
 4  
 5  
 6 #numpy数组操作
 7 def access_pixles(image):
 8     print(image.shape)
 9     height = image.shape[0]
10     width = image.shape[1]
11     channel = image.shape[2]
12     print("width : %s, height : %s, channel : %s" % (width, height, channel))
13     for row in range(height):
14         for col in range(width):
15             for c in range(channel):
16                 pv = image[row, col, c]
17                 image[row, col, c] = 255 - pv
18     cv.imshow("修改后", image)
19  
20  
21 src = cv.imread("C://1.jpg")
22 #cv.namedWindow("原来", cv.WINDOW_NORMAL)
23 cv.imshow("原来", src)
24 t1 = cv.getTickCount()#毫秒级别的计时函数,记录了系统启动以来的时间毫秒
25 access_pixles(src)
26 t2 = cv.getTickCount()
27 time = (t2 - t1)*1000/cv.getTickFrequency()#getTickFrequency用于返回CPU的频率,就是每秒的计时周期数
28 print("time: %s" % time)#输出运行的时间
29 cv.waitKey(0)
30 cv.destroyAllWindows()
(2)调用库函数
 1 # -*- coding=GBK -*-
 2 import cv2 as cv
 3 import numpy as np
 4  
 5  
 6 #像素取反
 7 def inverse(image):
 8     dst = cv.bitwise_not(image)
 9     cv.imshow("取反", dst)
10  
11  
12 src = cv.imread("C://1.jpg")
13 cv.namedWindow("原来", cv.WINDOW_NORMAL)
14 cv.imshow("原来", src)
15 t1 = cv.getTickCount()
16 inverse(src)
17 t2 = cv.getTickCount()
18 time = (t2 - t1)*1000/cv.getTickFrequency()
19 print("time: %s" % time)
20 cv.waitKey(0)
21 cv.destroyAllWindows()
(五)opnecv 利用getTickCount()和getTickFrequency()计算执行时间
t1 = cv.getTickCount()
picture_message(scr)
t2 = cv.getTickCount()
time = 1000*(t2-t1)/cv.getTickFrequency()
print('Time is %s ms'% time)

getTickCount():用于返回从操作系统启动到当前所经的计时周期数,看名字也很好理解,get Tick Count(s)。
getTickFrequency():用于返回CPU的频率。get Tick Frequency。这里的单位是秒,也就是一秒内重复的次数。

所以剩下的就很清晰了:
总次数/一秒内重复的次数 = 时间(s)
1000 *总次数/一秒内重复的次数= 时间(ms)

这个逻辑很清晰,没什么问题,但是这里有一个小坑,那就是C版本的cvGetTickFrequency()函数和C++版本的getTickFrequency()的单位不一样,前者以ms计算频率,后者以s为单位计算频率,所以如果使用C版本的cvGetTickFrequency()计算时间的话,应该是:
总次数/一秒内重复的次数*1000 = 时间(ms)
总次数/一秒内重复的次数= 时间(s)

参考:
https://blog.csdn.net/u013355826/article/details/64905921
https://blog.csdn.net/u011321546/article/details/79523115
https://www.cnblogs.com/jczhuang/p/9766950.html
posted @ 2020-09-18 19:57  山那边不是山  阅读(1268)  评论(0编辑  收藏  举报