QT讀取本地txt影象資料並在介面上顯示該影象
阿新 • • 發佈:2018-12-19
int pixel[180*1500]; int i=0; char filenametxt[150] = "inputimg.txt"; ifstream infile(filenametxt,ios::in); //測試檔案是否成功開啟 if(!infile) { qDebug()<<"open error!"; exit(1); } //讀取檔案中的資料 while(!infile.eof()) { infile>>pixel[i]; i++; } infile.close(); //將資料寫到Mat矩陣中 Mat inputimg = Mat::zeros(180,1500,CV_8UC1); int j=0; for(int m=0;m<inputimg.rows;m++) { for(int n=0;n<inputimg.cols;n++) { inputimg.at<uchar>(m,n)=pixel[j]; j++; } } //在介面label上顯示 QImage Q_out_Img = MattoQImage.Mat2QImage(inputimg); const QSize s_out = ui->label_outputImg->size() ; ui->label_outputImg->setPixmap( QPixmap::fromImage( Q_out_Img ).scaled(s_out,Qt::KeepAspectRatio ) );