我有一个包含2个主按钮的gui应用程序,第一个帮助用户选择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();*/
}
}
}
答案 0 :(得分:0)
(Answer moved from comments to a community wiki.)
OP写道:问题在于此指令
sortie.at<Vec3b>(Point(x,y)) = couleur_FG;
,正是当我尝试将像素视为Vec3b
时,因为图像被定义为单通道图像。解决方案是使用uchar
代替Vec3b
。