【发布时间】: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