none
程序执行完后返回异常 RRS feed

  • 问题

  • #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include "CompressiveTracker.h"
    using namespace cv;
    using namespace std;
    vector <Rect> readConfig(char* configFileName, char* imgFilePath);
    /*  Description: read the tracking information from file "config.txt"
        Arguments:	
    	-configFileName: config file name
    	-ImgFilePath:    Path of the storing image sequences
    	-box:            [x y width height] intial tracking position
    	*/
    void readImageSequenceFiles(char* ImgFilePath,vector <string> &imgNames);
    /*  Description: search the image names in the image sequences 
        Arguments:
    	-ImgFilePath: path of the image sequence
    	-imgNames:  vector that stores image name
    	
    */
    bool check_unique(vector <Rect> box, int index);
    /*  Description: check if current position is unique in box
        Arguments:
    	-box: box record the position of flies
    	-index: which position is being checking
    	
    */
    int main(int argc, char * argv[])
    {
    	char imgFilePath[100];
        char  conf[100];
    	strcpy(conf,"./config.txt");
    	
    	char tmpDirPath[MAX_PATH+1];
    	vector <Rect> box;// [x y width height] tracking position
    	vector <string> imgNames;
    	box = readConfig(conf,imgFilePath);//
    	
    	readImageSequenceFiles(imgFilePath,imgNames);
    	// CT framework
    	vector <CompressiveTracker> ct;
    	for(int i = 0; i < box.size(); i++)
    	{
    		ct.push_back(CompressiveTracker());
    	}
    	//CompressiveTracker *ct = new CompressiveTracker[2];
    	Mat frame;
    	Mat grayImg;
    	sprintf(tmpDirPath, "%s/", imgFilePath);
    	imgNames[0].insert(0,tmpDirPath);
    	frame = imread(imgNames[0]);
        cvtColor(frame, grayImg, CV_RGB2GRAY);    
    	//ct->init(grayImg, box[0]);  
    	for(int i=0;i<ct.size();i++)
    	{
    		ct[i].init(grayImg, box[i]);
    	}
    	char strFrame[20];
        FILE* resultStream;
    	resultStream = fopen("TrackingResults.txt", "w");
    	for(int i = 0; i < box.size(); i++)
    	{
    		fprintf (resultStream,"%i %i %i %i\n",(int)box[i].x,(int)box[i].y,(int)box[i].width,(int)box[i].height);
    	}
    	
    	for(int i = 1; i < imgNames.size()-1; i ++)
    	{
    		
    		sprintf(tmpDirPath, "%s/", imgFilePath);
            imgNames[i].insert(0,tmpDirPath);
            		
    		frame = imread(imgNames[i]);// get frame
    		cvtColor(frame, grayImg, CV_RGB2GRAY);
    		for(int n = 0; n<ct.size();n++)
    		{
    			ct[n].processFrame(grayImg, box[n]);// Process frame
    			if(!check_unique(box, n))
    			{
    				ct.erase(ct.begin()+n);
    				box.erase(box.begin()+n);
    				n--;
    				break;
    			}
    			rectangle(frame, box[n], Scalar(200,0,0),2);// Draw rectangle
    		}
    		
    		
    		//rectangle(frame, box[0], Scalar(200,0,0),2);// Draw rectangle
    		fprintf (resultStream,"%i %i %i %i\n",(int)box[0].x,(int)box[0].y,(int)box[0].width,(int)box[0].height);
    		sprintf(strFrame, "#%d ",i);
    		//putText(frame,strFrame,cvPoint(0,20),2,1,CV_RGB(25,200,25));
    		
    		imshow("CT", frame);// Display
    		waitKey(1);		
    	}
    	fclose(resultStream);
    	ct.clear();
    	box.clear();
    	return 0;
    }
    vector <Rect> readConfig(char* configFileName, char* imgFilePath)	
    {
    	int x;
    	int y;
    	int w;
    	int h;
    	int num;
    	fstream f;
    	char cstring[1000];
    	int readS=0;
    	f.open(configFileName, fstream::in);
    	char param1[200]; strcpy(param1,"");
    	char param2[200]; strcpy(param2,"");
    	char param3[200]; strcpy(param3,"");
    	f.getline(cstring, sizeof(cstring));
    	readS=sscanf (cstring, "%s %s %s", param1,param2, param3);
    	strcpy(imgFilePath,param3);
    	f.getline(cstring, sizeof(cstring));
    	readS=sscanf (cstring, "%i", &num);
    	vector <Rect> box;
    	f.getline(cstring, sizeof(cstring)); 
    	
    	for(int i = 0; i < num; i++)
    	{
    		f.getline(cstring, sizeof(cstring));
    		readS=sscanf (cstring, "%s %s %i %i %i %i", param1,param2, &x, &y, &w, &h);
    		box.push_back(Rect(x, y, w, h));
    	}
    	return box;
    	
    }
    void readImageSequenceFiles(char* imgFilePath,vector <string> &imgNames)
    {	
    	imgNames.clear();
    	char tmpDirSpec[MAX_PATH+1];
    	sprintf (tmpDirSpec, "%s/*", imgFilePath);
    	WIN32_FIND_DATA f;
    	HANDLE h = FindFirstFile(tmpDirSpec , &f);
    	if(h != INVALID_HANDLE_VALUE)
    	{
    		FindNextFile(h, &f);	//read ..
    		FindNextFile(h, &f);	//read .
    		do
    		{
    			imgNames.push_back(f.cFileName);
    		} while(FindNextFile(h, &f));
    	}
    	FindClose(h);	
    }
    bool check_unique(vector <Rect> box, int index)
    {
    	
    	for(int i = index+1; i < box.size(); i++)
    	{
    		
    		float diffx = ( box[i].x+box[i].width/2 ) - ( box[index].x + box[index].width/2 );
    		float diffy = ( box[i].y+box[i].height/2 ) - ( box[index].y + box[index].height/2 );
    		
    		
    		
    		if(sqrt(pow(diffx,2) + pow(diffy,2)) < 2)
    			return false;
    	}
    	return true;
    }

    一个跟踪算法的程序

    程序能够正常运行,效果也正确。但是在执行return 0后发生错误异常如下:

    First-chance exception at 0x773cc398 in CompressiveTracking.exe: 0xC0000005: Access violation writing location 0x00000035.
    Unhandled exception at 0x773cc398 in CompressiveTracking.exe: 0xC0000005: Access violation writing location 0x00000035.

    不知道是程序内部出现内存泄露还是这个程序以外的问题(觉得是后者)

    如果是程序外的问题该如何解决

    谢谢

    2012年8月26日 10:53

答案