C++로 작업하는 Im OpenCV 라이브러리가있는 Visual Studio 2012에서. 문제는 콘솔 응용 프로그램을 실행할 때 약 2 ~ 3 초 후에 닫힙니다. 을 Heres 코드 : 내 카메라가 제대로 작동하고Visual Studio 2012 C++ 콘솔 응용 프로그램이 즉시 종료됩니다.
#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;
}
}
}
여전히 종료됩니다.
CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
및
이delete moments;
당신은 두 가지를 혼합하지해야합니다 malloc/free
및 new/delete
- 사전
메모리 누수, 확인되지 않은 포인터 ... 무엇을 사랑하지 않으시겠습니까? –
엄지 손가락 :'포인터 연산을위한 포인터 사용, 다른 모든 것에 대한 참조 ' –
메모리를 할당하기 위해'malloc'을 사용하고, 메모리를 할당하기 위해'delete'를 사용하고 있습니다. – lapk