Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #include <opencv2/opencv.hpp>
- using namespace cv;
- #pragma comment(lib, "opencv_world340.lib")
- int main(void)
- {
- Mat cat_img = imread("cat.png", CV_LOAD_IMAGE_GRAYSCALE);
- threshold(cat_img, cat_img, 127, 255, THRESH_BINARY);
- vector<vector<Point> > contours;
- vector<Vec4i> hierarchy;
- findContours(cat_img, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
- vector<vector<Point> > contours_poly(contours.size());
- for (int i = 0; i < contours.size(); i++)
- approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
- Mat cat_contours = Mat::zeros(cat_img.size(), CV_8UC3);
- for (int i = 0; i < contours_poly.size(); i++)
- {
- if(1 == contours_poly[i].size())
- {
- line(cat_contours, contours_poly[i][0], contours_poly[i][0], Scalar(0, 255, 0), 1, 8, 0);
- }
- else if (2 == contours_poly[i].size())
- {
- line(cat_contours, contours_poly[i][0], contours_poly[i][1], Scalar(0, 0, 255), 1, 8, 0);
- }
- else
- {
- for (int j = 0; j < contours_poly[i].size() - 1; j++)
- line(cat_contours, contours_poly[i][j], contours_poly[i][j + 1], Scalar(255, 0, 0), 1, 8, 0);
- line(cat_contours, contours_poly[i][contours_poly[i].size() - 1], contours_poly[i][0], Scalar(255, 0, 0), 1, 8, 0);
- }
- }
- imshow("cat img", cat_contours);
- waitKey(0);
- destroyAllWindows();
- return 0;
- }
Add Comment
Please, Sign In to add comment