Ios 找到文件的一角

Ios 找到文件的一角,ios,iphone,objective-c,opencv,image-processing,Ios,Iphone,Objective C,Opencv,Image Processing,我是openCV的新手,所以在过去的3到4天里我一直在努力,我已经检测到了纸张的边界,现在我想在角落里画4个圆圈 我从这个代码中画出边界 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,但

我是openCV的新手,所以在过去的3到4天里我一直在努力,我已经检测到了纸张的边界,现在我想在角落里画4个圆圈

我从这个代码中画出边界

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 filter
Canny(img1, img1, 100, 200);

// find the contours
vector< vector<cv::Point> > contours;
findContours(img1, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);

/////for SQUARE CODE
std::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, &p,&n, 1, true, Scalar(0,0,255), 10, CV_AA);
    //polylines(image, &p,&n, 1, true, Scalar(255,255,0), 5, CV_AA);
////////////
}
-(cv::Mat)finshWork:(cv::Mat&)图像
{
//读取苹果(更改文件路径)
Mat img0=image;//imread(“/home/philipp/img/apple.jpg”,1);
Mat-img1;
CVT颜色(img0、img1、CV_rgb2灰色);
//应用你的过滤器
Canny(img1,img1,100200);
//找到轮廓
矢量<矢量>等高线;
findContours(img1、等高线、等高线、等高线外部、等高线链近似值);
/////平方码
向量平方;
std::向量近似;
对于(size_t i=0;i1000和&cv::isContourConvex(cv::Mat(近似))){
双最大余弦=0;
对于(int j=2;j<5;j++)
{
双余弦=fabs(角度(约[j%4],约[j-2],约[j-1]);
最大余弦=最大值(最大余弦,余弦);
}
如果(最大余弦<0.3){
正方形。推回(大约);
cv::Point newPoint=约[0];
NSLog(@“x是%d,y是%d”,newPoint.x,newPoint.y);
}
}
}
常数cv::点*p=&平方[0][0];
int n=(int)正方形[0]。大小();
NSLog(@“%d”,n);
//这是工作代码
多段线(图像、p和n、1、真、标量(0,0255)、10、CV_AA);
//多段线(图像、&p、&n、1、真、标量(255255,0)、5、CV_AA);
////////////
}
感谢

Take for reference,它只检测图像上的正方形

这意味着在应用程序的main方法中,您将编写类似于以下伪代码的代码来调用
find_squares()


你能澄清一下你是如何找到这份报纸的吗?您最好添加更多代码。@moosgummi请检查我的编辑,我想我在这里使用了您问题中的部分代码,感谢您分享这部分内容。您好,如果白皮书在白色桌面上,我如何检测它?。。。我尝试过改变灰度和canny转换,但找不到矩形。。。。thnks:)如何找到旋转矩形(其边不平行于坐标轴的矩形)的角?大家好,我记录了最大的方点,但它们超出了设备的界限。(24001500)是值。有没有公式将这些cv::点转换为设备坐标是的,这就是所谓的三法则。
Mat image = imread("test.jpg", 1);

// Detect all regions in the image that are similar to a rectangle
vector<vector<Point> > squares;
find_squares(image, squares);

// The largest of them probably represents the paper
vector<Point> largest_square;
find_largest_square(squares, largest_square);

// Print the x,y coordinates of the square
cout << "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;
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];
}