Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将图像阵列切片为子阵列_Python_Arrays_Numpy - Fatal编程技术网

Python 将图像阵列切片为子阵列

Python 将图像阵列切片为子阵列,python,arrays,numpy,Python,Arrays,Numpy,这是我的代码,用于将图像数组切片为8*8块。我试图将此数组转换为8*8矩阵,但在运行上述python代码时生成了3*8矩阵。有关这方面的任何信息都会有所帮助 import numpy as np import numpy as numpy import cv2 from PIL import Image from numpy import array img = cv2.imread('image test.jpg') gray = cv2.cvtColor(img.cv2.COLOR_BGR2

这是我的代码,用于将图像数组切片为8*8块。我试图将此数组转换为8*8矩阵,但在运行上述python代码时生成了3*8矩阵。有关这方面的任何信息都会有所帮助

import numpy as np
import numpy as numpy
import cv2
from PIL import Image
from numpy import array
img = cv2.imread('image test.jpg')
gray = cv2.cvtColor(img.cv2.COLOR_BGR2GRAY)
myArray = array(gray)
slice = myArray[:8,:8]
print slice
print myArray 
k = cv2.waitKey(0)
if k == 27:
    cv2.destroyAllWindows()
此输出重复三次。当print res[40]显示错误时,列表索引超出范围。

我猜您的数组,即myArray的形状为3*n,n>=8。这是这种行为唯一可能的原因

演示:

将数据拆分为8x8子阵列的代码:

>>> import numpy as np
>>> a = np.ones((16,16))
>>> slice = a[:8,:8]
>>> slice.shape
(8, 8)
>>> a = np.ones((3,16))
>>> slice = a[:8,:8]
>>> slice.shape
(3, 8)
更换这些线路:

>>> import numpy as np
>>> data = np.random.randint(0,10,size=(320,240)) # create a random array of 320x240
>>> data = np.split(data, data.shape[0]/8)  # first split into 8x240, 8x240,... sub-arrays, i.e., split by rows of 8 first.
>>> res = []
>>> for arr in data:
...     res.extend(np.split(arr,arr.shape[1]/8, axis=1)) # now for each 8x240 sub-array split it column-wise into 8x8, 8x8,... arrays
... 
>>> res[0]
array([[5, 7, 3, 0, 2, 7, 5, 2],
       [8, 1, 8, 6, 3, 8, 8, 5],
       [7, 4, 6, 7, 9, 5, 1, 6],
       [0, 2, 4, 3, 1, 2, 0, 3],
       [4, 4, 8, 8, 5, 7, 4, 2],
       [2, 0, 8, 2, 9, 8, 9, 3],
       [6, 4, 0, 3, 3, 3, 5, 8],
       [6, 2, 8, 5, 0, 5, 1, 3]])
       .
       .
       .
>>> res[1199]
array([[8, 5, 5, 9, 1, 7, 5, 4],
       [0, 1, 8, 0, 3, 8, 5, 9],
       [2, 5, 3, 6, 7, 2, 8, 8],
       [1, 1, 7, 0, 0, 4, 3, 1],
       [5, 5, 8, 6, 6, 6, 5, 7],
       [9, 4, 2, 2, 7, 2, 1, 1],
       [6, 9, 5, 2, 5, 9, 3, 4],
       [1, 8, 1, 9, 7, 6, 7, 0]])
与:


但在实际图像中,例如img是320x240形状的。那么我创建myArray时它是如何给出3*n的形状的呢?img.shape和myArray.shape resultimg=cv2.imread'image test.jpg'会产生一个numpy数组,所以使用myArray=arrayimg将numpy数组转换为numpy数组永远不会导致数据丢失。我想说的是,img的尺寸也是3*240。因此,cv2.imread'image test.jpg'将导致3*240大小的图像读取。请检查图像形状…两者的结果均为240320,3谢谢通过将图像转换为灰度解决了问题。
>>> import numpy as np
>>> data = np.random.randint(0,10,size=(320,240)) # create a random array of 320x240
>>> data = np.split(data, data.shape[0]/8)  # first split into 8x240, 8x240,... sub-arrays, i.e., split by rows of 8 first.
>>> res = []
>>> for arr in data:
...     res.extend(np.split(arr,arr.shape[1]/8, axis=1)) # now for each 8x240 sub-array split it column-wise into 8x8, 8x8,... arrays
... 
>>> res[0]
array([[5, 7, 3, 0, 2, 7, 5, 2],
       [8, 1, 8, 6, 3, 8, 8, 5],
       [7, 4, 6, 7, 9, 5, 1, 6],
       [0, 2, 4, 3, 1, 2, 0, 3],
       [4, 4, 8, 8, 5, 7, 4, 2],
       [2, 0, 8, 2, 9, 8, 9, 3],
       [6, 4, 0, 3, 3, 3, 5, 8],
       [6, 2, 8, 5, 0, 5, 1, 3]])
       .
       .
       .
>>> res[1199]
array([[8, 5, 5, 9, 1, 7, 5, 4],
       [0, 1, 8, 0, 3, 8, 5, 9],
       [2, 5, 3, 6, 7, 2, 8, 8],
       [1, 1, 7, 0, 0, 4, 3, 1],
       [5, 5, 8, 6, 6, 6, 5, 7],
       [9, 4, 2, 2, 7, 2, 1, 1],
       [6, 9, 5, 2, 5, 9, 3, 4],
       [1, 8, 1, 9, 7, 6, 7, 0]])
for arr in data:
    res.extend(np.split(arr,arr.shape[1]/8, axis = 1)
    print res[0]
for arr in data:
    res.extend(np.split(arr,arr.shape[1]/8, axis = 1)

for i in res: print i