2

I am working on 2D medical images for segmentation purpose, where I implement fuzzy c-means. I get the error "ValueError: sequence too large; cannot be greater than 32". I need to show a segmentation of whole image through fuzzy c-means. Here is the code, the pic variable dimension is 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)

The error:

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"
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Uzair
  • 33
  • 9
  • Please copy-paste the full error message, with the stack trace. It gives important information that is helpful in finding out what the problem is. – Cris Luengo Sep 03 '21 at 14:10
  • If your array is 512x512x3, then you have a 2D color image, not a 3D image. The image is 2D because the pixels span a domain in two dimensions. – Cris Luengo Sep 03 '21 at 14:12
  • @CrisLuengo Sorry for my mistake, the 3 actually represents a channel not a dimension and i already add the trace above. – Uzair Sep 03 '21 at 14:19
  • @CrisLuengo now i just need to get an output of segmented image. – Uzair Sep 03 '21 at 14:21

1 Answers1

0

You are attempting to reshape one image to the shape of another:

np.reshape(new_img,pic_n)

The second argument should be a shape, not an image. It should read:

np.reshape(new_img,pic_n.shape)

I don't have the ability to test this code right now, but I guess it should read something like this:

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)
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • still have error "ValueError: cannot reshape array of size 6 into shape (262144,3)" – Uzair Sep 03 '21 at 15:13
  • Well, yes, if you generate only 6 values, you cannot reshape them into a 512x512x3 array. – Cris Luengo Sep 03 '21 at 15:17
  • I think you need to transpose the input to `skfuzzy.cluster.cmeans()`, so that the length 3 dimension is the 1st dimension, and the samples (pixels) are along the 2nd dimension. You should get back a `u` that is your image, transpose it and then reshape it to your *original* image size (`pic.shape`, not `pic_n.shape`). – Cris Luengo Sep 03 '21 at 15:24
  • then what should i do to reshape in original image dimension and how should i show their output as a segmented image in my code? can you do that? if possible, because i am new to image processing. – Uzair Sep 03 '21 at 15:26