Gaussian Mixture Models of an Image's Histogram

后端 未结 1 517
花落未央
花落未央 2020-12-31 14:43

I am attempting to do automatic image segmentation of the different regions of a 2D MR image based on pixel intensity values. The first step is implementing a Gaussian Mixtu

1条回答
  •  一整个雨季
    2020-12-31 15:16

    The issue was with passing the histogram rather than the array of pixel intensities to GaussianMixture.fit gmm = gmm.fit(hist). I also found that a minimum of n_components = 6 is needed to visually fit this particular histogram.

    import numpy as np
    import cv2
    import matplotlib.pyplot as plt
    from sklearn.mixture import GaussianMixture
    
    # Read image
    img = cv2.imread("test.jpg",0)
    
    hist = cv2.calcHist([img],[0],None,[256],[0,256])
    hist[0] = 0     # Removes background pixels
    
    data = img.ravel()
    data = data[data != 0]
    data = data[data != 1]  #Removes background pixels (intensities 0 and 1)
    
    # Fit GMM
    gmm = GaussianMixture(n_components = 6)
    gmm = gmm.fit(X=np.expand_dims(data,1))
    
    # Evaluate GMM
    gmm_x = np.linspace(0,253,256)
    gmm_y = np.exp(gmm.score_samples(gmm_x.reshape(-1,1)))
    
    
    # Plot histograms and gaussian curves
    fig, ax = plt.subplots()
    ax.hist(img.ravel(),255,[2,256], normed=True)
    ax.plot(gmm_x, gmm_y, color="crimson", lw=4, label="GMM")
    
    ax.set_ylabel("Frequency")
    ax.set_xlabel("Pixel Intensity")
    
    plt.legend()
    
    plt.show()
    

    0 讨论(0)
提交回复
热议问题