opencv - 如何使用opencv填充轮廓线?

标签 opencv

例如,这个矩形的中心是洞。 (白色每个像素值=255,黑色值=0)

enter image description here

但是,我想填补这个漏洞。 (如下图)

enter image description here

如何使用OpenCV按矩形填充孔。

最佳答案

首先找到它的凸包,然后填充它的内部区域:

cv::Mat inputImage = cv::imread("input.jpg", CV_LOAD_IMAGE_GRAYSCALE);
cv::threshold(inputImage, inputImage, 10, 255, 0);

// find non-zero elements
cv::Mat nonZeroCoordinates;
cv::findNonZero(inputImage, nonZeroCoordinates);

cv::vector<cv::Point> points;
for (int i = 0; i < nonZeroCoordinates.total(); i++)
{
    points.push_back(nonZeroCoordinates.at<cv::Point>(i));
}

// Find convex hull
std::vector<int> hull;
cv::convexHull(cv::Mat(points), hull, false);

cv::vector<cv::Point> hullpoints;
int hullcount = (int)hull.size();

for (int i = 0; i < hullcount; i++)
{
    cv::Point pt = points[hull[i]];
    hullpoints.push_back(pt);
}

std::vector<std::vector<cv::Point> > fillContAll;
fillContAll.push_back(hullpoints);

cv::Mat result = cv::Mat::zeros(inputImage.size(), CV_8UC1);
cv::fillPoly(result, fillContAll, cv::Scalar(255));

给定您的原始图像:

enter image description here

这是您的最终结果:

enter image description here

关于opencv - 如何使用opencv填充轮廓线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33645213/

相关文章:

c++ - OpenCV 3.0 无法加载神经网络

c++ - 使用opencv从图像中提取不同大小的矩形

c++ - OpenCV floodFill() 填充未连接的区域

git - 使用预建库安装 OpenCV 与从源文件创建自己的库有什么区别?

python - 如何使用 OpenCV ConnectedComponents 获取图像

c++ - openCV 报错

python - 在 Python 和 C++ 中根据彩色图像计算的 pig 描述符的差异

c++ - OpenCV 错误 : Assertion failed when using fitLine

opencv - 基尼杂质,在 opencv 中生长随机树

iphone - 微笑的瀑布