我从这段代码绘制边界
const cv::Point* p = &squares[i][0];int n = (int)squares[i].size();polylines(image,&p,&n,1,true,Scalar(255,255,0),5,CV_AA);
我在openCV中是新的,所以在我看来,我有左上角的点p> x和p-> y,但是我如何得到其他角落,我也在这个折线方法中的参数& n中感到困惑,这个多段线方法如何绘制完整的矩形?
当我使用边框,它不完美,它给纸张的一小部分空间.
任何帮助真的很感激
代码是:
- (cv::Mat)finshWork:(cv::Mat &)image{// read in the apple (change path to the file)Mat img0 =image;// imread("/home/philipp/img/apple.jpg",1);Mat img1;cvtcolor(img0,img1,CV_RGB2GRAY);// apply your filterCanny(img1,100,200);// find the contoursvector< vector<cv::Point> > contours;findContours(img1,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);/////for SQUARE CODEstd::vector<std::vector<cv::Point> > squares;std::vector<cv::Point> approx;for( size_t i = 0; i < contours.size(); i++ ){ cv::approxpolyDP(cv::Mat(contours[i]),approx,arcLength(cv::Mat(contours[i]),true)*0.02,true); if( approx.size() == 4 && fabs(contourArea(cv::Mat(approx))) > 1000 && cv::isContourConvex(cv::Mat(approx))) { double maxCosine = 0; for( int j = 2; j < 5; j++ ) { double cosine = fabs(angle(approx[j%4],approx[j-2],approx[j-1])); maxCosine = MAX(maxCosine,cosine); } if( maxCosine < 0.3 ) { squares.push_back(approx); cv::Point newPoint = approx[0]; NSLog(@"x is %d and y is %d",newPoint.x,newPoint.y); } }}const cv::Point* p = &squares[0][0];int n = (int)squares[0].size();NSLog(@"%d",n);//THIS IS WORKING CODE polylines(image,Scalar(0,255),10,CV_AA); //polylines(image,CV_AA);////////////}
谢谢
解决方法 参考 my original code,它简单地检测图像上的正方形.这意味着在应用程序的主要方法中,您可以编写以下伪代码来调用find_squares():
Mat image = imread("test.jpg",1);// Detect all regions in the image that are similar to a rectanglevector<vector<Point> > squares;find_squares(image,squares);// The largest of them probably represents the papervector<Point> largest_square;find_largest_square(squares,largest_square);// Print the x,y coordinates of the squarecout << "Point 1: " << largest_square[0] << endl;cout << "Point 2: " << largest_square[1] << endl;cout << "Point 3: " << largest_square[2] << endl;cout << "Point 4: " << largest_square[3] << endl;
诀窍依赖于find_largest_square(),如下所示:
voID find_largest_square(const vector<vector<Point> >& squares,vector<Point>& biggest_square){ if (!squares.size()) { // no squares detected return; } int max_wIDth = 0; int max_height = 0; int max_square_IDx = 0; const int n_points = 4; for (size_t i = 0; i < squares.size(); i++) { // Convert a set of 4 unordered Points into a meaningful cv::Rect structure. Rect rectangle = boundingRect(Mat(squares[i])); // cout << "find_largest_square: #" << i << " rectangle x:" << rectangle.x << " y:" << rectangle.y << " " << rectangle.wIDth << "x" << rectangle.height << endl; // Store the index position of the biggest square found if ((rectangle.wIDth >= max_wIDth) && (rectangle.height >= max_height)) { max_wIDth = rectangle.wIDth; max_height = rectangle.height; max_square_IDx = i; } } biggest_square = squares[max_square_IDx];}总结
以上是内存溢出为你收集整理的ios – 找到论文的角落全部内容,希望文章能够帮你解决ios – 找到论文的角落所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)