Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与OpenCV 2.1.X相比,OpenCV 2.4.X使用网络摄像头进行方形慢速检测_Opencv_Detection - Fatal编程技术网

与OpenCV 2.1.X相比,OpenCV 2.4.X使用网络摄像头进行方形慢速检测

与OpenCV 2.1.X相比,OpenCV 2.4.X使用网络摄像头进行方形慢速检测,opencv,detection,Opencv,Detection,我曾尝试使用OpenCV 2.4.1-2.4.4移植方形检测,但结果似乎非常缓慢。由于提供了新的功能,我很想使用更新版本的OpenCV,但效果很慢 我的2.4.X版OpenCV代码是: // The "Square Detector" program. // It loads several images sequentially and tries to find squares in // each image #include "opencv2/core/core.hpp" #inclu

我曾尝试使用OpenCV 2.4.1-2.4.4移植方形检测,但结果似乎非常缓慢。由于提供了新的功能,我很想使用更新版本的OpenCV,但效果很慢

我的2.4.X版OpenCV代码是:

// The "Square Detector" program.
// It loads several images sequentially and tries to find squares in
// each image

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <math.h>
#include <string.h>

using namespace cv;
using namespace std;



int thresh = 50, N = 11;
const char* wndname = "Square Detection Demo";

// helper function:
// finds a cosine of angle between vectors
// from pt0->pt1 and from pt0->pt2
static double angle( Point pt1, Point pt2, Point pt0 )
{
    double dx1 = pt1.x - pt0.x;
    double dy1 = pt1.y - pt0.y;
    double dx2 = pt2.x - pt0.x;
    double dy2 = pt2.y - pt0.y;
    return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}

// returns sequence of squares detected on the image.
// the sequence is stored in the specified memory storage
static void findSquares( const Mat& image, vector<vector<Point> >& squares )
{
    squares.clear();

    Mat pyr, timg, gray0(image.size(), CV_8U), gray;

    // down-scale and upscale the image to filter out the noise
    pyrDown(image, pyr, Size(image.cols/2, image.rows/2));
    pyrUp(pyr, timg, image.size());
    vector<vector<Point> > contours;

    // find squares in every color plane of the image
    for( int c = 0; c < 3; c++ )
    {
        int ch[] = {c, 0};
        mixChannels(&timg, 1, &gray0, 1, ch, 1);

        // try several threshold levels
        for( int l = 0; l < N; l++ )
        {
            // hack: use Canny instead of zero threshold level.
            // Canny helps to catch squares with gradient shading
            if( l == 0 )
            {
                // apply Canny. Take the upper threshold from slider
                // and set the lower to 0 (which forces edges merging)
                Canny(gray0, gray, 0, thresh, 5);
                // dilate canny output to remove potential
                // holes between edge segments
                dilate(gray, gray, Mat(), Point(-1,-1));
            }
            else
            {
                // apply threshold if l!=0:
                //     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
                gray = gray0 >= (l+1)*255/N;
            }

            // find contours and store them all as a list
            findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

            vector<Point> approx;

            // test each contour
            for( size_t i = 0; i < contours.size(); i++ )
            {
                // approximate contour with accuracy proportional
                // to the contour perimeter
                approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

                // square contours should have 4 vertices after approximation
                // relatively large area (to filter out noisy contours)
                // and be convex.
                // Note: absolute value of an area is used because
                // area may be positive or negative - in accordance with the
                // contour orientation
                if( approx.size() == 4 &&
                    fabs(contourArea(Mat(approx))) > 1000 &&
                    isContourConvex(Mat(approx)) )
                {
                    double maxCosine = 0;

                    for( int j = 2; j < 5; j++ )
                    {
                        // find the maximum cosine of the angle between joint edges
                        double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                        maxCosine = MAX(maxCosine, cosine);
                    }

                    // if cosines of all angles are small
                    // (all angles are ~90 degree) then write quandrange
                    // vertices to resultant sequence
                    if( maxCosine < 0.3 )
                        squares.push_back(approx);
                }
            }
        }
    }
}


