使用imwrite OpenCV时断言失败

运行此代码时

int main(int argc, char *argv[])
{
    vector<cv::Mat> imgs(argc - 1);
    for (auto i = 1; i < argc; ++i)
    {
        cv::Mat img = cv::imread(argv[i]);
        cv::Mat dst;
        cv::GaussianBlur(img, dst, cv::Size(3, 3), 0, 0, cv::BORDER_DEFAULT);
        cv::Sobel(dst, dst, -1, 1, 1);
        imgs.push_back(dst);
    }

    for (auto i = 1; i < argc; ++i)
    {
        string filename(argv[i]);
        filename[0] = toupper(filename[0]);
        filename.end()[-5] = 'a';
        cv::imwrite(filename, imgs[i]);
    }
}

我收到以下错误消息:

libc ++ abi.dylib:以类型未捕获的异常终止   cv :: Exception:OpenCV(4.3.0)   /tmp/opencv-20200408-​​5080-l00ytm/opencv-4.3.0/modules/imgcodecs/src/loadsave.cpp:738:   错误:(-215:声明失败)函数'imwrite'中的__img.empty()

我不知道我在做什么错。

评论
avelit
avelit
vector<cv::Mat> imgs(argc - 1);

应该

vector<cv::Mat> imgs;

You don't create the vector at the required size and then use push_back.

Either create the vector at zero size and use push_back to add items to it, or create the vector at the required size and use subscripting to assign items to it.

就像Gerardo Zinno所说的那样,您在最终的for循环中犯了一个错误。

cv::imwrite(filename, imgs[i]);

应该

cv::imwrite(filename, imgs[i-1]); 
点赞
评论