澳门新葡亰赌995577python数据分析(六)

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

这里利用numpy 的split(hsplit和vsplit) 对数据分块处理成8×8的小块,
然后在利用OpenCV的dct函数做变换, 同时利用idct 验证数据变换是否正确.

import numpy as np
import cv2

a = 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[i],m/8)
     #垂直分成高度为8的块后,在水平切成长度是8的块, 也就是8×8 的块
     for j in range(0, m//8):
         block = blockdata[j]
         print(“block[{},{}] data \n{}”.format(i,j,blockdata[j]))
         Yb = cv2.dct(block.astype(np.float))
         print(“dct data\n{}”.format(Yb))
         iblock = cv2.idct(Yb)
         print(“idct data\n{}”.format(iblock))

以下是最后个8×8块的数据:

block[1,1] data
[[136 137 138 139 140 141 142 143]
  [152 153 154 155 156 157 158 159]
  [168 169 170 171 172 173 174 175]
  [184 185 186 187 188 189 190 191]
  [200 201 202 203 204 205 206 207]
  [216 217 218 219 220 221 222 223]
  [232 233 234 235 236 237 238 239]
澳门新葡亰赌995577,  [248 249 250 251 252 253 254 255]]
dct data
[[ 1.56400000e+03 -1.82216412e+01  0.00000000e+00 -1.90481783e+00
    0.00000000e+00 -5.68239222e-01  0.00000000e+00 -1.43407825e-01]
  [-2.91546259e+02  0.00000000e+00  0.00000000e+00  0.00000000e+00
    0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00]
  [ 0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00
    0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00]
  [-3.04770852e+01  0.00000000e+00  0.00000000e+00  0.00000000e+00
    0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00]
  [ 0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00
    0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00]
  [-9.09182756e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00
    0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00]
  [ 0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00
    0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00]
  [-2.29452520e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00
    0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00]]
idct data
[[136. 137. 138. 139. 140. 141. 142. 143.]
  [152. 153. 154. 155. 156. 157. 158. 159.]
  [168. 169. 170. 171. 172. 173. 174. 175.]
  [184. 185. 186. 187. 188. 189. 190. 191.]
  [200. 201. 202. 203. 204. 205. 206. 207.]
  [216. 217. 218. 219. 220. 221. 222. 223.]
  [232. 233. 234. 235. 236. 237. 238. 239.]
  [248. 249. 250. 251. 252. 253. 254. 255.]]

数据与原数据值大小一致.

cnblogs Tags: python,
opencv,
DCT

# -*- coding: utf-8 -*-

#向量相加-Python

def pythonsum(n):

a = range(n)

b = range(n)

c = []

for i in range(len(a)):

a[i] = i ** 2

b[i] = i ** 3

c.append(a[i] + b[i])

return c

#向量相加-NumPy

import numpy as np

def numpysum(n):

a = numpy.arange(n) ** 2

b = numpy.arange(n) ** 3

c = a + b

return c

#效率比较

import sys

from datetime import datetime

import numpy as np

size = 1000

start = datetime.now()

c = pythonsum(size)

delta = datetime.now() – start

print “The last 2 elements of the sum”, c[-2:]

print “PythonSum elapsed time in microseconds”, delta.microseconds

start = datetime.now()

c = numpysum(size)

delta = datetime.now() – start

print “The last 2 elements of the sum”, c[-2:]

print “NumPySum elapsed time in microseconds”, delta.microseconds

#numpy数组

a = arange(5)

a.dtype

a

a.shape

#创建多维数组

m = np.array([np.arange(2), np.arange(2)])

print m

print m.shape

print m.dtype

np.zeros(10)

np.zeros((3, 6))

np.empty((2, 3, 2))

np.arange(15)

#选取数组元素

a = np.array([[1,2],[3,4]])

print “In: a”

print a

print “In: a[0,0]”

print a[0,0]

print “In: a[0,1]”

print a[0,1]

print “In: a[1,0]”

print a[1,0]

print “In: a[1,1]”

print a[1,1]

#numpy数据类型

print “In: float64(42)”

print np.float64(42)

print “In: int8(42.0)”

print np.int8(42.0)

print “In: bool(42)”

print np.bool(42)

print np.bool(0)

print “In: bool(42.0)”

print np.bool(42.0)

print “In: float(True)”

print np.float(True)

print np.float(False)

print “In: arange(7, dtype=uint16)”

print np.arange(7, dtype=np.uint16)

print “In: int(42.0 + 1.j)”

try:

print np.int(42.0 + 1.j)

except TypeError:

print “TypeError”

#Type error

print “In: float(42.0 + 1.j)”

print float(42.0 + 1.j)

#Type error

# 数据类型转换

arr = np.array([1, 2, 3, 4, 5])

arr.dtype

float_arr = arr.astype(np.float64)

float_arr.dtype

arr = np.array([3.7, -1.2, -2.6, 0.5, 12.9, 10.1])

arr

arr.astype(np.int32)

numeric_strings = np.array([‘1.25’, ‘-9.6′, ’42’], dtype=np.string_)

numeric_strings.astype(float)

#数据类型对象

a = np.array([[1,2],[3,4]])

print a.dtype.byteorder

print a.dtype.itemsize

#字符编码

print np.arange(7, dtype=’f’)

print np.arange(7, dtype=’D’)

print np.dtype(float)

print np.dtype(‘f’)

print np.dtype(‘d’)

print np.dtype(‘f8’)

print np.dtype(‘Float64’)

#dtype类的属性

t = np.dtype(‘Float64’)

print t.char

print t.type

print t.str

#创建自定义数据类型

t = np.dtype([(‘name’, np.str_, 40), (‘numitems’, np.int32), (‘price’,
np.float32)])

print t

print t[‘name’]

itemz = np.array([(‘Meaning of life DVD’, 42, 3.14), (‘Butter’, 13,
2.72)], dtype=t)

print itemz[1]

#数组与标量的运算

arr = np.array([[1., 2., 3.], [4., 5., 6.]])

arr

arr * arr

arr – arr

1 / arr

arr ** 0.5

#一维数组的索引与切片