C++ Visual Studio 2012 C++;控制台应用程序立即退出
我用VisualStudio 2012用OpenCV库来处理C++。 问题是,当我运行控制台应用程序时,大约2或3秒后它就会关闭。 代码如下:C++ Visual Studio 2012 C++;控制台应用程序立即退出,c++,opencv,console,exit,C++,Opencv,Console,Exit,我用VisualStudio 2012用OpenCV库来处理C++。 问题是,当我运行控制台应用程序时,大约2或3秒后它就会关闭。 代码如下: #include "cv.h" #include "highgui.h" IplImage* GetThresholdedImage(IplImage *img) { // Convert the image into an HSV image IplImage* imgHSV = cvCreateImage(cvGetSize(img
#include "cv.h"
#include "highgui.h"
IplImage* GetThresholdedImage(IplImage *img)
{
// Convert the image into an HSV image
IplImage* imgHSV = cvCreateImage(cvGetSize(img), 8, 3);
cvCvtColor(img, imgHSV, CV_BGR2HSV);
IplImage* imgThreshed = cvCreateImage(cvGetSize(img), 8, 1);
cvInRangeS(imgHSV, cvScalar(20, 100, 100), cvScalar(30, 255, 255), imgThreshed); //Detect Colour
cvReleaseImage(&imgHSV);
return imgThreshed;
}
int main()
{
// Initialize capturing live feed from the camera
CvCapture* capture = 0;
capture = cvCaptureFromCAM(0); // cvCaptureFromCAM(0) indicates camera being used, Change the 0 for a different camera
// Couldn't get a device? Throw an error and quit
if(!capture)
{
printf("Camera not working\n");
return -1;
}
// The two windows we'll be using
cvNamedWindow("video"); //Create new window containing video
cvNamedWindow("thresh"); //Create another window containing thresholded image
// This image holds the "scribble" data
// the tracked positions of the ball
IplImage* imgScribble = NULL;
// An infinite loop
while(true)
{
// Will hold a frame captured from the camera
IplImage* frame = 0;
frame = cvQueryFrame(capture);
// If we couldn't grab a frame... quit
if(!frame)
printf("Couldnt get frame \n");
// If this is the first frame, we need to initialize it
if(imgScribble == NULL)
{
imgScribble = cvCreateImage(cvGetSize(frame), 8, 3);
}
// Holds the yellow thresholded image (yellow = white, rest = black)
IplImage* imgYellowThresh = GetThresholdedImage(frame);
// Calculate the moments to estimate the position of the ball
CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
cvMoments(imgYellowThresh, moments, 1);
// The actual moment values
double moment10 = cvGetSpatialMoment(moments, 1, 0);
double moment01 = cvGetSpatialMoment(moments, 0, 1);
double area = cvGetCentralMoment(moments, 0, 0);
// Holding the last and current ball positions
static int posX = 0;
static int posY = 0;
int lastX = posX;
int lastY = posY;
posX = moment10/area;
posY = moment01/area;
// Print it out for debugging purposes
printf("position (%d,%d)\n", posX, posY);
// We want to draw a line only if its a valid position
if(lastX>0 && lastY>0 && posX>0 && posY>0)
{
// Draw a yellow line from the previous point to the current point
cvLine(imgScribble, cvPoint(posX, posY), cvPoint(lastX, lastY), cvScalar(0,255,255), 5);
}
// Add the scribbling image and the frame
cvAdd(frame, imgScribble, frame);
cvShowImage("thresh", imgYellowThresh);
cvShowImage("video", frame);
// Release the thresholded image+moments we need no memory leaks please
cvReleaseImage(&imgYellowThresh);
delete moments;
// We're done using the camera. Other applications can now use it
cvReleaseCapture(&capture);
return 0;
// Wait for a keypress
int c = cvWaitKey(10);
if(c!=-1)
{
// If pressed, break out of the loop
break;
}
}
}
我的相机工作正常,但还是停了。
提前感谢您混合了两种分配/释放内存的方法-
malloc/free
和new/delete
:
CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
及
你不应该把两者混为一谈<代码>malloc/free不要在分配的内存上调用构造函数/析构函数new/delete
在分配的内存上调用构造函数/析构函数,并且可能使用其他内存函数而不是malloc/free
来分配/释放实际内存。因此,您可能会遇到各种各样的错误,其原因包括对未构造对象调用析构函数,以及使用非对应函数分配/释放内存
当然,还有其他问题。这是一个显而易见的问题,你应该先解决它
编辑#1:虽然上面所说的一切都是正确的,应该被修复,但很可能,你已经确定了:你无条件地从循环内部返回。在看到malloc/delete
problem;)之后,我不应该停止检查您的代码
编辑#2:回答您的评论。
如果cvm
是C结构,请使用free
而不是delete
。如果是C++类,则使用<代码>新< /COD>代替 MALLC/<代码>。p>
您还必须在循环结束时检查代码。很难说你想在那里做什么。从外观上看,我会将时刻的内存分配放在循环之前,并在循环之后释放内存-您会继续分配/释放相同数量的内存。你甚至需要把它堆起来吗?你们能不能把它放在堆栈上?似乎您还希望继续使用相机,直到用户按下一个键。您是否需要在用户按键后或每次开始等待之前释放摄像头?在任何情况下,您的return
语句在控件转到if
语句之前退出循环和main
(和程序)。将return
语句置于循环之外
简言之:
循环前-分配内存。记住使用malloc/free
或new/delete
,但不要混用它们。想一想,若你们需要堆,也许堆栈是可以的
循环中-删除释放内存,删除释放摄像头,删除返回。你将在下一次循环中使用它们,对吗?如果用户按下一个键(在循环内),您将退出循环
在循环之后-现在你真的想要清理。释放内存,释放摄像头,从main
返回您混合了两种分配/释放内存的方法-malloc/Free
和new/delete
:
CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
及
你不应该把两者混为一谈<代码>malloc/free
不要在分配的内存上调用构造函数/析构函数new/delete
在分配的内存上调用构造函数/析构函数,并且可能使用其他内存函数而不是malloc/free
来分配/释放实际内存。因此,您可能会遇到各种各样的错误,其原因包括对未构造对象调用析构函数,以及使用非对应函数分配/释放内存
当然,还有其他问题。这是一个显而易见的问题,你应该先解决它
编辑#1:虽然上面所说的一切都是正确的,应该被修复,但很可能,你已经确定了:你无条件地从循环内部返回。在看到malloc/delete
problem;)之后,我不应该停止检查您的代码
编辑#2:回答您的评论。
如果cvm
是C结构,请使用free
而不是delete
。如果是C++类,则使用<代码>新< /COD>代替 MALLC/<代码>。p>
您还必须在循环结束时检查代码。很难说你想在那里做什么。从外观上看,我会将时刻的内存分配放在循环之前,并在循环之后释放内存-您会继续分配/释放相同数量的内存。你甚至需要把它堆起来吗?你们能不能把它放在堆栈上?似乎您还希望继续使用相机,直到用户按下一个键。您是否需要在用户按键后或每次开始等待之前释放摄像头?在任何情况下,您的return
语句在控件转到if
语句之前退出循环和main
(和程序)。将return
语句置于循环之外
简言之:
循环前-分配内存。记住使用malloc/free
或new/delete
,但不要混用它们。想一想,若你们需要堆,也许堆栈是可以的
循环中-删除释放内存,删除释放摄像头,删除返回。你将在下一次循环中使用它们,对吗?如果用户按下一个键(在循环内),您将退出循环
在循环之后-现在你真的想要清理。释放内存,释放摄像头,从main
返回Malloc和delete不能一起使用,请使用Free(ptr)。Malloc和delete不能一起使用,请使用Free(ptr)。
// We're done using the camera. Other applications can now use it
cvReleaseCapture(&capture)
返回0
一定是出了while循环体。我想
// We're done using the camera. Other applications can now use it
cvReleaseCapture(&capture)
返回0
必须是超出while循环体。内存泄漏、未检查的指针。。。什么是不喜欢的?经验法则:使用指针进行指针运算,其他一切都使用引用。
您正在使用malloc
分配内存,并delete
释放内存。内存泄漏,未检查的指针。。。什么是不爱的?经验法则:Use