openCV 绘制轮廓的更好方法
•浏览 1
openCV better way to draw contours
我已经使用 openCV 网站上提供的代码根据 ROI 直方图检测到手,这是我获得的结果
根据这个结果,我想绘制轮廓,但生成的图像不是我未来处理所需的图像。
我需要做什么才能获得更平滑的轮廓?谢谢!
您的图像有太多"洞"。尝试一些形态
这是 C 代码,但您可以轻松移植到 Python。您可能需要稍微调整一下参数。
#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;
}
如果你关闭你的图片,你会得到更好的结果,像这样:
但是,您可能需要从之前的处理中获得更好的结果。