首页 > 解决方案 > 尝试使用 openCV 和 HoughCircles 分割 DICOM 图像中的圆圈时出错

问题描述

我正在尝试分割 DICOM 图像中的圆圈。我正在尝试使用 opencv 实现霍夫变换。我收到此错误:

cv2.error: OpenCV(4.1.0) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/hough.cpp:1736: error: (-215:Assertion failed) !_image.empty() && _image.type() == CV_8UC1 && (_image.isMat() || _image.isUMat()) in function 'HoughCircles'

代码:

#Segment circle code using openCV
def segment_circles(self): 

    image = np.float(self.image)
    output = image.copy()
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 100)

    if circles is not None:
        circles = np.round(circles[0, :].astype("int"))

        for (x, y, r) in circles:
            cv2.circle(output, (x,y), r, (0, 255, 0), 4)
            cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)

            cv2.imshow("output", np.hstack([image, output]))
            cv2.waitKey(0)
#how self.image is created in another function
self.image = PIL.Image.fromarray(numpy_array)

预先感谢您的帮助。

标签: pythonopencvdicomhough-transform

解决方案


这是一个可用于从磁盘读取 DICOM 文件并执行霍夫变换的工作流程

    import pydicom
    import cv2
    import numpy as np

    # read the DICOM file
    d16=pydicom.read_file('view0010.dcm')

    print(f"original data type={d16.pixel_array.dtype}")

    # rescale original 16 bit image to 8 bit values [0,255]
    x0=d16.pixel_array.min()
    x1=d16.pixel_array.max()
    y0=0
    y1=255.0
    i8=((d16.pixel_array-x0)*((y1-y0)/(x1-x0)))+y0

    # create new array with rescaled values and unsigned 8 bit data type
    o8=i8.astype(np.uint8)

    print(f"rescaled data type={o8.dtype}")

    # do the Hough transform
    h=cv2.HoughCircles(o8, cv2.HOUGH_GRADIENT, 1.2, 100)

    print(f"h={h}")

当我在我的计算机上使用真实的 MR 图像运行它时,这里是输出......

    original data type=int16
    rescaled data type=uint8
    h=[[[172.20001 259.80002 154.92001]
      [319.80002 273.      161.64   ]]]

当然,霍夫变换的结果对你来说会有所不同,但我认为它显示了在真实的 DICOM 图像上运行 cv2 HoughCircles 函数必须做的事情。


推荐阅读