如何使用 OpenCV 和 Python 在写入圆形对象内用白色填充黑色区域? [英] How to fill black region by white inside a write circular shaped object using OpenCV and Python?

查看:144
本文介绍了如何使用 OpenCV 和 Python 在写入圆形对象内用白色填充黑色区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测写入圆形或半圆形对象内的黑色区域,然后用白色填充它.在这里,我在下面展示了两个输入图像.

I would like to detect black region inside a write circular or semi circular shaped object and then fill it with white color. Here, I have shown two input images below.

这两个图像有一些被白色包围的黑色区域.将这些黑色转换成白色,就可以计算出整个物体的周长.例如:我得到了一张图片(下图),我可以从中计算周长.

This two images have some black region surrounded by white. It these black can be converted to white, the perimeter of the whole object can be calculated. For example: I got one image (below) from where I could calculate perimeter.

OpenCVpython 中是否有任何函数或方法用于此操作?

Are there any function or method in OpenCV and python for this operation?

推荐答案

你可以试试convexHull()里面的怎么用(代码是C++的,但是可以考虑步骤和用 Python 实现)

You can try the convexHull() there how to use it (the code is in C++, but you can consider the steps and implement it in Python)

cv::namedWindow("origin", cv::WINDOW_FREERATIO);
cv::namedWindow("result", cv::WINDOW_FREERATIO);
cv::Mat img = cv::imread(R"(ObumF.png)");

cv::Mat gray;
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);
cv::threshold(gray, gray, 100, 255, cv::THRESH_BINARY);

std::vector<std::vector<cv::Point> > contours;
cv::findContours(gray, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
std::vector<std::vector<cv::Point> > convexHulls(contours.size());

for (unsigned int i(0); i<contours.size(); i++) {
    cv::convexHull(contours[i], convexHulls[i]);
}
cv::imshow("origin", img);

cv::drawContours(img, convexHulls, -1, cv::Scalar(255, 255, 255), -1);
cv::imshow("result", img);

cv::waitKey();

这是输出:

希望有帮助!

这篇关于如何使用 OpenCV 和 Python 在写入圆形对象内用白色填充黑色区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