How to use approxPolyDP to close contours
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:
And I apply canny, then find contours.
How can I close those contour curves?
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
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<vector<Point> > contours;
vector<Vec4i> 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<vector<Point> > approx;
approx.resize(contours.size());
for (intk = 0; k < contours.size(); k++)
approxPolyDP(Mat(contours[k]), approx[k], 3, true);