我是使用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& filename,int flags = 1)
我该如何解决这个问题?
答案 0 :(得分:3)
cv::Mat
没有接受字符串的构造函数。请改用imread
。由于imread
接受std::string
,而不接受QString
,只需执行:
cv::Mat yourImage = cv::imread(filename.toStdString());
答案 1 :(得分:0)
您不能使用cv :: Mat变量。要解决此问题,您应该使用" imread" function.I认为以下代码可以帮助您解决这个问题。您必须包含以下库。
#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.
}