segmentation fault in image processing using SURF
I am getting a segmentation fault after the loop runs for sometime. I want the loop to run as an infinite loop. Why is the error occurring?
int main( int argc, char** argv ) {
cv::VideoCapture cap("tcp://192.168.1.1:5555"); // open a video file
if(!cap.isOpened()) // check if succeeded { std::cout << " not found or could not be opened" << std::endl; printf("4\n"); return -1; }
unsigned long counter =0; cv::Mat frame; // read frames until end of video:
struct timeval start,end; long seconds,useconds; long mtime=0; gettimeofday(&start,NULL); cap >> frame; imwrite("image.pgm",frame); Mat img_1 = imread( "image.pgm" ); int minHessian = 500;
SurfFeatureDetector detector( minHessian,1,2,false,true ); detector.detect( img_1, keypoints_1p );
SurfDescriptorExtractor extractor(minHessian,1,1,1,0); Mat descriptors1, descriptors2;
extractor.compute(img_1, keypoints_1p, descriptors1);
while(cap.read(frame)) {
// adjust the filename by incrementing a counter
std::stringstream filename (std::stringstream::in | std::stringstream::out);
filename << "img-000" << counter++ << ".pgm";
//printf("filename =%s ",filename);
std::cout << "writing " << filename.str().c_str() << " to disk" << std::endl;
// save frame to file: image0.jpg, image1.jpg, and so on...
cv::imwrite(filename.str().c_str(),frame);
Mat img_2 = imread( filename.str().c_str() );
if( !img_1.data || !img_2.data )
{
std::cout<< " --(!) Error reading images " << std::endl; return -1;
}
//-- Step 1: Detect the keypoints using SURF Detector
detector.detect( img_2, keypoints_2p);
// computing descriptors
extractor.compute(img_2, keypoints_2p, descriptors2);
int i,j;
Mat a(descriptors1.rows,descriptors1.cols,descriptors1.type()); Mat b(descriptors2.rows,descriptors2.cols, descriptors2.type()); a=descriptors1; b=descriptors2;
Mat m(descriptors1.rows,2,CV_16U);
if(((descriptors2.rows)||(descriptors1.rows))==0) { m.push_back(0); return 0; }
Mat des2t(descriptors2.rows,descriptors2.cols,descriptors2.type());
des2t= b.t();
m= Mat::zeros(descriptors1.rows,1,CV_32F);
Mat dotprod = (descriptors1*des2t);
Mat angle(dotprod.rows,dotprod.cols,dotprod.type());
for(int k=0;k<dotprod.rows;k++) {="" for(int="" l="0;l<dotprod.cols;l++)" {<="" p="">
angle.at<float>(k,l)= acos(dotprod.at<float>(k,l));
}
}
int jmin,jmin2;
float minangle,min2; for(i=0;i< dotprod.rows;i++) { minangle=angle.at<float>(i,0); jmin=1; min2=angle.at<float>(i,0); jmin2=1; for(j=1;j<dotprod.rows;j++) {<="" p="">
if(minangle > angle.at<float>(i,j))
{
minangle=angle.at<float>(i,j);
jmin=j;
}
if((min2>angle.at<float>(i,j)) && (angle.at<float>(i,j) > minangle))
{
min2=angle.at<float>(i,j);
jmin2=j;
}
}
if(minangle < 0.8*min2)
{
m.at<int>(i,0)=jmin;
//m.at<int>(i,0)=i;
}
else
{
m.at<int>(i,0)=jmin2;
//m.at<int>(i,0)=i;
}
}
vector <point2f> im1; vector <point2f> im2;
for(int z=0;z<= descriptors2.rows;z++) { for(int y=0;y<=descriptors2.cols;y++) {
im2.push_back(keypoints_2p[descriptors2.at<float>(z,y)].pt);
} }
for( int z=0;z<= descriptors1.rows;z++) { for( int y=0;y<=descriptors1.cols;y++) {
im1.push_back(keypoints_1p[descriptors1.at<float>(z,y)].pt);
} } //calculate the dx and dy for all features int v; vector <float> dx,dy ...
please at least try to format that wall of code ( edit-> mark -> 10101 button)