Last active
April 14, 2019 02:40
-
-
Save sailfish009/f55e5a58f2a78f44bf81602c2079e029 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
findContours: | |
https://docs.opencv.org/2.4.13.4/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html | |
approxPolyDP: | |
http://answers.opencv.org/question/71309/how-to-get-the-corners-of-rectangle-shapes/ | |
#include <opencv2/opencv.hpp> | |
#include "opencv2/core/core.hpp" | |
#include "opencv2/imgproc/imgproc.hpp" | |
#include "opencv2/highgui/highgui.hpp" | |
#include <iostream> | |
using namespace cv; | |
using namespace std; | |
vector<vector<Point> > contours; | |
vector<Vec4i> hierarchy; | |
int main(int argc, char **argv) | |
{ | |
Mat mThresh; | |
Mat m=imread("C:/Users/Laurent.PC-LAURENT-VISI/Downloads/rectangle.png",CV_LOAD_IMAGE_GRAYSCALE); | |
Mat mc; | |
threshold(m,mThresh,80,255,THRESH_BINARY); | |
findContours(mThresh,contours,hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_NONE, cv::Point(0,0)); | |
imshow("Image",m); | |
mc = Mat::zeros(m.size(),CV_8UC3); | |
drawContours(mc,contours,0,Scalar(255,0,0),1); | |
vector<Point> approx; | |
double d=0; | |
do | |
{ | |
d=d+1; | |
approxPolyDP(contours[0],approx,d,true); | |
cout << approx.size() << " " <<d<<endl; | |
} | |
while (approx.size()>4); | |
contours.push_back(approx); | |
drawContours(mc,contours,contours.size()-1,Scalar(0,0,255),1); | |
imshow("Ctr",mc); | |
waitKey(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment