我希望将计算出的2D数组元素插入3D数组中。第一个for循环是需要多少个数组。例如当i = 1时,值将进入3D数组的第一个数组。计算出的元素显示在代码的底行
"dctArray[dctRow][dctColumn]".
基本上,我试图将此2D数组值等同于3D数组的第一个数组。像这样:
dctArrayBlocks[i][dctRow][dctColumn] = dctArray[dctRow][dctColumn]
但是这不适用于整个数组。当我打印
的单个值时dctArrayBlocks[i][dctRow][dctColumn]
这是正确的,但是当我尝试打印整个数组时,输出的值非常不正确。这些输出在底部。
谢谢,这是完整的代码:
noBlocksOn1Axis = (int(imageLength/blockSize))
blocksList = []
previousRow = 0
for rowBlock in range(noBlocksOn1Axis):
previousRow = rowBlock * blockSize
previousColumn = 0
for columnBlock in range(noBlocksOn1Axis):
previousColumn = columnBlock * blockSize
block = arrayY[previousRow:previousRow+blockSize,previousColumn:previousColumn+blockSize]
blocksList.append(block)
dctArray = np.zeros((8,8))
dctArrayBlocks = np.zeros(shape = (4,8,8))
for i in range(4):
for dctRow in range(blockSize):
for dctColumn in range (blockSize):
dctSum = 0
for row in range (blockSize):
for column in range (blockSize):
dctSum = dctSum + blocksList[i][row][column]*math.cos(((2.0*row+1)*dctRow*math.pi)/16.0)*math.cos(((2.0*column+1)*dctColumn*math.pi)/16.0)
if(dctRow == 1):
cRow = 1/math.sqrt(2)
else:
cRow = 1
if(dctColumn == 1):
cColumn= 1/math.sqrt(2)
else:
cColumn = 1
dctArray[dctRow][dctColumn] =
1/4.0*cRow*cColumn*dctSum
dctArrayBlocks[i][dctRow][dctColumn] =
dctArray[dctRow][dctColumn]
print(dctArrayBlocks[i][dctRow][dctColumn])
print(dctArrayBlocks)
以下是列表形式的预期输出
1160.75
2.15309774781
0.230969883128
0.325283736973
-0.883883476483
-0.332410006991
0.0956708580911
-0.370181380455
-29.1901374775
-0.0455109087818
0.543799659533
0.500630540423
-0.469718941578
0.21007762182
0.340311556876
-0.818563547551
-1.97625897152
1.34074300692
0.265165042945
-1.05682448828
0.450332944883
0.750180006989
0.109834957055
-0.46727710967
-2.38227629503
-0.720900476592
0.904589640549
1.02494023825
-0.564894042278
-1.11318780084
-0.318826625164
-0.627633469915
0.883883476483
0.151835185028
0.760559161902
0.114395653893
0.625
0.465174095446
-0.450332944883
0.732747465688
-1.08735713314
-0.336526203213
0.0151562551075
-0.582857714948
-0.652328957388
0.00538984764367
-0.131671763221
0.541152940662
2.15798528204
-0.175494902565
-0.640165042945
-0.631820804478
0.760559161902
0.315865402699
-0.265165042945
0.266936390222
0.373044516988
-0.443563547551
0.319493333954
-0.857754793709
0.355794721991
0.311551890598
-0.111193339081
0.0606917316737
这就是我要得到的。
[[[ 1.16075000e+03 2.15309775e+00 2.30969883e-01 3.25283737e-01
-8.83883476e-01 -3.32410007e-01 9.56708581e-02 -3.70181380e-01]
[ -2.91901375e+01 -4.55109088e-02 5.43799660e-01 5.00630540e-01
-4.69718942e-01 2.10077622e-01 3.40311557e-01 -8.18563548e-01]
[ -1.97625897e+00 1.34074301e+00 2.65165043e-01 -1.05682449e+00
4.50332945e-01 7.50180007e-01 1.09834957e-01 -4.67277110e-01]
[ -2.38227630e+00 -7.20900477e-01 9.04589641e-01 1.02494024e+00
-5.64894042e-01 -1.11318780e+00 -3.18826625e-01 -6.27633470e-01]
[ 8.83883476e-01 1.51835185e-01 7.60559162e-01 1.14395654e-01
6.25000000e-01 4.65174095e-01 -4.50332945e-01 7.32747466e-01]
[ -1.08735713e+00 -3.36526203e-01 1.51562551e-02 -5.82857715e-01
-6.52328957e-01 5.38984764e-03 -1.31671763e-01 5.41152941e-01]
[ 2.15798528e+00 -1.75494903e-01 -6.40165043e-01 -6.31820804e-01
7.60559162e-01 3.15865403e-01 -2.65165043e-01 2.66936390e-01]
[ 3.73044517e-01 -4.43563548e-01 3.19493334e-01 -8.57754794e-01
3.55794722e-01 3.11551891e-01 -1.11193339e-01 6.06917317e-02]]
答案 0 :(得分:0)
好吧,我试图重写代码以使其更具pythonic风格,并展示numpy向量化的强大功能:
import numpy as np
np.random.seed(1)
imageLength = 16
arrayY= np.random.randint(1,255,(imageLength,imageLength))
blockSize = 8
noBlocksOn1Axis = imageLength//blockSize
blocksList = []
previousRow = 0
for rowBlock in range(noBlocksOn1Axis):
previousRow = rowBlock * blockSize
previousColumn = 0
for columnBlock in range(noBlocksOn1Axis):
previousColumn = columnBlock * blockSize
block = arrayY[previousRow:previousRow+blockSize , previousColumn:previousColumn+blockSize]
blocksList.append(block)
dctArrayBlocks = np.zeros((4,8,8))
bl = np.array(blocksList)
dctSum = np.empty((blockSize,blockSize))
m = np.outer(np.arange(blockSize),np.ones(blockSize))
for i in range(bl.shape[0]):
for n1 in range(blockSize):
for n2 in range (blockSize):
dctSum[n1,n2] = np.sum(
bl[i] *
np.cos((2*m +1) * n1*np.pi/16.) *
np.cos((2*m.T+1) * n2*np.pi/16.)
)
dctArrayBlocks[i] = dctSum/4
dctArrayBlocks [:,1,:] /= np.sqrt(2)
dctArrayBlocks [:,:,1] /= np.sqrt(2)
这应该可以完全执行您的代码,但是要使用numpy和python的功能。
正如我在评论中指出的,区别只是格式问题, 关于格式,您可以使用类似的
np.set_printoptions(formatter={'float':lambda x: str(x)})