聚集图像的高斯混合模型

发布于 2025-02-09 05:39:14 字数 2172 浏览 1 评论 0原文

nan pdf 这就是我希望得到的 < a href =“ https://i.sstatic.net/pw1lj.png” rel =“ nofollow noreferrer”>输出我开发了此Python代码,以将图像的高斯混合物聚集。它可以通过图像分割效果很好,并显示图像直方图上的GMM。但是,在直方图上的不同簇上显示不同的分布是错误的。感谢您的帮助。

import os
import matplotlib.pyplot as plt
import numpy as np
import cv2
from scipy import stats


img = cv2.imread("test.tif")

# Convert MxNx3 image into Kx3 where K=MxN
img2 = img.reshape((-1,3))  #-1 reshape means, in this case MxN

from sklearn.mixture import GaussianMixture as GMM

#covariance choices, full, tied, diag, spherical

k = 7
gmm_model = GMM(n_components=k, covariance_type='full').fit(img2)  #tied works better than full
gmm_labels = gmm_model.predict(img2)

#Put numbers back to original shape so we can reconstruct segmented image
original_shape = img.shape
segmented = gmm_labels.reshape(original_shape[0], original_shape[1])
cv2.imwrite("test_segmented.tif")

data = img2.ravel()
data = data[data != 0]
data = data[data != 1]  #Removes background pixels (intensities 0 and 1)
gmm = GMM(n_components = k)
gmm = gmm.fit(X=np.expand_dims(data,1))
gmm_x = np.linspace(0,255,256)
gmm_y = np.exp(gmm.score_samples(gmm_x.reshape(-1,1)))

gmm_model.means_

gmm_model.covariances_

gmm_model.weights_

# Plot histograms and gaussian curves
fig, ax = plt.subplots()
ax.hist(img2.ravel(),255,[2,256], density=True, stacked=True)
ax.plot(gmm_x, gmm_y, color="crimson", lw=2, label="GMM")

ax.set_ylabel("Frequency")
ax.set_xlabel("Pixel Intensity")

plt.legend()
plt.grid(False)
plt.xlim([0, 256])

plt.show()

for m in range(gmm_model.n_components):
    

    pdf = gmm_model.weights_[m] * stats.norm(gmm_model.means_[m, 0],
                                       np.sqrt(gmm_model.covariances_[m, 0])).pdf(gmm_x.reshape(-1,1))
    plt.fill(gmm_x, pdf, facecolor='gray',
             edgecolor='none')
plt.xlim(0, 256)

nan pdf this is what I expect to get output I developed this python code to cluster the Gaussian mixture models for an image. It works fine with the image segmentation and it shows the GMM on the image histogram. However, there is something wrong with showing different distributions on the different clusters on the histogram. Thanks for helping.

import os
import matplotlib.pyplot as plt
import numpy as np
import cv2
from scipy import stats


img = cv2.imread("test.tif")

# Convert MxNx3 image into Kx3 where K=MxN
img2 = img.reshape((-1,3))  #-1 reshape means, in this case MxN

from sklearn.mixture import GaussianMixture as GMM

#covariance choices, full, tied, diag, spherical

k = 7
gmm_model = GMM(n_components=k, covariance_type='full').fit(img2)  #tied works better than full
gmm_labels = gmm_model.predict(img2)

#Put numbers back to original shape so we can reconstruct segmented image
original_shape = img.shape
segmented = gmm_labels.reshape(original_shape[0], original_shape[1])
cv2.imwrite("test_segmented.tif")

data = img2.ravel()
data = data[data != 0]
data = data[data != 1]  #Removes background pixels (intensities 0 and 1)
gmm = GMM(n_components = k)
gmm = gmm.fit(X=np.expand_dims(data,1))
gmm_x = np.linspace(0,255,256)
gmm_y = np.exp(gmm.score_samples(gmm_x.reshape(-1,1)))

gmm_model.means_

gmm_model.covariances_

gmm_model.weights_

# Plot histograms and gaussian curves
fig, ax = plt.subplots()
ax.hist(img2.ravel(),255,[2,256], density=True, stacked=True)
ax.plot(gmm_x, gmm_y, color="crimson", lw=2, label="GMM")

ax.set_ylabel("Frequency")
ax.set_xlabel("Pixel Intensity")

plt.legend()
plt.grid(False)
plt.xlim([0, 256])

plt.show()

for m in range(gmm_model.n_components):
    

    pdf = gmm_model.weights_[m] * stats.norm(gmm_model.means_[m, 0],
                                       np.sqrt(gmm_model.covariances_[m, 0])).pdf(gmm_x.reshape(-1,1))
    plt.fill(gmm_x, pdf, facecolor='gray',
             edgecolor='none')
plt.xlim(0, 256)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埋葬我深情 2025-02-16 05:39:14

