2017-11-09 15 views
2

DTUのスタートラッカーが撮影した一連の画像を入力するプログラムを作成しています。背景が黒で星が白のROIで構成されています。私のプログラムは星を追跡する必要があります。OpenCv 3.3.1 C++のトラッキングでランダムクラッシュが発生する

コード:

#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2\video\tracking.hpp> 

#include <cmath> 
#include <string> 
#include <iostream> 
#include <sstream> 
#include <filesystem> 
#include <cstring> 


int main(int argc, char *argv[]) 
{ 

    //Tracking of points of interest: 
    cv::Mat img; 
    cv::UMat prevgray; 

    std::vector<cv::Point2f> featuresPrevious; 
    std::vector<cv::Point2f> featuresCurrent; 
    //int count = 0; 



    for (auto& p : std::experimental::filesystem::directory_iterator("C:\\Billeder")) 
    { 
     img = cv::imread(p.path().string(), CV_LOAD_IMAGE_GRAYSCALE); 

     //printf("Image number: %d\n", count); 
     //count++; 

     if (prevgray.empty() == false) 
     { 
      std::vector<cv::Point2f> featuresNextPos; 
      std::vector<uchar> featuresFound; 
      cv::Mat err; 


      featuresPrevious = std::move(featuresCurrent); 
      // create points of interest in current image: 
      goodFeaturesToTrack(img, featuresCurrent, 50, 0.0005, 16); //calculate the features for use in next iteration 



      // Track points of interest in current image: 
      calcOpticalFlowPyrLK(prevgray, img, featuresPrevious, featuresNextPos, featuresFound, err); 
      err.release(); 

      //printf("featuresNextPos length: %d \n", (int)featuresNextPos.size()); 
      //printf("featuresfound length: %d \n", (int)featuresFound.size()); 
      //Draw lines connecting previous position and current position 
      for (size_t i = 0; i<featuresNextPos.size(); i++) { 
       if (featuresFound[i]) { 

        float dist = sqrt(pow(std::abs(featuresPrevious[i].x - featuresNextPos[i].x), 2) + pow(std::abs(featuresPrevious[i].y - featuresNextPos[i].y), 2)); 
        if (dist > 15) { 
         // printf("The distance is too great to make sense \n"); 
        } 
        else { 
         //Draw line from previous features to next features: 
         line(img, featuresPrevious[i], featuresNextPos[i], cv::Scalar(255, 0, 0)); 
         if (std::abs(featuresPrevious[i].y - featuresNextPos[i].y)>9) 
         { 
          printf("Satellite might be found? Distance is: %f \n", dist); 
          //printf("Path of image containing satellite: %s \n", p.path().string()); 
         } 
        } 

        //Draw the features: 
        circle(img, featuresNextPos[i], 3, cv::Scalar(255, 0, 0),2); 
       } 
      } 


     } 
     else { 
      printf("Go here First\n"); 
      goodFeaturesToTrack(img, featuresCurrent, 50, 0.005, 16); //calculate the features for use in next iteration 

      //Move current image into "previous" image: 
      img.copyTo(prevgray); 
     } 

     // Print image to show 
     cv::namedWindow("Tracking Current", cv::WINDOW_AUTOSIZE); 
     imshow("Tracking Current", img); 


     img.copyTo(prevgray); 

     // wait for image to be printed 
     cv::waitKey(10); 
     //free memory again 
     img.release(); 
    } 


    cv::waitKey(0); 
    return 0; 

} 

が、私はランダムなC++のエラーにまで読んで、私はランダムなクラッシュが割り当てられていないメモリに起因するものである理解することができます何のためにされています。メモリが「無作為に」埋められ、空の場合はコードが実行され、そうでない場合はコードがクラッシュするクラッシュには系統的な発生はありません。またはgoodFeaturesToTrac私はいけないので、彼らがメモリを割り当てる方法を知っている。プログラムはので、私は実際に行くには何も持っていないクラッシュしたとき、私は何のエラーメッセージが表示されますない。

うまくいけば、任意のあなたのoverstack天才が私を助けることができる。

を敬具 Ditlev

含まれています結果的に画像になるので、あなたは何の仕事をしているのかを知ることができます。

First Image Second Image

+0

あなたは、少なくともエラーとスタックトレースを記述する必要がありますが、少なくとも、それが失敗していると間違っている可能性がものを見るために行知っています。 – api55

+0

私はエラーは一度も起こりません。プログラムはうまくいき、ランダムな量の画像に対して実行されます。私は、しかし、コードをoutcommentingされている問題は、私は、行calcOpticalFlowPyrLKになるときに発生します。私はスタックトレースが何であるかを調べなければならないでしょう。私はそれを得るとき私はそれを書きます。 –

答えて

0

私は私がエラーを発見したと思います。

私はstd::vector<cv::Point2f> featuresNextPos;を作成し、std::vector<cv::Point2f> featuresCurrent;を持っていました。それは本質的に同じです。 featuesNextPosはすべてのループで '作成'され、featuresCurrentは一度作成されたに過ぎません。私はこれ以上のクラッシュを取得

calcOpticalFlowPyrLK(prevgray, img, featuresPrevious, featuresNextPos, featuresFound, err);

calcOpticalFlowPyrLK(prevgray, img, featuresPrevious, featuresCurrent, featuresFound, err);

:行を変更することにより

。うまくいけば、これはただの幸運なストライキではなく永久的な修正でした。なぜ誰もがそれがクラッシュする理由を説明することができれば、それぞれのループはここでコメントすること自由に感じる。

よろしく Ditlev

関連する問題