繁体   English   中英

我的Qt C ++程序以异常方式终止

[英]My Qt C++ program terminates in an unusual way

我有一个gui应用程序,其中包含2个主要按钮,第一个可帮助用户选择10张图像并将其保存在Mat数组中。 第二个按钮用于背景扣除和绘制边界矩形。 该程序在我第一次运行时运行良好。 但是当我再次单击第一个按钮并选择其他图像并尝试使用第二个按钮处理它们时,有时效果很好,但有时我的程序以异常方式终止,而控制台中没有任何错误消息(仅此一个:退出代码255。)。 这是我的代码:

void Principale::chooseImages_clicked()
{
    QFileDialog* _f_dlg = new QFileDialog(this);
  _f_dlg->setFileMode(QFileDialog::ExistingFiles);
  _f_dlg->setOption(QFileDialog::DontUseNativeDialog, true);

  QListView *l = _f_dlg->findChild<QListView*>("listView");
  if (l) {
    l->setSelectionMode(QAbstractItemView::MultiSelection);
   }
  QTreeView *t = _f_dlg->findChild<QTreeView*>();
   if (t) {
     t->setSelectionMode(QAbstractItemView::MultiSelection);
    }

  _f_dlg->exec();
  _fnames = _f_dlg->selectedFiles();
  if(_fnames.size()!=10)
      QMessageBox::information(this, "Error!","choose 10 images!");

  else
  {
      image_choosed=true;
      for(int i=0;i<10;i++)
          Tab_IMG[i]=imread(_fnames.at(i).toStdString(),CV_16UC1);

      Entree entree;
      entree.setModal(true);
      entree.exec();
  }

void Principale::on_processing_clicked()
{
    if(!image_choosed)
        QMessageBox::information(this, "error!","choose 10 images!");
    else
    {
        chut=0;
        Mat BG; double moyenne=0;
        char chaine[20]="";int j=1;
        //Mat Subs;
        Vec3b couleur_dst,couleur_FG,couleur_sortie;
        for(int i=1;i<10;i++)
        {
            BG=imread(_fnames.first().toStdString(),CV_16UC1);


// background substraction

                Mat Subs(480, 640, CV_16UC1);
                absdiff(Tab_IMG[i],BG,Subs);
                BG.convertTo(BG,CV_8UC1,1.0/255.0);
                Tab_IMG[i].convertTo(Tab_IMG[i],CV_8UC1,1.0/255.0);
                Subs.convertTo(Subs,CV_8UC1);
                threshold(Subs,Subs,25, 255,THRESH_BINARY);
                Mat ones(3,3,CV_8UC1);
                morphologyEx(Subs,Subs,MORPH_OPEN,ones,Point(1,-1),1); // try 2 instead of 1
                erode(Subs,Subs,ones,Point(1,-1),1);

//--------------------- bounding rectangle 


            Rect bounding_rect;
            int largest_area=0;
            int largest_contour_index=0;
            Mat dst(Subs.rows,Subs.cols,CV_8UC1,Scalar::all(0));
            Mat thr=Subs;
            vector<vector<Point> > contours;
            vector<Vec4i> hierarchy;
            findContours( thr, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
            for( int k = 0; k< contours.size(); k++ )
            {
                double a=contourArea( contours[k],false);
                if(a>largest_area)
                {
                   largest_area=a;
                   largest_contour_index=k;
                   bounding_rect=boundingRect(contours[k]);
                }
            }

            Scalar color( 255,255,255);
            drawContours( dst, contours,largest_contour_index, color, CV_FILLED, 8, hierarchy );
            rectangle(dst, bounding_rect,Scalar(255,0,0),2, 8,0);
            int compteur=0; double moyenne=0;  int seuille=255; double depth;
            Mat sortie(Tab_IMG[i].rows,Tab_IMG[i].cols,CV_8UC1);
            sortie.setTo(0);
            Vec3b couleur_dst,couleur_FG,couleur_sortie;
            for(int x=0;x<dst.cols;x++)
            {
                for(int y=0;y<dst.rows;y++)
                {
                    couleur_dst= dst.at<Vec3b>(Point(x,y));
                    couleur_FG=Tab_IMG[i].at<Vec3b>(Point(x,y));
                    if((couleur_dst.val[0]==255)&&(couleur_dst.val[1]==255)&&(couleur_dst.val[2]==255))
                    {
                        sortie.at<Vec3b>(Point(x,y)) = couleur_FG;
                        seuille=couleur_FG.val[0];
                        depth=(3640*couleur_FG.val[0])/255;
                        moyenne+=depth;
                        compteur++;
                    }
                 }
             }
             moyenne=moyenne/compteur;
             Tab_dist[i]=moyenne;
             Tab_silh[i]=sortie;
             Tab_rect[i]=bounding_rect;
             Tab_surf[i]=bounding_rect.width*bounding_rect.height;
             if(i==1)
                  surf_one=Tab_surf[i];
             else
                  moy_dif_surf+=Tab_surf[i]-surf_one;
            Tab_rect[i]=bounding_rect;

         Tab_silh[0]=imread("C:\\Users\\Eden\\Desktop\\Images\\noire.png",CV_16UC1);
        /*Sortie sortie;
        sortie.setModal(true);
        sortie.exec();*/
    }
}

}

答案已从评论移至社区Wiki。

OP写道:

问题出在该指令sortie.at<Vec3b>(Point(x,y)) = couleur_FG; ,正是当我尝试将像素作为Vec3b像素访问时,这是不正确的,因为该图像被定义为单通道图像。 解决方案是使用uchar代替Vec3b

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM