shaoheshaohe 发表于 2020-12-24 10:48:51

在MATLAB中有blkproc (blockproc)对数据处理, 在python下没找到对...

在MATLAB中有blkproc (blockproc)对数据处理, 在python下没找到对应的Function,这里利用numpy 的split(hsplit和vsplit) 对数据分块处理成8x8的小块, 然后在利用OpenCV的dct函数做变换, 同时利用idct 验证数据变换是否正确.
import numpy as np
import cv2a = np.arange(256).reshape((16,16))
print("ori data: \n{}".format(a))
m,n = a.shape
print(m,n)# Y = np.zeros(256).reshape((16,16))hdata = np.vsplit(a,n/8) # 垂直分成高度度为8 的块
for i in range(0, n//8):
   blockdata = np.hsplit(hdata,m/8)
   #垂直分成高度为8的块后,在水平切成长度是8的块, 也就是8x8 的块
   for j in range(0, m//8):
         block = blockdata
         print("block[{},{}] data \n{}".format(i,j,blockdata))
         Yb = cv2.dct(block.astype(np.float))
         print("dct data\n{}".format(Yb))
         iblock = cv2.idct(Yb)
         print("idct data\n{}".format(iblock))
以下是最后个8x8块的数据:block data
[






]
dct data
[[ 1.56400000e+03 -1.82216412e+010.00000000e+00 -1.90481783e+00
    0.00000000e+00 -5.68239222e-010.00000000e+00 -1.43407825e-01]
[-2.91546259e+020.00000000e+000.00000000e+000.00000000e+00
    0.00000000e+000.00000000e+000.00000000e+000.00000000e+00]
[ 0.00000000e+000.00000000e+000.00000000e+000.00000000e+00
    0.00000000e+000.00000000e+000.00000000e+000.00000000e+00]
[-3.04770852e+010.00000000e+000.00000000e+000.00000000e+00
    0.00000000e+000.00000000e+000.00000000e+000.00000000e+00]
[ 0.00000000e+000.00000000e+000.00000000e+000.00000000e+00
    0.00000000e+000.00000000e+000.00000000e+000.00000000e+00]
[-9.09182756e+000.00000000e+000.00000000e+000.00000000e+00
    0.00000000e+000.00000000e+000.00000000e+000.00000000e+00]
[ 0.00000000e+000.00000000e+000.00000000e+000.00000000e+00
    0.00000000e+000.00000000e+000.00000000e+000.00000000e+00]
[-2.29452520e+000.00000000e+000.00000000e+000.00000000e+00
    0.00000000e+000.00000000e+000.00000000e+000.00000000e+00]]
idct data
[






]数据与原数据值大小一致.cnblogs Tags: python, opencv, DCT

shaoheshaohe 发表于 2020-12-24 10:48:58

https://www.cnblogs.com/gxgl314/p/9287628.html
页: [1]
查看完整版本: 在MATLAB中有blkproc (blockproc)对数据处理, 在python下没找到对...