Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 41027

How to use approxPolyDP to close contours

$
0
0
Hi, I would like to use approxPoly to close contours. But I am not quite sure about how it works. I am using OpenCV 2.4.11. So I have two images: ![image description](/upfiles/14462552767692222.png) ![image description](/upfiles/1446255311546153.png) And I apply canny, then find contours. ![image description](/upfiles/14462553636876007.png) ![image description](/upfiles/14462553718997857.png) How can I close those contour curves? #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include #include using namespace cv; RNG rng(12345); int main() { Mat src, srcGray, srcBlur, srcThresh, srcCanny; src = imread("source.png", 1); cvtColor(src, srcGray, CV_BGR2GRAY); blur(srcGray, srcBlur, Size(3, 3)); double otsu; otsu = threshold(srcBlur, srcThresh, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU); Canny(srcBlur, srcCanny, otsu, otsu * 2, 3, true); vector> contours; vector hierarchy; findContours(srcCanny, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); Mat drawing = Mat::zeros(srcCanny.size(), CV_8UC3); for (int i = 0; i< contours.size(); i++) { Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point()); } imshow("Contours", drawing); cvWaitKey(); return 0; } I tried something like this but it made it didn't help at all. vector> approx; approx.resize(contours.size()); for (intk = 0; k < contours.size(); k++) approxPolyDP(Mat(contours[k]), approx[k], 3, true);

Viewing all articles
Browse latest Browse all 41027

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>