Toy*_*oyo 1 c++ opencv edge-detection
我正在寻找一种算法,可以对仅包含两个不同RGB值的理想图像进行外部和内部轮廓检测。
下面是我要处理的图像的典型示例(第一个),下一个图像是我自己显示的图像,该图像显示了我期望的结果。
最后一个是从通过在现有的演示软件制作OpenCV的坎尼检测结果OpenCV的演示软件。
canny算法不能令人满意,因为它使形状(尤其是角)变得过于平滑。
是否有任何优雅的算法可以提供与第二张图片相同的结果?
轮廓提取是最简单的方法:
int main(int argc, char* argv[])
{
cv::Mat input = cv::imread("C:/StackOverflow/Input/ContourExtraction.png");
cv::Mat mask;
// create a perfect mask: Easy if you know the 2 colors present in your image:
cv::inRange(input, cv::Scalar(100, 0, 0), cv::Scalar(255, 255, 255), mask);
cv::imshow("mask", mask);
std::vector<std::vector<cv::Point> > contours; // contour points
std::vector<cv::Vec4i> hierarchy; // this will give you the information whether it is an internal or external conotour.
// contour extraction: This will alter the input image, so if you need it later use mask.clone() instead
findContours(mask, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE, cv::Point(0, 0)); // use different CV_CHAIN_APPROX_ if you dont need ALL the points but only the ones that dont lie on a common line
// output images:
cv::Mat contoursExternal = input.clone();
cv::Mat contoursInternal = input.clone();
cv::Mat contoursAll = cv::Mat::zeros(input.size(), CV_8UC1);
// draw contours
for (unsigned int i = 0; i < contours.size(); ++i)
{
cv::drawContours(contoursAll, contours, i, cv::Scalar::all(255), 1);
if (hierarchy[i][3] != -1) cv::drawContours(contoursInternal, contours, i, cv::Scalar::all(255), 1);
else cv::drawContours(contoursExternal, contours, i, cv::Scalar::all(255), 1);
}
cv::imshow("internal", contoursInternal);
cv::imshow("external", contoursExternal);
cv::imshow("all", contoursAll);
cv::imshow("input", input);
cv::waitKey(0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给出以下结果:
外部轮廓:
内部轮廓:
结果掩码: