@前言
因工程需要将matlab代码重构成pyhton代码,以下是个人历程,希望对后来者有所帮助。有错误麻烦积极指正,我将不胜感激。
- matlab 函数 对应python基本函数文档下载(全):猛点免费下载
- matlab 对应 numpy:https://numpy.org/doc/stable/user/numpy-for-matlab-users.html#table-of-rough-matlab-numpy-equivalents
-
matla和pyhton
图像读取差别
cv2读入的形式为B G R matlab读入的形式为R G B
-
matlab 加载mat文件
load('file.mat');
import scipy.io as scio load_mat = scio.loadmat("file.mat") 变量a = load_mat["变量a"] 变量b = load_mat["变量b"]
-
matlab 变换矩阵形状
reshape(A,[row col])
A.reshape((row, col), order='F')
-
matlab 复制矩阵行列
repmat(A ,[row col])
np.tile(A ,[row col])
-
matlab 拼接矩阵
[a(:) b(:) c(:)....n(:)
]np.c_[a.flatten(), b.flatten(), c.flatten(), d..flatten()]
-
conv2(gray_temp1, Gx, 'same');
scipy.ndimage.filters.convolve1d(gray_temp1, Gx, axis=1, mode='constant')
-
circshift(MSCN,[1 0]);
np.roll(MSCN, 1, axis=0)
-
mrdivide(A,B);
# A * B逆 np.matmul(A, np.linalg.inv(B))
-
mtimes(A, B);
# A * B np.matmul(A, B)
-
length(A);
max(A.shape)
-
rgb2hsv
def rgb2hsv(img): """ :param img: 彩色图片 :return: H、S、V 三个通道 """ HSV = cv2.cvtColor(img, cv2.COLOR_RGB2HSV) H, S, V = cv2.split(HSV) return H, S, V
-
// matlab 对图像进行高斯滤波
MSCN_window = fspecial('gaussian',7,7/6);
mu = imfilter(Ig,MSCN_window,'replicate');
def gaussian_filter(img, Hsize=(3, 3), sigma=0.5, boedertype=cv2.BORDER_REPLICATE):
"""
对图像进行高斯滤波
:param img: 待滤波图像
:param Hsize: 核的尺寸
:param sigma: 标准差
:param boedertype: 边界填充模式
:return: 滤波后的图像
"""
r, c = Hsize
gaussian_kernel = np.multiply(cv2.getGaussianKernel(r, sigma), (cv2.getGaussianKernel(c, sigma)).T)
filter_img = cv2.filter2D(img, -1, gaussian_kernel, borderType=boedertype)
return filter_img
im2col(MSCN, [ps ps], 'distinct');
def im2col(mtx, block_size):
mtx_shape = mtx.shape
if mtx_shape[0] % block_size[0] != 0:
sx = mtx_shape[0] // block_size[0] + 1
else:
sx = mtx_shape[0] // block_size[0]
if mtx_shape[1] % block_size[1] != 0:
sy = mtx_shape[1] // block_size[1] + 1
else:
sy = mtx_shape[1] // block_size[1]
result = np.empty((block_size[0] * block_size[1], sx * sy))
for i in range(sy):
for j in range(sx):
Zeros = np.zeros((block_size[0], block_size[1]))
block = mtx[j * block_size[0]: j * block_size[0] + block_size[0], i * block_size[1]: i * block_size[1] + block_size[1]]
if block.shape[0] < Zeros.shape[0] and block.shape[1] == Zeros.shape[1]:
concat = np.zeros((Zeros.shape[0] - block.shape[0], Zeros.shape[1]))
block = np.vstack((block, concat))
if block.shape[1] < Zeros.shape[1] and block.shape[0] == Zeros.shape[0]:
concat = np.zeros((Zeros.shape[0], Zeros.shape[1] - block.shape[1]))
block = np.hstack((block, concat))
if block.shape[0] < Zeros.shape[0] and block.shape[1] < Zeros.shape[1]:
concat = np.zeros((block_size[0], block_size[1]))
concat[:block.shape[0], :block.shape[1]] = block
block = concat
for line in range(block.shape[0]):
Zeros[line, :] = block[line, :]
result[:, i * sx + j] = Zeros.ravel(order='F')
return result
entropy
def entropy(img): rows, cols = img.shape entropy_list = [] for col in range(cols): hist, _ = np.histogram(img[:, col], 256, [0, 256]) noZero_list = [] index = np.where(hist != 0)[0] for idx in index: noZero_list.append(hist[idx]) noZero_array = np.array(noZero_list, dtype=np.float32) noZero_array = noZero_array / rows E = -np.sum(noZero_array * np.log2(noZero_array), axis=0) entropy_list.append(E) return np.array(entropy_list, dtype=np.float32)