// the function draws all the squares in the image
static void drawSquares( Mat& image, const vector<vector<Point> >& squares )
{
    for( size_t i = 0; i < squares.size(); i++ )
    {
        const Point* p = &squares[i][0];
        int n = (int)squares[i].size();
        polylines(image, &p, &n, 1, true, Scalar(0,255,0), 3, CV_AA);
    }

    imshow(wndname, image);
}


int main()
{
    VideoCapture cap;
    cap.open(0);
    Mat frame,image;


    namedWindow( "Square Detection Demo", 1 );
    vector<vector<Point> > squares;

    for(;;)
    {

        cap >> frame;
        if( frame.empty() ){
            break;
        }


        frame.copyTo(image);

        if( image.empty() )
        {
            cout << "Couldn't load image" << endl;
            continue;
        }

        findSquares(image, squares);
        drawSquares(image, squares);

        //imshow("Window", image);

        int c = waitKey(1);
        if( (char)c == 27 )
            break;
    }

    return 0;
}
//方形探测器”程序。
//它按顺序加载多个图像,并尝试在图像中查找正方形
//每张图片
#包括“opencv2/core/core.hpp”
#包括“opencv2/imgproc/imgproc.hpp”
#包括“opencv2/highgui/highgui.hpp”
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
int thresh=50,N=11;
const char*wndname=“方形检测演示”;
//辅助功能:
//查找向量之间角度的余弦
//从pt0->pt1和从pt0->pt2
静态双角度(点pt1、点pt2、点pt0)
{
双dx1=pt1.x-pt0.x;
双dy1=pt1.y-pt0.y;
双dx2=pt2.x-pt0.x;
双dy2=pt2.y-pt0.y;
返回(dx1*dx2+dy1*dy2)/sqrt(dx1*dx1+dy1*dy1)*(dx2*dx2+dy2*dy2)+1e-10);
}
//返回在图像上检测到的正方形序列。
//序列存储在指定的存储器中
静态空心findSquares(常数矩阵和图像、向量和正方形)
{
正方形。清除();
Mat-pyr,timg,灰色0(image.size(),CV_8U),灰色;
//缩小和放大图像以滤除噪声
pyrDown(image,pyr,Size(image.cols/2,image.rows/2));
pyrUp(pyr,timg,image.size());
矢量等值线;
//在图像的每个颜色平面中查找正方形
对于(int c=0;c<3;C++)
{
int ch[]={c,0};
混音通道(&timg,1,&0,1,ch,1);
//尝试几个阈值级别
对于(int l=0;l=(l+1)*255/N;
}
//找到等高线并将其全部存储为列表
findContours(灰色、等高线、等高线列表、等高线链近似简单);
向量近似;
//测试每个轮廓
对于(size_t i=0;i1000&&
isContourConvex(材料(近似)))
{
双最大余弦=0;
对于(int j=2;j<5;j++)
{
//求关节边之间角度的最大余弦
双余弦=fabs(角度(约[j%4],约[j-2],约[j-1]);
最大余弦=最大值(最大余弦,余弦);
}
//如果所有角度的余弦都很小
//(所有角度约为90度)然后写入量子线
//结果序列的顶点
如果(最大余弦<0.3)
正方形。推回(大约);
}
}
}
}
}
//该函数用于绘制图像中的所有正方形
静态空心绘图方块(材质和图像、常量向量和方块)
{
对于(size_t i=0;i>框架;
if(frame.empty()){
打破
}
frame.copyTo(图像);
if(image.empty())
{
库特pt2
双角度(CvPoint*pt1、CvPoint*pt2、CvPoint*pt0)
{
双dx1=pt1->x-pt0->x;
双dy1=pt1->y-pt0->y;
双dx2=pt2->x-pt0->x;
双dy2=pt2->y-pt0->y;
返回(dx1*dx2+dy1*dy2)/sqrt(dx1*dx1+dy1*dy1)*(dx2*dx2+dy2*dy2)+1e-10);
}
//返回在图像上检测到的正方形序列。
//序列存储在指定的存储器中
CvSeq*findSquares4(IplImage*img,CvMemStorage*storage)
{
CvSeq*等高线;
int i,c,l,N=11;
CvSize sz=CvSize(img->width&-2,img->height&-2);
IplImage*timg=cvCloneImage(img);//复制输入图像
IplImage*gray=cvCreateImage(sz,8,1);
IplImage*pyr=cvCreateImage(cvSize(sz.width/2,sz.height/2),8,3);
IplImage*tgray;
CvSeq*结果;
双s,t;
//创建将包含点的空序列-
//每个正方形4个点(正方形的顶点)
CvSeq*squares=cvCreateSeq(0,sizeof(CvSeq),sizeof(CvPoint),存储);
//选择图像中的最大ROI
//宽度和高度可被2整除
个人简历
    #include <cv.h>
#include <highgui.h>


int thresh = 50;
IplImage* img = 0;
IplImage* img0 = 0;
CvMemStorage* storage = 0;



// helper function:
// finds a cosine of angle between vectors
// from pt0->pt1 and from pt0->pt2
double angle( CvPoint* pt1, CvPoint* pt2, CvPoint* pt0 )
{
    double dx1 = pt1->x - pt0->x;
    double dy1 = pt1->y - pt0->y;
    double dx2 = pt2->x - pt0->x;
    double dy2 = pt2->y - pt0->y;
    return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}



// returns sequence of squares detected on the image.
// the sequence is stored in the specified memory storage
CvSeq* findSquares4( IplImage* img, CvMemStorage* storage )
{
    CvSeq* contours;
    int i, c, l, N = 11;
    CvSize sz = cvSize( img->width & -2, img->height & -2 );
    IplImage* timg = cvCloneImage( img ); // make a copy of input image
    IplImage* gray = cvCreateImage( sz, 8, 1 );

    IplImage* pyr = cvCreateImage( cvSize(sz.width/2, sz.height/2), 8, 3 );
    IplImage* tgray;
    CvSeq* result;
    double s, t;
    // create empty sequence that will contain points -
    // 4 points per square (the square's vertices)
    CvSeq* squares = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvPoint), storage );

    // select the maximum ROI in the image
    // with the width and height divisible by 2
    cvSetImageROI( timg, cvRect( 0, 0, sz.width, sz.height ));

    //cvSetImageROI( timg, cvRect( 0,0,50, 50 ));

    // down-scale and upscale the image to filter out the noise
    cvPyrDown( timg, pyr, 7 );
    cvPyrUp( pyr, timg, 7 );
    tgray = cvCreateImage( sz, 8, 1 );

    // find squares in every color plane of the image
    for( c = 0; c < 3; c++ )
    {
        // extract the c-th color plane
        cvSetImageCOI( timg, c+1 );
        cvCopy( timg, tgray, 0 );

        // try several threshold levels
        for( l = 0; l < N; l++ )
        {
            // hack: use Canny instead of zero threshold level.
            // Canny helps to catch squares with gradient shading
            if( l == 0 )
            {
                // apply Canny. Take the upper threshold from slider
                // and set the lower to 0 (which forces edges merging)
                cvCanny( tgray, gray, 0, thresh, 5 );
                // dilate canny output to remove potential
                // holes between edge segments
                cvDilate( gray, gray, 0, 1 );
            }
            else
            {
                // apply threshold if l!=0:
                //     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
                cvThreshold( tgray, gray, (l+1)*255/N, 255, CV_THRESH_BINARY );
            }

            // find contours and store them all as a list
            cvFindContours( gray, storage, &contours, sizeof(CvContour),
                CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );

            // test each contour
            while( contours )
            {
                // approximate contour with accuracy proportional
                // to the contour perimeter
                result = cvApproxPoly( contours, sizeof(CvContour), storage,
                    CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0 );
                // square contours should have 4 vertices after approximation
                // relatively large area (to filter out noisy contours)
                // and be convex.
                // Note: absolute value of an area is used because
                // area may be positive or negative - in accordance with the
                // contour orientation


                if( result->total == 4 &&
                    cvContourArea(result,CV_WHOLE_SEQ,0) > 1000 &&
                    cvCheckContourConvexity(result) )
                {
                    s = 0;

                    for( i = 0; i < 5; i++ )
                    {
                        // find minimum angle between joint
                        // edges (maximum of cosine)
                        if( i >= 2 )
                        {
                            t = fabs(angle(
                            (CvPoint*)cvGetSeqElem( result, i ),
                            (CvPoint*)cvGetSeqElem( result, i-2 ),
                            (CvPoint*)cvGetSeqElem( result, i-1 )));
                            s = s > t ? s : t;
                        }
                    }

                    // if cosines of all angles are small
                    // (all angles are ~90 degree) then write quandrange
                    // vertices to resultant sequence
                    if( s < 0.3 )
                        for( i = 0; i < 4; i++ )
                            cvSeqPush( squares,
                                (CvPoint*)cvGetSeqElem( result, i ));
                }

                // take the next contour
                contours = contours->h_next;
            }
        }
    }

    // release all the temporary images
    cvReleaseImage( &gray );
    cvReleaseImage( &pyr );
    cvReleaseImage( &tgray );
    cvReleaseImage( &timg );

    return squares;
}



// the function draws all the squares in the image
void drawSquares( IplImage* img, CvSeq* squares )
{
    CvSeqReader reader;
    IplImage* cpy = cvCloneImage( img );
    int i;

    // initialize reader of the sequence
    cvStartReadSeq( squares, &reader, 0 );

    // read 4 sequence elements at a time (all vertices of a square)
    for( i = 0; i < squares->total; i += 4 )
    {

        CvPoint pt[4], *rect = pt;
        int count = 4;

        // read 4 vertices
        CV_READ_SEQ_ELEM( pt[0], reader );
        CV_READ_SEQ_ELEM( pt[1], reader );
        CV_READ_SEQ_ELEM( pt[2], reader );
        CV_READ_SEQ_ELEM( pt[3], reader );

        // draw the square as a closed polyline
        cvPolyLine( cpy, &rect, &count, 1, 1, CV_RGB(0,255,0), 3, CV_AA, 0 );
    }

    // show the resultant image
    cvShowImage( "Squares", cpy );
    cvReleaseImage( &cpy );
}




int main(int argc, char** argv){

     // Crea una ventana llamada Original Image con un tamaño predeterminado.
    cvNamedWindow("Original Image", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("Squares", CV_WINDOW_AUTOSIZE);

    // Crea la conexion con la Webcam.
    CvCapture* capture = cvCreateCameraCapture(0);

    if( !capture ){
        throw "Error when reading steam_avi";
    }

    storage = cvCreateMemStorage(0);
    while(true)
        {

        // Pongo el frame capturado dentro de la imagen originalImg.
        img0 = cvQueryFrame(capture);
        if(!img0){
            break;
        }





        img = cvCloneImage( img0 );

        // find and draw the squares
        drawSquares( img, findSquares4( img, storage ) );




        cvShowImage("Original Image", img0);


        cvReleaseImage(&img);   
        // clear memory storage - reset free space position
        cvClearMemStorage( storage );

        // Espero a que me pulsen el ESC para salir del bucle infinito.
        char c = cvWaitKey(10);
        if( c == 27 ) break;
    }

    //cvReleaseImage(&img); 
    cvReleaseImage(&img0);

    // clear memory storage - reset free space position
        cvClearMemStorage( storage );
    // Destruye la ventana “Original Image”.
    cvDestroyWindow("Original Image");
    cvDestroyWindow("Squares");
    // Libera la memoria utilizada por la variable capture.
    cvReleaseCapture(&capture);
}