【发布时间】:2019-09-17 05:39:17
【问题描述】:
我正在尝试基于 MathWorks 的示例在 Python 中使用 K-means 实现颜色/图像分割:
https://nl.mathworks.com/help/images/ref/imsegkmeans.html
使用 (R,G,B) 值作为特征集,我得到以下结果:
但是,如果我们将纹理信息(使用 Gabor 过滤器)和像素位置信息 (x,y) 添加到特征集中,这可以得到改善。
结果:
对于这个结果,我没有使用 (R,G,B) 值,因为狗的颜色与瓷砖大致相同。我使用的是灰度图像,24 个 Gabor 过滤器扩展了像素坐标。
很遗憾,结果不如 Mathworks 中的这些:
目标是使用颜色/纹理分割将背景与对象分开。
您有改进的想法吗?非常感谢!
# Based on https://www.mathworks.com/help/images/ref/imsegkmeans.html
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn import preprocessing
# Building some gabor kernels to filter image
orientations = [0.0, np.pi / 2, np.pi, 3 * np.pi / 2]
wavelengths = [3, 6, 12, 24, 48, 96]
def build_gabor_kernels():
filters = []
ksize = 40
for rotation in orientations:
for wavelength in wavelengths:
kernel = cv.getGaborKernel((ksize, ksize), 4.25, rotation, wavelength, 0.5, 0, ktype=cv.CV_32F)
filters.append(kernel)
return filters
image = cv.imread('./kobi.png')
rows, cols, channels = image.shape
# Resizing the image.
# Full image is taking to much time to process
image = cv.resize(image, (int(cols * 0.5), int(rows * 0.5)))
rows, cols, channels = image.shape
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
gaborKernels = build_gabor_kernels()
gaborFilters = []
for (i, kernel) in enumerate(gaborKernels):
filteredImage = cv.filter2D(gray, cv.CV_8UC1, kernel)
# Blurring the image
sigma = int(3*0.5*wavelengths[i % len(wavelengths)])
# Sigma needs to be odd
if sigma % 2 == 0:
sigma = sigma + 1
blurredImage = cv.GaussianBlur(filteredImage,(int(sigma),int(sigma)),0)
gaborFilters.append(blurredImage)
# numberOfFeatures = 1 (gray color) + number of gabor filters + 2 (x and y)
numberOfFeatures = 1 + len(gaborKernels) + 2
# Empty array that will contain all feature vectors
featureVectors = []
for i in range(0, rows, 1):
for j in range(0, cols, 1):
vector = [gray[i][j]]
for k in range(0, len(gaborKernels)):
vector.append(gaborFilters[k][i][j])
vector.extend([i+1, j+1])
featureVectors.append(vector)
# Some example results:
# featureVectors[0] = [164, 3, 10, 255, 249, 253, 249, 2, 43, 255, 249, 253, 249, 3, 10, 255, 249, 253, 249, 2, 43, 255, 249, 253, 249, 1, 1]
# featureVectors[1] = [163, 3, 17, 255, 249, 253, 249, 2, 43, 255, 249, 253, 249, 3, 17, 255, 249, 253, 249, 2, 43, 255, 249, 253, 249, 1, 2]
# Normalizing the feature vectors
scaler = preprocessing.StandardScaler()
scaler.fit(featureVectors)
featureVectors = scaler.transform(featureVectors)
kmeans = KMeans(n_clusters=2, random_state=170)
kmeans.fit(featureVectors)
centers = kmeans.cluster_centers_
labels = kmeans.labels_
result = centers[labels]
# Only keep first 3 columns to make it easy to plot as an RGB image
result = np.delete(result, range(3, numberOfFeatures), 1)
plt.figure(figsize = (15,8))
plt.imsave('test.jpg', result.reshape(rows, cols, 3) * 100)
【问题讨论】:
-
嗨,克里斯,不完全是。我正在尝试在没有 Matlab 函数的情况下在 Python 中实现 Matlab 示例,而是使用 OpenCV。他们的示例运行正常。
-
在评论之前我没有注意标签或您的代码。 ://
标签: python matlab opencv scikit-learn k-means