0

我正在处理用于分割目的的 2D 医学图像,我在其中实现了模糊 c 均值。我收到错误“ValueError:序列太大;不能大于 32”。我需要通过模糊 c 均值显示整个图像的分割。这是代码,图片变量尺寸为512x512x3:

from skimage.color import rgb2gray
import numpy as np
from math import log10, sqrt
import cv2
import matplotlib.pyplot as plt
from PIL import Image
import skfuzzy as fuzz


pic = cv2.imread('E:/volumes/Images/corona_4.png')
pic = cv2.cvtColor(pic, cv2.COLOR_BGR2RGB)
cv2.imshow("CT Scan", pic)
cv2.waitKey(0)
cv2.destroyAllWindows()

pic_n = pic.reshape(pic.shape[0]*pic.shape[1], pic.shape[2])
pic_n.shape

def change_color_fuzzycmeans(cluster_membership, clusters):
    img = []
    for pix in cluster_membership.T:
        img.append(clusters[np.argmax(pix)])
    return img

cntr, u, u0, d, jm, p, fpc =fuzz.cluster.cmeans(pic_n,2,2,0.005,100)

new_img = change_color_fuzzycmeans(u,cntr)
fuzzy_img = np.reshape(new_img,pic_n).astype(np.uint8)

错误:

Traceback (most recent call last):

  File "<ipython-input-16-e78a9da752f4>", line 2, in <module>
    fuzzy_img = np.reshape(new_img,pic_n).astype(np.uint8)

  File "C:\Users\User\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 292, in reshape
    return _wrapfunc(a, 'reshape', newshape, order=order)

  File "C:\Users\User\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 66, in _wrapfunc
    return _wrapit(obj, method, *args, **kwds)

  File "C:\Users\User\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 46, in _wrapit
    result = getattr(asarray(obj), method)(*args, **kwds)

ValueError: sequence too large; cannot be greater than 32"
4

1 回答 1

0

您正在尝试将一个图像重塑为另一个图像的形状:

np.reshape(new_img,pic_n)

第二个参数应该是一个形状,而不是一个图像。它应该是:

np.reshape(new_img,pic_n.shape)

我现在没有能力测试这段代码,但我想它应该是这样的:

data = pic.reshape(pic.shape[0]*pic.shape[1], pic.shape[2]).transpose()
cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(data, 2, 2, 0.005, 100)
output = u.transpose().reshape(pic.shape)
于 2021-09-03T14:57:19.797 回答