样本 DICOM 图像的虚拟掩码

dummy mask for sample DICOM Image

我正在尝试使用 pydicom、Opencv 和 numpy 在示例 DICOM 图像上创建虚拟掩码

获取错误-

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'

这是我正在尝试的代码 -

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()
错误中的

CV_32S 表示预期的数据类型为 np.int32

此外,您正在遍历多边形 - 这意味着您在单个点上调用 fillConvexPoly。你必须在点数列表上调用它。

以下应该有效:

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)