使用Python-opencv对图像原始数据进行操作

使用Python-opencv对图像原始数据进行操作

在ipython下进行调试,结果如下

以下是一些很基础的图像操作,虽然现有的图像处理工具很强大,封装的很高级,但正是因为太高级,也导致使用者不了解一些底层的东西,当然也包括我。

"""
参考:《OpenCV3计算机视觉 Python语言实现 第二版》,该书使用的是Python2
"""
In [1]: import cv2

In [2]: import numpy as np

In [4]: img = np.zeros([640,480,3],dtype = np.uint8)

In [5]: img
Out[5]:
array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       ...,

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]], dtype=uint8)

In [6]: cv2.imwrite('black.png',img)#RGB888数据要保存成png格式
Out[6]: True

输出结果
该图即为输出结果。

"""
In [7]: img[:,:,0]
Out[7]:
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)

在opencv中,读写图片通道顺序位均BGR

"""
In [8]: img[:,:,0] = 255  # 通道1 blue

In [9]: cv2.imwrite('blu.png',img)
Out[9]: True

In [11]: img[:,:,1] = 255 # 通道2 green

In [12]: img[:,:,0] = 0

In [13]: cv2.imwrite('green.png',img)
Out[13]: True

In [14]: img[:,:,1] = 0

In [15]: img[:,:,2] = 255

In [16]: cv2.imwrite('red.png',img)
Out[16]: True

In [17]: image = cv2.imread('red.png')

In [18]: image
Out[18]:
array([[[  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        ...,
        [  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255]],

       [[  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        ...,
        [  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255]],

       [[  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        ...,
        [  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255]],

       ...,

       [[  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        ...,
        [  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255]],

       [[  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        ...,
        [  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255]],

       [[  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        ...,
        [  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255]]], dtype=uint8)

In [23]: imgcvt = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
In [25]: imgcvt
Out[25]:
array([[[255,   0,   0],
        [255,   0,   0],
        [255,   0,   0],
        ...,
        [255,   0,   0],
        [255,   0,   0],
        [255,   0,   0]],

       [[255,   0,   0],
        [255,   0,   0],
        [255,   0,   0],
        ...,
        [255,   0,   0],
        [255,   0,   0],
        [255,   0,   0]],

       [[255,   0,   0],
        [255,   0,   0],
        [255,   0,   0],
        ...,
        [255,   0,   0],
        [255,   0,   0],
        [255,   0,   0]],

       ...,

       [[255,   0,   0],
        [255,   0,   0],
        [255,   0,   0],
        ...,
        [255,   0,   0],
        [255,   0,   0],
        [255,   0,   0]],

       [[255,   0,   0],
        [255,   0,   0],
        [255,   0,   0],
        ...,
        [255,   0,   0],
        [255,   0,   0],
        [255,   0,   0]],

       [[255,   0,   0],
        [255,   0,   0],
        [255,   0,   0],
        ...,
        [255,   0,   0],
        [255,   0,   0],
        [255,   0,   0]]], dtype=uint8)
In [26]: cv2.imwrite('BLUE.png',imgcvt)
Out[26]: True

图像数据与原始字节转换演示:

"""
In [37]: A = np.zeros([5,5,3],dtype = np.uint8)
In [44]: for i in range(5):
    ...:     for j in range(5):
    ...:         A[:,:,0][i,j] = i+j
    ...:

In [45]: A[:,:,0]
Out[45]:
array([[0, 1, 2, 3, 4],
       [1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7],
       [4, 5, 6, 7, 8]], dtype=uint8)
In [46]: A[:,:,1] = A[:,:,0] +1

In [47]: A[:,:,2] = A[:,:,0] +2

In [48]: A[:,:,1]
Out[48]:
array([[1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7],
       [4, 5, 6, 7, 8],
       [5, 6, 7, 8, 9]], dtype=uint8)

In [49]: A[:,:,2]
Out[49]:
array([[ 2,  3,  4,  5,  6],
       [ 3,  4,  5,  6,  7],
       [ 4,  5,  6,  7,  8],
       [ 5,  6,  7,  8,  9],
       [ 6,  7,  8,  9, 10]], dtype=uint8)
#图像数据与原始字节转换  
In [51]: AbyteArray = bytearray(A) #
In [52]: AbyteArray
bytearray(b'\x00\x01\x02\x01\x02\x03\x02\x03\x04\x03\x04\x05\x04\x05\x06\x01\x02\x03\x02\x03\x04\x03\x04\x05\x04\x05\x06\x05\x06\x07\x02\x03\x04\x03\x04\x05\x04\x05\x06\x05\x06\x07\x06\x07\x08\x03\x04\x05\x04\x05\x06\x05\x06\x07\x06\x07\x08\x07\x08\t\x04\x05\x06\x05\x06\x07\x06\x07\x08\x07\x08\t\x08\t\n')
#由此可以看出,使用bytearray进行数据与原始字节转换时,是按照先处理像素后处理通道的原则进行的。

In [64]: A.tostring() == bytearray(A)
Out[64]: True

In [65]: A.tostring() == A.tobytes()
Out[65]: True

In [84]: np.fromstring(bytearray(A),dtype='uint8')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-84-c84d62f1d047> in <module>()
----> 1 np.fromstring(bytearray(A),dtype='uint8')

TypeError: fromstring() argument 1 must be read-only bytes-like object, not bytearray

In [86]: B = np.frombuffer(A.tobytes(),dtype='uint8')

In [87]: B
Out[87]:
array([ 0,  1,  2,  1,  2,  3,  2,  3,  4,  3,  4,  5,  4,  5,  6,  1,  2,
        3,  2,  3,  4,  3,  4,  5,  4,  5,  6,  5,  6,  7,  2,  3,  4,  3,
        4,  5,  4,  5,  6,  5,  6,  7,  6,  7,  8,  3,  4,  5,  4,  5,  6,
        5,  6,  7,  6,  7,  8,  7,  8,  9,  4,  5,  6,  5,  6,  7,  6,  7,
        8,  7,  8,  9,  8,  9, 10], dtype=uint8)
In [91]: C = B.reshape(5,5,3)
In [94]: C == A
Out[94]:
array([[[ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True]],

       [[ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True]],

       [[ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True]],

       [[ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True]],

       [[ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True],
        [ True,  True,  True]]])

版权声明:本文为python_java0C原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。