关于 python:openCV 绘制轮廓的更好方法
openCV better way to draw contours
我已经使用 openCV 网站上提供的代码根据 ROI 直方图检测到手,这是我获得的结果

根据这个结果,我想绘制轮廓,但生成的图像不是我未来处理所需的图像。

我需要做什么才能获得更平滑的轮廓?谢谢!
相关讨论
- 所以你的问题不是关于如何绘制轮廓而是如何检测轮廓?恕我直言,图像中的轮廓绘制得很完美......你想要的是某种"凹壳"方法。也许你可以从"凸壳"开始
您的图像有太多"洞"。尝试一些形态
这是 C 代码,但您可以轻松移植到 Python。您可能需要稍微调整一下参数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <opencv2\\opencv.hpp> using namespace cv; int main() { Mat3b img = imread("path_to_image"); Mat1b binary; cvtColor(img, binary, CV_BGR2GRAY); threshold(binary, binary, 1, 255, THRESH_BINARY); Mat1b morph; Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(11,11)); morphologyEx(binary, morph, MORPH_CLOSE, kernel, Point(-1,-1), 3); vector<vector<Point>> contours; findContours(morph.clone(), contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); /// Draw contours Mat3b draw = img.clone(); for (int i = 0; i < contours.size(); i++) { drawContours(draw, contours, i, Scalar(0,0,255), 2); } return 0; } |
如果你关闭你的图片,你会得到更好的结果,像这样:

但是,您可能需要从之前的处理中获得更好的结果。