user6092483
user6092483

Reputation: 5

dummy mask for sample DICOM Image

I am trying to create a dummy mask on a sample DICOM Image using pydicom, Opencv and numpy

Getting error -

Traceback (most recent call last):
  File "mymask.py", line 22, in <module>
    cv2.fillConvexPoly(mask, np.array(i), 1)
cv2.error: OpenCV(4.5.5) /Users/runner/work/opencv-python/opencv-python/opencv/modules/imgproc/src/drawing.cpp:2374: error: (-215:Assertion failed) points.checkVector(2, CV_32S) >= 0 in function 'fillConvexPoly'

Here is the code that I am trying with -

import numpy as np
import cv2 
import matplotlib.pyplot as plt
import copy
import pydicom
from pydicom.uid import ExplicitVRLittleEndian

ds = pydicom.dcmread("CR-MONO1-10-chest.dcm", force=True)
ds.file_meta.TransferSyntaxUID = ExplicitVRLittleEndian

mask = np.zeros((440, 440), dtype=np.int16)
polys = np.array([[235,224], [274,248], [285,281], [271,311], [239,308]], dtype=np.int16)

mask_list = [] 
for i in polys:
    cv2.fillConvexPoly(mask, np.array(i), 1)
    maski = copy.deepcopy(mask)
    mask_list.append(maski)
    
mask_list[0].astype('int16')
dcm_sample.PixelData = mask_list[0].tobytes()
dcm_sample.save_as("001.dcm")
plt.imshow(dcm_sample.pixel_array) 
plt.show()

Upvotes: 0

Views: 396

Answers (1)

J.D.
J.D.

Reputation: 4561

CV_32S in the error means the expected datatype is np.int32

Also, you're iterating over the poly - meaning you're calling fillConvexPoly on single points. You have to call it on the list of points.

The following should work:

mask = np.zeros((440, 440), dtype=np.int32)
polys = np.array([[235,224], [274,248], [285,281], [271,311], [239,308]], dtype=np.int32)
cv2.fillConvexPoly(mask, polys, 1)

Upvotes: 1

Related Questions