环境:linux+qtcreator2.3+opencv2.31
1.配置好Creator环境
2.新建工程
3.配置pro文件,将opencv库路径添加到pro中
4.编辑ui界面文件,绘制2个按钮和2个label,保存文件
右键添加槽函数
实现:
void MainWindow::on_openPushButton_clicked() { QString filename = QFileDialog::getOpenFileName(this,tr("Open Image"),"",tr("Image File(*.bmp *.jpg *.jpeg *.png *.ico)")); QTextCodec *code = QTextCodec::codecForName("gb18030"); std::string name = code->fromUnicode(filename).data(); cout<<name<<endl; origin_image= cv::imread(name,1); if(!origin_image.data) { QMessageBox msgBox; msgBox.setText(tr("image data is null")); msgBox.exec(); } else { int hight =ui->label->height(); int width =ui->label->width(); cout<<"hight is "<<hight<<" width is"<<width<<endl; cv::resize(origin_image,processed_image,cv::Size(width,hight)); cv::cvtColor(processed_image,processed_image,CV_BGR2RGB); cv::imshow("origin_image",origin_image); cv::imshow("processed_image",processed_image); img = QImage((const unsigned char*)(processed_image.data),processed_image.cols,processed_image.rows,QImage::Format_RGB888); ui->label->clear(); ui->label->setPixmap(QPixmap::fromImage(img)); ui->processPushButton->setEnabled(true); // ui->label->resize(ui->label->pixmap()->size()); } } void MainWindow::on_processPushButton_clicked() { //cv::Sobel(processed_image,processed_image,CV_8U,1,0); cv::flip(processed_image,processed_image,1); //cv::cvtColor(processed_image,processed_image,CV_BGR2RGB); img = QImage((const unsigned char*)(processed_image.data),processed_image.cols,processed_image.rows,QImage::Format_RGB888); ui->label2->clear(); ui->label2->setPixmap(QPixmap::fromImage(img)); ui->processPushButton->setEnabled(true); //ui->label2->resize(ui->label->pixmap()->size()); ui->label2->setPixmap(QPixmap::fromImage(img)); }
QString filename = QFileDialog::getOpenFileName(this,tr("Open Image"),"",tr("Image File(*.bmp *.jpg *.jpeg *.png *.ico)")); QTextCodec *code = QTextCodec::codecForName("gb18030"); std::string name = code->fromUnicode(filename).data();
cv::cvtColor(processed_image,processed_image,CV_BGR2RGB); img = QImage((const unsigned char*)(processed_image.data),processed_image.cols,processed_image.rows,QImage::Format_RGB888);
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = cvshow TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.ui INCLUDEPATH += G:\opencv2410\build\include CONFIG(release,debug|release){ LIBS += G:\opencv2410\build\x86\vc10\lib\opencv_calib3d2410.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_contrib2410.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_core2410.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_features2d2410.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_flann2410.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_gpu2410.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_highgui2410.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_imgproc2410.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_legacy2410.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_ml2410.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_nonfree2410.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_objdetect2410.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_photo2410.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_stitching2410.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_video2410.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_videostab2410.lib \ } CONFIG(debug,debug|release){ LIBS += G:\opencv2410\build\x86\vc10\lib\opencv_calib3d2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_contrib2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_core2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_features2d2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_flann2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_gpu2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_highgui2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_imgproc2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_legacy2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_ml2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_nonfree2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_objdetect2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_photo2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_stitching2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_ts2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_video2410d.lib \ G:\opencv2410\build\x86\vc10\lib\opencv_videostab2410d.lib \ }
OPENFILENAME ofn ; // a another memory buffer to contain the file name char szFile[100] ;
// open a file name TCHAR szFile[100] = {0}; //GetWindowText(szFile, _countof(szFile)); ZeroMemory( &ofn , sizeof( ofn)); ofn.lStructSize = sizeof ( ofn ); ofn.hwndOwner = NULL ; ofn.lpstrFile = szFile ; ofn.lpstrFile[0] = '\0'; ofn.nMaxFile = sizeof( szFile ); ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0"; ofn.nFilterIndex =1; ofn.lpstrFileTitle = NULL ; ofn.nMaxFileTitle = 0 ; ofn.lpstrInitialDir=NULL ; ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST ; GetOpenFileName( &ofn ); // Now simpley display the file name //MessageBox ( NULL , ofn.lpstrFile , "File Name" , MB_OK); std::cout<<ofn.lpstrFile<<std::endl; TRACE( "图片路径:%s\n",ofn.lpstrFile); //MFC程序不存在控制台,需要用TRACE输出 cv::Mat image =cv::imread(ofn.lpstrFile);
[filename,pathname]=uigetfile({'*.*';'*.jpg';'*.bmp';'*.gif'},'选择图片'); if isequal(filename,0) disp('Users Selected Canceled'); else str=[pathname filename]; im = imread(str); imshow(im);