Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.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

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代码运行非常慢(可能是内存泄漏?)_C_Opencv_Memory Leaks - Fatal编程技术网

我的opencv代码运行非常慢(可能是内存泄漏?)

我的opencv代码运行非常慢(可能是内存泄漏?),c,opencv,memory-leaks,C,Opencv,Memory Leaks,我正在写一个跟随鼠标的图像放大器。但它有一些问题。随着时间的推移,它变得非常慢。我想原因可能是内存泄漏。但我不知道哪里出了问题,以及如何纠正它。如果有人能帮我,我将不胜感激 这是我的代码: #include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; using namespace cv; #define Radi

我正在写一个跟随鼠标的图像放大器。但它有一些问题。随着时间的推移,它变得非常慢。我想原因可能是内存泄漏。但我不知道哪里出了问题,以及如何纠正它。如果有人能帮我,我将不胜感激

这是我的代码:

#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <iostream>

using namespace std;
using namespace cv;

#define Radius_0 60
#define Radius_1 90

IplImage* src = 0;
IplImage* dst = 0;

void  my_mouse_callback(int event, int x, int y, int flags, void * param)
{
    int center_x0, center_y0;
    int center_x1, center_y1;
    if(event == CV_EVENT_MOUSEMOVE)
    {
        if(x <= 60) center_x0 = 60;
        else if(x >= src->width - 60) center_x0 = src->width - 60;
        else
            center_x0 = x;
        if(y <= 60) center_y0 = 60;
        else if(y >= src->height - 60) center_y0 = src->height - 60;
        else
            center_y0 = y;
        if(x <= 90) center_x1 = 90;
        else if(x >= dst->width - 90) center_x1 = src->width - 90;
        else
            center_x1 = x;
        if(y <= 90) center_y1 = 90;
        else if(y >= src->height - 90) center_y1 = src->height - 90;
        else
            center_y1 = y;

        IplImage* tmp = cvCreateImage(CvSize(Radius_0*3, Radius_0*3), src->depth, src->nChannels);
        cvZero(tmp);
        CvPoint p0 = cvPoint(center_x0-Radius_0, center_y0-Radius_0);
                CvPoint p1 = cvPoint(center_x0+Radius_0, center_y0+Radius_0);
                cvSetImageROI(src,cvRect(p0.x,p0.y,p1.x-p0.x,p1.y-p0.y));
                cvResize(src, tmp);
        cvResetImageROI(src);

        cvZero(dst);
        dst = cvCloneImage(src);
        uchar* DstData = (uchar *)dst->imageData;
        uchar* TmpData = (uchar *)tmp->imageData;
        int DstStep = dst->widthStep/sizeof(uchar);
        int TmpStep = tmp->widthStep/sizeof(uchar);
                int channels = dst -> nChannels;
        for(int i = center_y1-Radius_1; i < center_y1+Radius_1; ++ i)
        {
            for(int j = center_x1-Radius_1; j < center_x1+Radius_1; ++ j)
            {
                int r = (i - center_y1)*(i - center_y1) + (j - center_x1)*(j - center_x1);
                if(r <= Radius_1*Radius_1)
                {
                    int m = i + Radius_1 - center_y1;
                    int n = j + Radius_1 - center_x1;
                    DstData[i*DstStep+j*channels+0] = TmpData[m*TmpStep+n*channels+0];
                    DstData[i*DstStep+j*channels+1] = TmpData[m*TmpStep+n*channels+1];
                    DstData[i*DstStep+j*channels+2] = TmpData[m*TmpStep+n*channels+2];
                }
            }
        }
        cvShowImage("dst",dst); 
        cvReleaseImage(&tmp);
    }
}

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

    if(argc != 2){
        return -1;
    }
    src = cvLoadImage(argv[1], 1);
    dst = cvCloneImage(src);
    cvNamedWindow("dst", 1);
    cvShowImage("dst",dst);
        cvSetMouseCallback("dst", my_mouse_callback, 0 );
        while(1){
                if(cvWaitKey(15) == 27)
                        break;
        }
        cvDestroyAllWindows();
        cvReleaseImage(&src);
    cvReleaseImage(&dst);
        return 0;
}           
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间cv;
#定义半径_0 60
#定义半径_1 90
IplImage*src=0;
IplImage*dst=0;
void my_mouse_回调(int事件、int x、int y、int标志、void*param)
{
int center_x0,center_y0;
int center_x1,center_y1;
if(event==CV\u event\u MOUSEMOVE)
{
如果(x=src->width-60)中心_x0=src->width-60;
其他的
中心x0=x;
如果(y=src->高度-60)居中,则y=src->高度-60;
其他的
中心_y0=y;
如果(x=dst->宽度-90)中心_x1=src->宽度-90;
其他的
中心_x1=x;
如果(y=src->高度-90)居中_y1=src->高度-90;
其他的
中心_y1=y;
IplImage*tmp=cvCreateImage(CvSize(半径0*3,半径0*3),src->depth,src->nChannels);
cvZero(tmp);
CvPoint p0=CvPoint(中心×0-半径×0,中心×y0-半径×0);
CvPoint p1=CvPoint(中心x0+半径0,中心y0+半径0);
cvSetImageROI(src,cvRect(p0.x,p0.y,p1.x-p0.x,p1.y-p0.y));
cvResize(src,tmp);
cvr(src);
cvZero(dst);
dst=克隆图像(src);
uchar*DstData=(uchar*)dst->imageData;
uchar*TmpData=(uchar*)tmp->imageData;
int DstStep=dst->widthStep/sizeof(uchar);
int TmpStep=tmp->widthStep/sizeof(uchar);
int channels=dst->n通道;
对于(int i=中心半径;i<中心半径
{
对于(int j=中心半径;j<中心半径
{
int r=(i-中心×1)*(i-中心×1)+(j-中心×1)*(j-中心×1);

如果(R)请不要使用OpenCV的可尊敬的C-API,但是C++(CV::MAT)。难怪你会有这样的MyBox。Balk谢谢,Balac!但是我想知道为什么内存泄漏会发生?我在代码中使用了CVRelaseIMAGE(…)。不释放以前的代码<代码> DST<代码>,所以如果你想坚持下去,在调用之前插入一个<代码> CVRelaseIMAGE(& DST)< /C> >如果代码> DST 不是空的。谢谢,RogerRowland!就是!谢谢!@罗杰罗兰请不要使用OpenCV的古老的C API,但是C++一个(CV::MAT)。难怪你会有这样的memleaks。@berak谢谢,berak!但我想知道为什么会发生内存泄漏?我在代码中使用了cvReleaseImage()。。正如@berak所说,请不要再使用旧的C API。我不知道,但我怀疑
dst=cvCloneImage(src)
不会发布以前的
dst
内容,因此如果您确实想继续,请在调用之前插入
cvReleaseImage(&dst)
,前提是
dst
不为空。谢谢,@rogerowland!就是这样!!!!!非常感谢!!@rogerowland