Gegeven een NumPy-tabel x, bestaande uit ints die ofwel een zwart/wit afbeelding (2D tabel) ofwel een afbeelding in kleur (3D) voorstelt. We zullen gebruik maken van de DCT, waarbij we de originele figuur zullen opdelen in rechthoekjes van hoogte $$M$$ en breedte $$N$$. Je mag aannemen dat het aantal rijen van de figuur een geheel veelvoud van $$M$$ is, en anloog voor de kolommen (dus een geheel veelvoud dan $$N$$).

Programmeer volgende functies:

Hoewel de afbeeldingen uit gehele getallen opgebouwd zijn, moet je de transformaties via floats uitvoeren.

Opmerkingen:

Voorbeeld

DCT
r = np.array(
[[ 0,  1,  2,  3,  4,  5,  6,  7], 
 [ 8,  9, 10, 11, 12, 13, 14, 15], 
 [16, 17, 18, 19, 20, 21, 22, 23], 
 [24, 25, 26, 27, 28, 29, 30, 31], 
 [32, 33, 34, 35, 36, 37, 38, 39], 
 [40, 41, 42, 43, 44, 45, 46, 47]])
 

r_dct = dct2D(r, 3, 4)
#[[ 1.14000000e+02 -9.46296609e+00 -1.77635684e-15 -6.72512294e-01
#   1.62000000e+02 -9.46296609e+00 -5.32907052e-15 -6.72512294e-01]
# [-5.54256258e+01 -8.88178420e-16  1.30038346e-15  3.55271368e-15
#  -5.54256258e+01 -8.88178420e-16  2.55645743e-15  3.55271368e-15]
# [ 0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00
#   0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00]
# [ 4.02000000e+02 -9.46296609e+00 -1.06581410e-14 -6.72512294e-01
#   4.50000000e+02 -9.46296609e+00 -1.42108547e-14 -6.72512294e-01]
# [-5.54256258e+01 -8.88178420e-16 -3.50840419e-15  3.55271368e-15
#  -5.54256258e+01 -8.88178420e-16  5.58888823e-15  3.55271368e-15]
# [ 1.42108547e-14  3.94430453e-31 -3.94430453e-31 -7.88860905e-31
#   1.42108547e-14  3.94430453e-31 -3.94430453e-31 -7.88860905e-31]]
IDCT
r = np.array([[114.0, -9.462966089696849, -1.7763568394002505e-15, -0.6725122937519501, 162.0, -9.462966089696845, -5.329070518200751e-15, -0.6725122937519572], [-55.42562584220408, -8.881784197001252e-16, 1.300383458813452e-15, 3.552713678800501e-15, -55.42562584220407, -8.881784197001252e-16, 2.556457425760472e-15, 3.552713678800501e-15], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [402.0, -9.462966089696849, -1.0658141036401503e-14, -0.6725122937519643, 450.0, -9.462966089696828, -1.4210854715202004e-14, -0.6725122937519927], [-55.42562584220407, -8.881784197001252e-16, -3.5084041869340696e-15, 3.552713678800501e-15, -55.425625842204056, -8.881784197001252e-16, 5.588888232107742e-15, 3.552713678800501e-15], [1.4210854715202004e-14, 3.944304526105059e-31, -3.944304526105059e-31, -7.888609052210118e-31, 1.4210854715202004e-14, 3.944304526105059e-31, -3.944304526105059e-31, -7.888609052210118e-31]])

r_idct = idct2D(r_dct, 3, 4)


#[[-1.18423789e-15  1.00000000e+00  2.00000000e+00  3.00000000e+00
#   4.00000000e+00  5.00000000e+00  6.00000000e+00  7.00000000e+00]
# [ 8.00000000e+00  9.00000000e+00  1.00000000e+01  1.10000000e+01
#   1.20000000e+01  1.30000000e+01  1.40000000e+01  1.50000000e+01]
# [ 1.60000000e+01  1.70000000e+01  1.80000000e+01  1.90000000e+01
#   2.00000000e+01  2.10000000e+01  2.20000000e+01  2.30000000e+01]
# [ 2.40000000e+01  2.50000000e+01  2.60000000e+01  2.70000000e+01
#   2.80000000e+01  2.90000000e+01  3.00000000e+01  3.10000000e+01]
# [ 3.20000000e+01  3.30000000e+01  3.40000000e+01  3.50000000e+01
#   3.60000000e+01  3.70000000e+01  3.80000000e+01  3.90000000e+01]
# [ 4.00000000e+01  4.10000000e+01  4.20000000e+01  4.30000000e+01
#   4.40000000e+01  4.50000000e+01  4.60000000e+01  4.70000000e+01]]

compressie
r = np.array(
[[ 0,  1,  2,  3,  4,  5,  6,  7], 
 [ 8,  9, 10, 11, 12, 13, 14, 15], 
 [16, 17, 18, 19, 20, 21, 22, 23], 
 [24, 25, 26, 27, 28, 29, 30, 31], 
 [32, 33, 34, 35, 36, 37, 38, 39], 
 [40, 41, 42, 43, 44, 45, 46, 47]])
 
r_rec, MSE = compressie(r, 3, 4, 2)

#[[4.28932188e-02 8.96446609e-01 2.10355339e+00 2.95710678e+00
#  4.04289322e+00 4.89644661e+00 6.10355339e+00 6.95710678e+00]
# [8.04289322e+00 8.89644661e+00 1.01035534e+01 1.09571068e+01
#  1.20428932e+01 1.28964466e+01 1.41035534e+01 1.49571068e+01]
# [1.60428932e+01 1.68964466e+01 1.81035534e+01 1.89571068e+01
#  2.00428932e+01 2.08964466e+01 2.21035534e+01 2.29571068e+01]
# [2.40428932e+01 2.48964466e+01 2.61035534e+01 2.69571068e+01
#  2.80428932e+01 2.88964466e+01 3.01035534e+01 3.09571068e+01]
# [3.20428932e+01 3.28964466e+01 3.41035534e+01 3.49571068e+01
#  3.60428932e+01 3.68964466e+01 3.81035534e+01 3.89571068e+01]
# [4.00428932e+01 4.08964466e+01 4.21035534e+01 4.29571068e+01
#  4.40428932e+01 4.48964466e+01 4.61035534e+01 4.69571068e+01]]

# 0.006282