我正在写一个跟随鼠标的图像放大器。但是它有一些问题。随着时间的推移它变得非常慢。我认为原因可能是内存泄漏。但我不是&# 39;知道哪里出错了以及如何纠正错误。如果有人能提供帮助,我将不胜感激!
这是我的代码:
#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;
}