0
votes

Im trying to fenestrated DICOM images through the following code. Unfortunately im running into the error, that tells im using the wrong np array format.

import cv2

ds_copy_2=np.copy(ds_copy)
#ds_copy_2 = ds_copy_2.astype('uint8')
kernel = np.ones((5,5),np.uint8) # standart version

for idx,img in enumerate(ds_copy_2):
    
    ds_copy_2[idx]=cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
    ds_copy_2[idx]=cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
    
    ds_copy_2[idx]=cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
    ds_copy_2[x,y,w,h]=cv2.boundingRect(img)
    
    roi=ds_copy_2[y:y+h,x:x+w]
    
    cv2.imshow(roi)
    
    cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0),1)
    
def morph_op(slice_index):
    plt.figure(figsize=(8,8))
    plt.axis('off')
    plt.imshow(ds_copy_2[slice_index], cmap=plt.cm.gray)
    return

interact(morph_op,
         slice_index=widgets.IntSlider(min=0, max=len(imgs)-1,step=1, value=233, description='Slice', continuous_update=False))

The error i get:

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
Input In [9], in <cell line: 7>()
      9 ds_copy_2[idx]=cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
     10 ds_copy_2[idx]=cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
---> 12 ds_copy_2[idx]=cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
     14 ds_copy_2[x,y,w,h]=cv2.boundingRect(img)
     16 roi=ds_copy_2[y:y+h,x:x+w]

error: OpenCV(4.5.4) ../modules/imgproc/src/contours.cpp:197: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function 'cvStartFindContours_Impl'

Sadly when i try to use another array format like the 8Bit one i get the following:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [10], in <cell line: 7>()
      9 ds_copy_2[idx]=cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
     10 ds_copy_2[idx]=cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
---> 12 ds_copy_2[idx]=cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
     14 ds_copy_2[x,y,w,h]=cv2.boundingRect(img)
     16 roi=ds_copy_2[y:y+h,x:x+w]

ValueError: could not broadcast input array from shape (2) into shape (512,512)

Any help would be really appreciated! If there is any missing information just let me know!