Created
April 16, 2018 23:41
-
-
Save sjhalayka/19cc683debf025792f1f237117331e52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment