问题
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.
Are there any function or method in OpenCV
and python
for this operation?
回答1:
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();
And this is the output:
Hope it helps!
来源:https://stackoverflow.com/questions/54993054/how-to-fill-black-region-by-white-inside-a-write-circular-shaped-object-using-op