我是新的使用Qt和OpenCV。我正试图在我的高清中读取图像并显示它。它不是一个特定的图像,程序可以读取用户选择的任何图像。我的代码:
QString Imagename = QFileDialog::getOpenFileName(
this,
tr("Open Images"),
"C://",
tr("Tiff Files (*.tif);; Raw file (*.raw)"));
if ( Imagename.isNull())
{
QMessageBox::warning(this,"Error!","Image not valid!");
}
cv::Mat src(filename);
垫配置为:Mat imread(const string&文件名,int flags=1)
我该如何解决这个问题?
cv::Mat
没有接受字符串的构造函数。使用imread
代替。由于imread
接受std::string
,而不是QString
,因此只需执行:
cv::Mat yourImage = cv::imread(filename.toStdString());
不能使用cv::Mat变量。要解决这个问题,你应该使用"imread"功能。我想下面的代码会帮助你解决这个问题。您必须包含以下库:
#include<QFileDialog>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
int main (){
// Gets file name with QFileDialog
QString file_name=QFileDialog::getOpenFileName(this,"Open Image File","C://","Image File (*.jpg *.tiff *.png *.bmp)");
// Read image with Color Image Parameter and store on image variable which type is cv::Mat
// You should convert file name from QString to StdString to use in imread function
cv::Mat image = cv::imread(file_name.toStdString(),CV_LOAD_IMAGE_COLOR);
if(!image.data){ // Checks whether the image was read successfully
qDebug()<< "Could not open or find the image";
return -1;
}
cv::namedWindow("Original Image",WINDOW_AUTOSIZE); // Creates a window which will display image
cv::imshow("Original Image",image); // Shows image on created window
cv::waitKey(0); // Waits for a keystroke in the window
return 0; // if you created console app in qt you should use return a.exec() instead of this.
}