0
votes

When running this code

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

I get the following error message:

libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(4.3.0) /tmp/opencv-20200408-5080-l00ytm/opencv-4.3.0/modules/imgcodecs/src/loadsave.cpp:738: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'

I don't know what I'm doing wrong.

1

1 Answers

2
votes
vector<cv::Mat> imgs(argc - 1);

should be

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.

Also as Gerardo Zinno said, you have an off by one error in your final for loop.

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

should be

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