1. 程式人生 > >Convert mat to numpy array , numpy gradient

Convert mat to numpy array , numpy gradient

1.mat to array:

arr = np.asarray(mat)

2.matlab and numpy gradient:

第二列減第一列,第三列減第一列除以2,第四列減第二列,......,最後一列減前一列。(行同理)

import cv2
import numpy as np
...#image read
img = cv2.imread(imgpath)
img_arr = np.asarray(img)   #mat to array
grad = np.gradient(img_arr) #gradient,parameter:<strong>edge_order</strong>---Gradient is calculated using N<sup>th</sup> order accurate difference the boundaries. 
<pre name="code" class="python">                            #Default: 1.<span class="versionmodified"> New in version 1.9.1.</span>
print(grad) #grad[0]---row (Y axis); grad[1]---col(X axis)
print(np.gradient(img_arr),2) #edge_order = 2

reference:

convert:

http://stackoverflow.com/questions/7116751/how-to-convert-cvmat-to-numpy-array

(more)http://stackoverflow.com/questions/3337301/numpy-matrix-to-array

gradient:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.gradient.html