c++ 无法将参数1从“std::filesystem::path”转换为“const cv::String &”(std::string)

pcww981p  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(536)

我有以下代码:

int upsample(DnnSuperResImpl model,
                      std::filesystem::path f,
                      std::filesystem::path s) {
    Mat img = cv::imread(f); //Read image
    Mat img;
    sr.upsample(img, img_new)
    cv::imwrite(s/ f.filename(), img); //Save upscaled image
    return 0;
}

有人知道为什么f和s不允许在不添加.string()的情况下编写吗?

jhkqcmku

jhkqcmku1#

这个错误消息告诉您cv::imread和cv::imwrite函数需要一个const cv::String&作为第一个参数,但是您传递给它们的是一个std::filesystem::path。
使用std::filesystem::path类的string()方法将file_path和保存_dir路径转换为std::string对象,然后将这些字符串传递给cv::imread和cv::imwrite函数。

int upsample_and_save(DnnSuperResImpl sr,
std::filesystem::path file_path,
std::filesystem::path save_dir) {
// Convert file_path and save_dir to strings
std::string file_path_str = file_path.string();
std::string save_dir_str = save_dir.string();

Copy code
// Read image using the string version of file_path
Mat img = cv::imread(file_path_str);

Mat img_new; //Container to store upsampled image
sr.upsample(img, img_new); //Upsample

// Save upscaled image using the string version of save_dir
cv::imwrite(save_dir_str / file_path.filename(), img_new);

return 0;
}

相关问题