尝试使用 openCV 和 HoughCircles 分割 DICOM 图像中的圆圈时出错
Error when trying to segment circles in DICOM image using openCV and HoughCircles
我正在尝试分割 DICOM 图像中的圆圈。我正在尝试使用 opencv 实现 hough 变换。我收到此错误:
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)
预先感谢您的帮助。
好吧,我想我会在“gray = ...”行之后添加几行代码来单独测试这些条件中的每一个。你确定 gray().isEmpty() 真的是假的吗?只需花一两分钟找出哪些条件未通过验证测试。
图像是什么模态?
请参阅 Wilf 的回答以进行正确修复。
原始代码的快速修复:
def hough_circles(self):
#load image
img = self.imager.values[self.imager.index, :, :]
image8 = np.uint8(img)
output = image8.copy()
#apply hough transform
circles = cv2.HoughCircles(image8, cv2.HOUGH_GRADIENT, 1.2, 100)
#place circles and cente rectangle on image
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([image8, output]))
cv2.waitKey(0)
这是一个工作流程,可用于从磁盘读取 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 ]]]
当然,Hough 变换的结果对您来说会有所不同,但我认为它显示了必须对 运行 真实 DICOM 图像上的 cv2 HoughCircles 函数执行的操作。
我正在尝试分割 DICOM 图像中的圆圈。我正在尝试使用 opencv 实现 hough 变换。我收到此错误:
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)
预先感谢您的帮助。
好吧,我想我会在“gray = ...”行之后添加几行代码来单独测试这些条件中的每一个。你确定 gray().isEmpty() 真的是假的吗?只需花一两分钟找出哪些条件未通过验证测试。
图像是什么模态?
请参阅 Wilf 的回答以进行正确修复。
原始代码的快速修复:
def hough_circles(self):
#load image
img = self.imager.values[self.imager.index, :, :]
image8 = np.uint8(img)
output = image8.copy()
#apply hough transform
circles = cv2.HoughCircles(image8, cv2.HOUGH_GRADIENT, 1.2, 100)
#place circles and cente rectangle on image
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([image8, output]))
cv2.waitKey(0)
这是一个工作流程,可用于从磁盘读取 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 ]]]
当然,Hough 变换的结果对您来说会有所不同,但我认为它显示了必须对 运行 真实 DICOM 图像上的 cv2 HoughCircles 函数执行的操作。