Assertion Failure with cv::Size












0















I'm new to OpenCV and I stuck with the problem while declaring a Mat instance.



#include <opencv2opencv.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:opencv343opencvbuildincludeopencv2coremat.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.










share|improve this question




















  • 1





    Read the documentation of cv::Size. Doing so, you should notice that the first parameter of the constructor is width, but you're setting it to number of rows (i.e. height). | As with so many other questions here... documentation is your friend. Get familiar with it, and use it.

    – Dan Mašek
    Nov 19 '18 at 14:27













  • Also, it seems like all you're trying to do here is to swap channels 0 and 2. This is basically equivalent to a conversion between BGR and RGB. Hence, a simple cv::cvtColor(after, before, cv::COLOR_BGR2RGB) will do the same job as your two nested loops, and most likely much faster too.

    – Dan Mašek
    Nov 19 '18 at 14:31











  • You are right. the code switched them each other. Maybe it was not wrong because in many tutorials they were identical values (square matrix).

    – cadenzah
    Nov 19 '18 at 14:31


















0















I'm new to OpenCV and I stuck with the problem while declaring a Mat instance.



#include <opencv2opencv.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:opencv343opencvbuildincludeopencv2coremat.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.










share|improve this question




















  • 1





    Read the documentation of cv::Size. Doing so, you should notice that the first parameter of the constructor is width, but you're setting it to number of rows (i.e. height). | As with so many other questions here... documentation is your friend. Get familiar with it, and use it.

    – Dan Mašek
    Nov 19 '18 at 14:27













  • Also, it seems like all you're trying to do here is to swap channels 0 and 2. This is basically equivalent to a conversion between BGR and RGB. Hence, a simple cv::cvtColor(after, before, cv::COLOR_BGR2RGB) will do the same job as your two nested loops, and most likely much faster too.

    – Dan Mašek
    Nov 19 '18 at 14:31











  • You are right. the code switched them each other. Maybe it was not wrong because in many tutorials they were identical values (square matrix).

    – cadenzah
    Nov 19 '18 at 14:31
















0












0








0








I'm new to OpenCV and I stuck with the problem while declaring a Mat instance.



#include <opencv2opencv.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:opencv343opencvbuildincludeopencv2coremat.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.










share|improve this question
















I'm new to OpenCV and I stuck with the problem while declaring a Mat instance.



#include <opencv2opencv.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:opencv343opencvbuildincludeopencv2coremat.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.







opencv opencv3.0






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 14:09







cadenzah

















asked Nov 19 '18 at 14:00









cadenzahcadenzah

597




597








  • 1





    Read the documentation of cv::Size. Doing so, you should notice that the first parameter of the constructor is width, but you're setting it to number of rows (i.e. height). | As with so many other questions here... documentation is your friend. Get familiar with it, and use it.

    – Dan Mašek
    Nov 19 '18 at 14:27













  • Also, it seems like all you're trying to do here is to swap channels 0 and 2. This is basically equivalent to a conversion between BGR and RGB. Hence, a simple cv::cvtColor(after, before, cv::COLOR_BGR2RGB) will do the same job as your two nested loops, and most likely much faster too.

    – Dan Mašek
    Nov 19 '18 at 14:31











  • You are right. the code switched them each other. Maybe it was not wrong because in many tutorials they were identical values (square matrix).

    – cadenzah
    Nov 19 '18 at 14:31
















  • 1





    Read the documentation of cv::Size. Doing so, you should notice that the first parameter of the constructor is width, but you're setting it to number of rows (i.e. height). | As with so many other questions here... documentation is your friend. Get familiar with it, and use it.

    – Dan Mašek
    Nov 19 '18 at 14:27













  • Also, it seems like all you're trying to do here is to swap channels 0 and 2. This is basically equivalent to a conversion between BGR and RGB. Hence, a simple cv::cvtColor(after, before, cv::COLOR_BGR2RGB) will do the same job as your two nested loops, and most likely much faster too.

    – Dan Mašek
    Nov 19 '18 at 14:31











  • You are right. the code switched them each other. Maybe it was not wrong because in many tutorials they were identical values (square matrix).

    – cadenzah
    Nov 19 '18 at 14:31










1




1





Read the documentation of cv::Size. Doing so, you should notice that the first parameter of the constructor is width, but you're setting it to number of rows (i.e. height). | As with so many other questions here... documentation is your friend. Get familiar with it, and use it.

– Dan Mašek
Nov 19 '18 at 14:27







Read the documentation of cv::Size. Doing so, you should notice that the first parameter of the constructor is width, but you're setting it to number of rows (i.e. height). | As with so many other questions here... documentation is your friend. Get familiar with it, and use it.

– Dan Mašek
Nov 19 '18 at 14:27















Also, it seems like all you're trying to do here is to swap channels 0 and 2. This is basically equivalent to a conversion between BGR and RGB. Hence, a simple cv::cvtColor(after, before, cv::COLOR_BGR2RGB) will do the same job as your two nested loops, and most likely much faster too.

– Dan Mašek
Nov 19 '18 at 14:31





Also, it seems like all you're trying to do here is to swap channels 0 and 2. This is basically equivalent to a conversion between BGR and RGB. Hence, a simple cv::cvtColor(after, before, cv::COLOR_BGR2RGB) will do the same job as your two nested loops, and most likely much faster too.

– Dan Mašek
Nov 19 '18 at 14:31













You are right. the code switched them each other. Maybe it was not wrong because in many tutorials they were identical values (square matrix).

– cadenzah
Nov 19 '18 at 14:31







You are right. the code switched them each other. Maybe it was not wrong because in many tutorials they were identical values (square matrix).

– cadenzah
Nov 19 '18 at 14:31














1 Answer
1






active

oldest

votes


















0














This exception occurred because I set the width and height inside the Size() in wrong order.
Right order is cv::Size(width(cols)_of_image, height(rows)_of_image)



It was not a problem for many tutorials as they usually used image of square size.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376282%2fassertion-failure-with-cvsize%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This exception occurred because I set the width and height inside the Size() in wrong order.
    Right order is cv::Size(width(cols)_of_image, height(rows)_of_image)



    It was not a problem for many tutorials as they usually used image of square size.






    share|improve this answer




























      0














      This exception occurred because I set the width and height inside the Size() in wrong order.
      Right order is cv::Size(width(cols)_of_image, height(rows)_of_image)



      It was not a problem for many tutorials as they usually used image of square size.






      share|improve this answer


























        0












        0








        0







        This exception occurred because I set the width and height inside the Size() in wrong order.
        Right order is cv::Size(width(cols)_of_image, height(rows)_of_image)



        It was not a problem for many tutorials as they usually used image of square size.






        share|improve this answer













        This exception occurred because I set the width and height inside the Size() in wrong order.
        Right order is cv::Size(width(cols)_of_image, height(rows)_of_image)



        It was not a problem for many tutorials as they usually used image of square size.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 14:34









        cadenzahcadenzah

        597




        597
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376282%2fassertion-failure-with-cvsize%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini