高斯混合模型适合Python和sklearn太慢了有什么选择吗?

2024-07-05 10:34:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在RGB图像上使用高斯混合模型,因此数据集相当大。这需要实时运行(从网络摄像头)。我首先用Matlab编写了这个代码,并且我能够为1729  866的图像实现0.5秒的运行时间。最终应用程序的图像将更小,因此计时将更快。在

但是,我需要在最终的应用程序中使用Python和OpenCV来实现这一点(我需要它在嵌入式板上运行)。我翻译了我所有的代码sklearn.mixture.GMM公司在Matlab中替换fitgmdist。计算GMM模型本身的代码行只需7.7e-05秒,而适合模型的代码行则需要19秒。我尝试过其他类型的协方差,比如“diag”或“spherel”,时间确实减少了一点,但结果更糟,时间仍然不够好,甚至不够接近。在

我想知道是否还有其他库可以使用,或者是否值得将函数从Matlab转换为Python。在

下面是我的例子:

import cv2
import numpy as np
import math
from sklearn.mixture import GMM

im = cv2.imread('Boat.jpg');

h, w, _ = im.shape;       # Height and width of the image

# Extract Blue, Green and Red
imB = im[:,:,0]; imG = im[:,:,1]; imR = im[:,:,2];

# Reshape Blue, Green and Red channels into single-row vectors
imB_V = np.reshape(imB, [1, h * w]);
imG_V = np.reshape(imG, [1, h * w]);
imR_V = np.reshape(imR, [1, h * w]);

# Combine the 3 single-row vectors into a 3-row matrix
im_V =  np.vstack((imR_V, imG_V, imB_V));

# Calculate the bimodal GMM
nmodes = 2;
GMModel = GMM(n_components = nmodes, covariance_type = 'full', verbose = 0, tol = 1e-3)
GMModel = GMModel.fit(np.transpose(im_V))

非常感谢你的帮助


Tags: andthe代码模型图像importimgnp