opencv - 如何将 pyplot.contour(..) 转换为等效的 opencv?

标签 opencv matplotlib contour

我有这样一张图片:(original.png)

original image

人体检测结果如下:(detection.png)

detected heatmap

并使用打击代码:

import numpy
import cv2
import matplotlib.pyplot as plt
import numpy as np

original  = cv2.imread('original.png')
detections = cv2.imread('detection.png')
fig = plt.figure(figsize=[12,12])
plt.imshow( original[:,:,::-1] )
plt.contour( detections[:,:,1]/256.,10, linewidths = 1 )
plt.contour( detections[:,:,2]/256.,10, linewidths = 1 )
plt.axis('off') ;
plt.show()

将生成这样的图像: with contours 我试图将其转换为:

plt.contour( detections[:,:,1]/256.,10, linewidths = 1 )

相当于 opencv:

_, contours, _ = cv2.findContours(detections,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

各种组合

retrieve mode + approximation method

但是没有一个给出预期的结果,那么如何解决这个问题呢?

最佳答案

OpenCv findcontours 需要黑白图像。

尝试:

_, thresh = cv2.threshold(detections[:,:,1], 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

_, contours, _ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

关于opencv - 如何将 pyplot.contour(..) 转换为等效的 opencv?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51073947/

相关文章:

c# - 使用 haarcascade_profileface.xml 时出错

python - 使用 matplotlib 调整图例中的线条颜色

python - 绘制不同索引长度的两条线图,在没有数据的情况下断开连接

python - OpenCV变换图像形状变换为给定的轮廓

python - 从二进制掩码裁剪图像

c++ - Opencv C++ 阈值在 absdiff、条码检测后不起作用

python - matplotlib 中的 pcolor

python - 打开CV轮廓-将凹面多边形分成多个凸面多边形

python - 使用 Opencv 获取轮廓内的平均颜色

opencv - HoughCircles param2 的合理搜索范围?