【问题标题】:Retinal Fluid Segmentation Using Fuzzy Level Set Using OpenCV Python使用 OpenCV Python 使用模糊水平集进行视网膜流体分割
【发布时间】:2020-12-22 17:17:05
【问题描述】:

我正在研究视网膜 OCT 扫描,并希望使用模糊水平集方法分割视网膜液。为此,我将输入扫描转换为灰度,并将其传递给模糊级别的 Chan-Vese 分割算法。应用此分割后,我想将输出传递给findContours() 方法进行边界检测。

图像输入和应用Chan-Vese分割算法的代码如下:

from skimage.segmentation import chan_vese
img = cv2.imread(img_name)
md_img = apply_median_filter(img)
gray = cv2.cvtColor(md_img, cv2.COLOR_BGR2GRAY)

chv = chan_vese(gray, mu=0.25, lambda1=1, lambda2=1, tol=1e-3, max_iter=200,
               dt=0.5, init_level_set="checkerboard", extended_output=True)

ls_img = chv[1].copy()
out_img2, areas = find_ret_contours(ls_img)

这里输入的图片ls_img的形状是(390, 508),肯定是灰度图。

这里调用的函数定义如下:

def find_ret_contours(gray):
  rows, cols = gray.shape
  out_img = md_img.copy()
  **contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)**
  areas = []
  for cntr in contours:
    flag = 1
    area = cv2.contourArea(cntr)
    areas.append(area)

    if area < 200 or area > 200:
      out_img = cv2.drawContours(out_img, [cntr], 0, (0,255,0), 3) ## -1 indicates drawing ALL, then the color

  return out_img, areas

错误跟踪如下:

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-79-1c419739b649> in <module>()
      2 #ls_img = np.array(ls_img, dtype=np.uint8)
      3 #gray_ls = cv2.cvtColor(ls_img, cv2.COLOR_BGR2GRAY)
----> 4 out_img2, areas = find_ret_contours(ls_img)
      5 #plt.imshow(cv[1])
      6 #ls_img.shape

<ipython-input-72-8f79b475aa3b> in find_ret_contours(gray)
      2   rows, cols = gray.shape
      3   out_img = md_img.copy()
----> 4   contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
      5   areas = []
      6   for cntr in contours:

error: OpenCV(4.1.2) /io/opencv/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'

【问题讨论】:

  • 你能在cv2.findContours之前print(gray.dtype, gray.shape)吗?
  • @ZdaR float64 (390, 508)
  • 问题是ls_img 变量包含负值。 findContours 方法适用于图像,其中每个像素值介于 0 - 255 之间。

标签: python opencv image-processing image-segmentation opencv-contour


【解决方案1】:

正如其中一个 cmets 中正确提到的,问题是由于 ls_img 中的负值造成的。因此,通过在0-255 范围内缩放图像中的所有值并取它们的绝对值来解决该问题。以下是更新后的代码:

data = chv[1].copy()

im_max = 255
data = abs(data.astype(np.float64) / data.max())
data = im_max * data # Now scale by 255
ls_img = data.astype(np.uint8)

temp = apply_k_mean_clustering(ls_img)
out_img2, areas = find_ret_contours(temp)

【讨论】:

    猜你喜欢
    • 2020-10-16
    • 2022-07-06
    • 2017-12-15
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    相关资源
    最近更新 更多