OPENCV 奇怪的错误

发布于 2024-12-24 18:55:29 字数 1785 浏览 3 评论 0原文

我正在使用背景扣除并想要显示内容。不知何故,由于内存异常,代码似乎总是中断。该错误似乎出在 cvCopy 函数中。想不通。

#include "cv.h"
#include "highgui.h"
#include "opencv2\core\operations.hpp"
#include "opencv2\core\core.hpp"
#include "opencv2\core\types_c.h"
#include "opencv\cxcore.h"
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int, char**)
{
    bool flag=0;
    VideoCapture cap(0); // open the default camera
    VideoCapture cap1(0);
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat gray,bg,result,frame,result1,final,frame1;
    //CvMemStorage*     contours = NULL;

    cap>>frame;
    cvtColor(frame,bg,CV_BGR2GRAY);

    namedWindow("GRAY",1);

    for(;;)
    {
        //final = Mat::zeros(mGreenScale.rows, mGreenScale.cols, CV_8UC3);
        cap >> frame; // get a new frame from camera
        cap1 >> frame1;
        cvtColor(frame, gray, CV_BGR2GRAY);
        absdiff(gray,bg,result);
        threshold(result,result1,50,255,THRESH_BINARY);
        //cvCopy(const CvArr* src, CvArr* dst, const CvArr* mask=NULL)¶
        //cvCopy(&frame1, &final, &result1);
        //findContours(result1,contours, ;CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
        //drawContours(final,contours,-1,CV_RGB(0,255,0));
        //imshow("GRAY",result1);
        //imshow("GRAY", result);
        imshow("GRAY1",final);

        if(flag)
        {
            imshow("BG",bg);
        }
        //if(waitKey(0)==27) break;
        if(waitKey(1)==32) 
        {
            cvtColor(frame,bg,CV_BGR2GRAY);
            flag=!flag;
        }
        if(waitKey(1)==27) 
        {
            break;
        }
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

I am using Background Subtraction and want to display the contents. Somehow the code seems to break all the time due to a memory exception. The error seems to be in cvCopy function. Can't figure it out.

#include "cv.h"
#include "highgui.h"
#include "opencv2\core\operations.hpp"
#include "opencv2\core\core.hpp"
#include "opencv2\core\types_c.h"
#include "opencv\cxcore.h"
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int, char**)
{
    bool flag=0;
    VideoCapture cap(0); // open the default camera
    VideoCapture cap1(0);
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat gray,bg,result,frame,result1,final,frame1;
    //CvMemStorage*     contours = NULL;

    cap>>frame;
    cvtColor(frame,bg,CV_BGR2GRAY);

    namedWindow("GRAY",1);

    for(;;)
    {
        //final = Mat::zeros(mGreenScale.rows, mGreenScale.cols, CV_8UC3);
        cap >> frame; // get a new frame from camera
        cap1 >> frame1;
        cvtColor(frame, gray, CV_BGR2GRAY);
        absdiff(gray,bg,result);
        threshold(result,result1,50,255,THRESH_BINARY);
        //cvCopy(const CvArr* src, CvArr* dst, const CvArr* mask=NULL)¶
        //cvCopy(&frame1, &final, &result1);
        //findContours(result1,contours, ;CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
        //drawContours(final,contours,-1,CV_RGB(0,255,0));
        //imshow("GRAY",result1);
        //imshow("GRAY", result);
        imshow("GRAY1",final);

        if(flag)
        {
            imshow("BG",bg);
        }
        //if(waitKey(0)==27) break;
        if(waitKey(1)==32) 
        {
            cvtColor(frame,bg,CV_BGR2GRAY);
            flag=!flag;
        }
        if(waitKey(1)==27) 
        {
            break;
        }
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2024-12-31 18:55:29

我建议您尽可能坚持使用 C++ API,而不是混合使用 C 和 C++ API。如果您只想复制矩阵,只需使用 Mat::clone()Mat ::copyTo()。由于您想使用掩码,请使用 copyTo 成员函数,如下所示:

Mat final;
frame1.copyTo(final, result1);

希望有帮助!

Instead of mixing the C and C++ APIs I would recommend you stick to the C++ API where possible. If you merely want to copy a matrix, just use either Mat::clone() or Mat::copyTo(). Since you want to use a mask, use the copyTo member function like this:

Mat final;
frame1.copyTo(final, result1);

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文