sagoma/lines.cpp
boyska 40d353a37a powered by spray paint
what doesn't kill you makes you high
2017-01-05 13:40:59 +01:00

63 lines
1.5 KiB
C++

#include<opencv2/opencv.hpp>
int main(int argc, char *argv[])
{
cv::Mat img=cv::imread("files/masckera.png",CV_LOAD_IMAGE_GRAYSCALE);
std::vector< std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(img,contours,hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
cv::drawContours(img,contours,-1,cv::Scalar(255,255,255),5);
if( 1!=contours.size() )
{
std::cout << "non ci piace" << std::endl;
return -1;
}
std::vector<cv::Point> hull;
cv::convexHull(cv::Mat(contours[0]),hull,false);
int i;
int idx[4]={0};
int distance[2]={0};
for( i=0; hull.size()>i; i++ )
{
cv::Point one=hull[i];
cv::Point two=hull[(i+1)%hull.size()];
int d=pow(one.x-two.x,2)+pow(one.y-two.y,2);
if( d>distance[0] )
{
idx[1]=idx[0];
idx[3]=(idx[1]+1)%hull.size();
idx[0]=i;
idx[2]=(idx[0]+1)%hull.size();
distance[1]=distance[0];
distance[0]=d;
}
else if( d>distance[1] )
{
idx[1]=i;
idx[3]=(i+1)%hull.size();
distance[1]=d;
}
}
cv::circle(img,hull[idx[0]],30,255,-1);
cv::circle(img,hull[(idx[0]+1)%hull.size()],30,255,-1);
cv::circle(img,hull[idx[1]],20,255,-1);
cv::circle(img,hull[(idx[1]+1)%hull.size()],20,255,-1);
std::cout << hull.size() << ", " << contours[0].size() << std::endl;
std::cout
<< "(" << idx[0] << "," << distance[0] << ") -- "
<< "(" << idx[1] << "," << distance[1] << ")" << std::endl;
cv::namedWindow("test",CV_WINDOW_NORMAL);
cv::imshow("test",img);
cv::waitKey(0);
return 0;
}