我对其进行了编辑,缺少的部分是将直方图放置在开始绘制每个群集归一化曲线之前。希望它会有所帮助!

import os
import matplotlib.pyplot as plt
import numpy as np
import cv2
from scipy import stats


img = cv2.imread("test.tif")

# Convert MxNx3 image into Kx3 where K=MxN
img2 = img.reshape((-1,3))  #-1 reshape means, in this case MxN

from sklearn.mixture import GaussianMixture as GMM

#covariance choices, full, tied, diag, spherical

k = 7
gmm_model = GMM(n_components=k, covariance_type='full').fit(img2)  #tied works better than full
gmm_labels = gmm_model.predict(img2)

#Put numbers back to original shape so we can reconstruct segmented image
original_shape = img.shape
segmented = gmm_labels.reshape(original_shape[0], original_shape[1])
cv2.imwrite("test_s.tif", segmented)

data = img2.ravel()
#data = data[data != 0]
#data = data[data != 1]  #Removes background pixels (intensities 0 and 1)

gmm = GMM(n_components = k)
gmm = gmm.fit(X=np.expand_dims(data,1))
gmm_x = np.linspace(0,255,256)
gmm_y = np.exp(gmm.score_samples(gmm_x.reshape(-1,1)))


gmm_model.means_

gmm_model.covariances_

gmm_model.weights_


# Plot histograms and gaussian curves
fig, ax = plt.subplots()
ax.hist(img2.ravel(),255,[2,256], density=True, stacked=True)
ax.plot(gmm_x, gmm_y, color="crimson", lw=2, label="GMM")

ax.set_ylabel("Frequency")
ax.set_xlabel("Pixel Intensity")

plt.legend()
plt.grid(False)
plt.xlim([0, 256])

plt.show()

for m in range(gmm_model.n_components):
    

    pdf = gmm_model.weights_[m] * stats.norm(gmm_model.means_[m, 0],
                                       np.sqrt(gmm_model.covariances_[m, 0])).pdf(gmm_x.reshape(-1,1))
    

    fig, ax = plt.subplots()
    ax.hist(img2.ravel(),255,[2,256], density=True, stacked=True)
    ax.plot(gmm_x, gmm_y, color="crimson", lw=2, label="GMM")
    plt.fill(gmm_x, pdf, facecolor='gray',
             edgecolor='none')
    plt.xlim(0, 256)
    plt.ylim(0, .06)

I edited it and the missing part was to put the histogram before start plotting each cluster normalized curve. I hope it will be helpful!

import os
import matplotlib.pyplot as plt
import numpy as np
import cv2
from scipy import stats


img = cv2.imread("test.tif")

# Convert MxNx3 image into Kx3 where K=MxN
img2 = img.reshape((-1,3))  #-1 reshape means, in this case MxN

from sklearn.mixture import GaussianMixture as GMM

#covariance choices, full, tied, diag, spherical

k = 7
gmm_model = GMM(n_components=k, covariance_type='full').fit(img2)  #tied works better than full
gmm_labels = gmm_model.predict(img2)

#Put numbers back to original shape so we can reconstruct segmented image
original_shape = img.shape
segmented = gmm_labels.reshape(original_shape[0], original_shape[1])
cv2.imwrite("test_s.tif", segmented)

data = img2.ravel()
#data = data[data != 0]
#data = data[data != 1]  #Removes background pixels (intensities 0 and 1)

gmm = GMM(n_components = k)
gmm = gmm.fit(X=np.expand_dims(data,1))
gmm_x = np.linspace(0,255,256)
gmm_y = np.exp(gmm.score_samples(gmm_x.reshape(-1,1)))


gmm_model.means_

gmm_model.covariances_

gmm_model.weights_


# Plot histograms and gaussian curves
fig, ax = plt.subplots()
ax.hist(img2.ravel(),255,[2,256], density=True, stacked=True)
ax.plot(gmm_x, gmm_y, color="crimson", lw=2, label="GMM")

ax.set_ylabel("Frequency")
ax.set_xlabel("Pixel Intensity")

plt.legend()
plt.grid(False)
plt.xlim([0, 256])

plt.show()

for m in range(gmm_model.n_components):
    

    pdf = gmm_model.weights_[m] * stats.norm(gmm_model.means_[m, 0],
                                       np.sqrt(gmm_model.covariances_[m, 0])).pdf(gmm_x.reshape(-1,1))
    

    fig, ax = plt.subplots()
    ax.hist(img2.ravel(),255,[2,256], density=True, stacked=True)
    ax.plot(gmm_x, gmm_y, color="crimson", lw=2, label="GMM")
    plt.fill(gmm_x, pdf, facecolor='gray',
             edgecolor='none')
    plt.xlim(0, 256)
    plt.ylim(0, .06)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文