多种GUI对图像进行简单操作(qt【linux和win7环境】、MFC、MATLAB)

环境:linux+qtcreator2.3+opencv2.31

1.配置好Creator环境

2.新建工程

多种GUI对图像进行简单操作(qt【linux和win7环境】、MFC、MATLAB)_第1张图片多种GUI对图像进行简单操作(qt【linux和win7环境】、MFC、MATLAB)_第2张图片多种GUI对图像进行简单操作(qt【linux和win7环境】、MFC、MATLAB)_第3张图片



3.配置pro文件,将opencv库路径添加到pro中

多种GUI对图像进行简单操作(qt【linux和win7环境】、MFC、MATLAB)_第4张图片



4.编辑ui界面文件,绘制2个按钮和2label,保存文件

多种GUI对图像进行简单操作(qt【linux和win7环境】、MFC、MATLAB)_第5张图片


右键添加槽函数

多种GUI对图像进行简单操作(qt【linux和win7环境】、MFC、MATLAB)_第6张图片多种GUI对图像进行简单操作(qt【linux和win7环境】、MFC、MATLAB)_第7张图片

实现:

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));
}

QT文件选择对话框获取文件名
    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();


4、实现效果:
    发现有些图显示正常,而有些图和原图对比失真严重
多种GUI对图像进行简单操作(qt【linux和win7环境】、MFC、MATLAB)_第8张图片 多种GUI对图像进行简单操作(qt【linux和win7环境】、MFC、MATLAB)_第9张图片

  原因在于图像在存储数据时的方式:opencv在存储图像宽度不是4的倍数时,对图像行进行了填补格式,以4补齐。这样如果Qimage的宽度不是4的倍数时会造成图像显示不正常。将Qimage宽度调整为4的倍数即可显示正常,这里对原图像进行了缩放,将其缩放成lable的大小,使label的宽度为4的倍数即可。另外 QImage是RGB排列,而Mat 是 BGR,在显示时需要进行转换。
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);
 
 
多种GUI对图像进行简单操作(qt【linux和win7环境】、MFC、MATLAB)_第10张图片
  路径包含中文时,无法读取图像。
环境:win7+qt5.51+opencv2.4.10
与linux下基本一样,将pro文件配置好即可。 
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 \
}

环境:Win7+VS2010+Opencv2.4.10
MFC:
   需要将图像转换成 CvvImage 格式

多种GUI对图像进行简单操作(qt【linux和win7环境】、MFC、MATLAB)_第11张图片


文件选择对话框
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);


打开/保存文件对话框:CFileDialog
 
  
 
  
MATLAB+GUI
   打开文件对话框:uigetfile
[filename,pathname]=uigetfile({'*.*';'*.jpg';'*.bmp';'*.gif'},'选择图片');
if isequal(filename,0)
    disp('Users Selected Canceled');
else
str=[pathname filename];
im = imread(str);
imshow(im);


多种GUI对图像进行简单操作(qt【linux和win7环境】、MFC、MATLAB)_第12张图片






你可能感兴趣的:(多种GUI对图像进行简单操作(qt【linux和win7环境】、MFC、MATLAB))