weixin_42193785 2020-02-27 18:25
浏览 358

python代码修改指导意见

现有python的PFCM算法的代码,已经能对示例图进行分割,示例图及其分割结果已附上,请教一下如何对代码进行修改,从而能对附上的大脑mri进行灰质白质脑脊液分割,谢谢图片说明图片说明图片说明代包也已经附上,望大佬能给点指点,万分感谢
import numpy as np

def pfcm(data, c, expo=2, max_iter=1000, min_impro=0.005, a=1, b=4, nc=3):
"""

Possiblistic Fuzzy C-Means Clustering Algorithm


Parameters :

  • data: Dataset to be clustered, with size M-by-N,
    where M is the number of data points
    and N is the number of coordinates for each data point.

  • c : Number of clusters

  • expo : exponent for the U matrix (default = 2)

  • max_iter : Maximum number of iterations (default = 1000)

  • min_impor : Minimum amount of imporvement (default = 0.005)

  • a : User-defined constant a (default = 1)

  • b : User-defined constant b that should be
    greater than a (default = 4)

  • nc : User-defined constant nc (default = 2)


The clustering process stops when the maximum number of iterations is
reached, or when objective function improvement or the maximum centers
imporvement between two consecutive iterations is less
than the minimum amount specified.


Return values :

  • cntr : The clusters centers

  • U : The C-Partionned Matrix (used in FCM)

  • T : The Typicality Matrix (used in PCM)

  • obj_fcn : The objective function for U and T


"""
obj_fcn = np.zeros(shape=(max_iter, 1))
ni = np.zeros(shape=(c, data.shape[0]))
U = initf(c, data.shape[0])
T = initf(c, data.shape[0])
cntr = np.random.uniform(low=np.min(data), high=np.max(data), size=(
c, data.shape[1]))
for i in range(max_iter):
current_cntr = cntr
U, T, cntr, obj_fcn[i], ni = pstepfcm(
data, cntr, U, T, expo, a, b, nc, ni)
if i > 1:
if abs(obj_fcn[i] - obj_fcn[i-1]) < min_impro:
break
elif np.max(abs(cntr - current_cntr)) < min_impro:
break
return cntr, U, T, obj_fcn

def pstepfcm(data, cntr, U, T, expo, a, b, nc, ni):
mf = np.power(U, expo)
tf = np.power(T, nc)
tfo = np.power((1-T), nc)
cntr = (np.dot(a*mf+b*tf, data).T/np.sum(
a*mf+b*tf, axis=1).T).T
dist = pdistfcm(cntr, data)
obj_fcn = np.sum(np.sum(np.power(dist, 2)*(a*mf+b*tf), axis=0)) + np.sum(
ni*np.sum(tfo, axis=0))
ni = mf*np.power(dist, 2)/(np.sum(mf, axis=0))
tmp = np.power(dist, (-2/(nc-1)))
U = tmp/(np.sum(tmp, axis=0))
tmpt = np.power((b/ni)*np.power(dist, 2), (1/(nc-1)))
T = 1/(1+tmpt)
return U, T, cntr, obj_fcn, ni

def initf(c, data_n):
A = np.random.random(size=(c, data_n))
col_sum = np.sum(A, axis=0)
return A/col_sum

def pdistfcm(cntr, data):
out = np.zeros(shape=(cntr.shape[0], data.shape[0]))
for k in range(cntr.shape[0]):
out[k] = np.sqrt(np.sum((np.power(data-cntr[k], 2)).T, axis=0))
return out

def pfcm_predict(data, cntr, expo=2, a=1, b=4, nc=3):
"""

Possiblistic Fuzzy C-Means Clustering Prediction Algorithm


Parameters :

  • data: Dataset to be clustered, with size M-by-N,
    where M is the number of data points
    and N is the number of coordinates for each data point.

  • cntr : centers of the dataset previoulsy calculated

  • expo : exponent for the U matrix (default = 2)

  • a : User-defined constant a (default = 1)

  • b : User-defined constant b that should be
    greater than a (default = 4)

  • nc : User-defined constant nc (default = 2)


The algortihm predicts which clusters the new dataset belongs to


Return values :

  • new_cntr : The new clusters centers

  • U : The C-Partionned Matrix (used in FCM)

  • T : The Typicality Matrix (used in PCM)

  • obj_fcn : The objective function for U and T


"""
dist = pdistfcm(cntr, data)
tmp = np.power(dist, (-2/(nc-1)))
U = tmp/(np.sum(tmp, axis=0))
mf = np.power(U, expo)
ni = mf*np.power(dist, 2)/(np.sum(mf, axis=0))
tmpt = np.power((b/ni)*np.power(dist, 2), (1/(nc-1)))
T = 1/(1+tmpt)
tf = np.power(T, nc)
tfo = np.power((1-T), nc)
new_cntr = (np.dot(a*mf+b*tf, data).T/np.sum(
a*mf+b*tf, axis=1).T).T
obj_fcn = np.sum(np.sum(np.power(dist, 2)*(a*mf+b*tf), axis=0)) + np.sum(
ni*np.sum(tfo, axis=0))
return new_cntr, U, T, obj_fcn
import numpy as np
import matplotlib.image as mpimg
from PFCM import pfcm
import time


def creat_image(labels, centers):
    """ color each pixel with the cluster's center color that it belongs to """
    img = np.zeros(shape=(labels.shape[0], labels.shape[1], 3))
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            img[i, j] = centers[labels[i, j]]
    if(img.max() > 1):
        img /= 255
    mpimg.imsave('Image Result_3.bmp', img) # image result
    return img


clusters = 3 # define the number of clusters
img = mpimg.imread('78.bmp') # read the test image
start_time = time.clock() # start calculating the execution time
centers, U, T, obj_fcn = pfcm(
    img.reshape(img.shape[0]*img.shape[1], img.shape[2]), clusters) # calling the pfcm function on the image after reshaping it
elapsed_time = time.clock() - start_time # end calculating the execution time
labels = np.argmax(U, axis=0).reshape(img.shape[0], img.shape[1]) # assing each pixel to its closest cluster
creat_image(labels, centers) # creat an image with the assigned clusters
print(f'elapsed time : {round(elapsed_time, 3)} seconds') # printing the execution time
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题