I'm new to OpenCV and I stuck with the problem while declaring a Mat instance.
#include <opencv2\opencv.hpp>
int main(int argc, char *argv[]) {
cv::Mat before = cv::imread("./irene.jpg", CV_LOAD_IMAGE_COLOR);
cv::imshow("before", before);
cv::waitKey(0);
cv::Mat after(cv::Size(before.rows, before.cols), CV_8UC3);
for (int y = 0; y < before.rows; y++) {
for (int x = 0; x < before.cols; x++) {
after.at<cv::Vec3b>(y, x)[0] = before.at<cv::Vec3b>(y, x)[2];
after.at<cv::Vec3b>(y, x)[1] = before.at<cv::Vec3b>(y, x)[1];
after.at<cv::Vec3b>(y, x)[2] = before.at<cv::Vec3b>(y, x)[0];
}
}
cv::imshow("after", after);
cv::waitKey(0);
return 0;
}
Just a simple code for changing color of an image. The problem is that, when I try to use cv::Size(), the compiler returns Assertion Failure. It builds okay, but when I try to press any key to go next of the code, it makes an exception.
What the compiler references and put on the console is:
OpenCV(3.4.3) Error: Assertion failed ((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels())) in cv::Mat::at, file c:\opencv343\opencv\build\include\opencv2\core\mat.inl.hpp, line 1101
I'm certain on that the exception is thrown because of the Size() struct, because if I change Size(before.rows, before.cols) to before.rows, before.cols, it works fine!
I cannot figure out what is wrong, but all the tutorials say the code I tried is okay code.