2

我从 Jupyter Notebook 运行以下代码:

import cv2 as cv
contours, hierarchy = cv.findContours(im, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cnt = contours[4]
cv.drawContours(im, contours, 2, (0, 230, 255), 6)
# Show the image with contours
cv.imshow('Contours', im)
cv.waitKey(0)

(im 是二进制图像)运行后,Jupyter Kernel 死亡。我应该改变什么?

4

1 回答 1

2

所以这里有一个解决方法。TL;DR:您需要使用im = cv.drawContours(im, contours, 2, (0, 230, 255), 6)来保存绘制的轮廓,im = np.expand_dims(im,axis=2).repeat(3,axis=2)以便能够绘制彩色轮廓。以下代码绘制所有轮廓im并使用 显示它matplotlib

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
#im an H X W array.
contours, hierarchy = cv.findContours(im, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
im = np.expand_dims(im, axis=2).repeat(3, axis=2) 
for k, _ in enumerate(contours):
    im = cv.drawContours(im, contours, k, (0, 230, 255), 6)
plt.imshow(im)
于 2020-10-01T19:33:37.023 回答