Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Does image.at<vec3b>(x,y)[3] == image.at<vec3b>(x+1,y)[0], or something like that?

In any case, why would you do that when you know that'll cause a memory access violation / segfault?

And why not use the data member pointer, instead of the at function? See the following code that reads an OpenCV Mat by using the data member:

#include <opencv2/opencv.hpp>
using namespace cv;

#include <iostream>
using namespace std;

int main(void)
{
    Mat frame = imread("sparks.png");

    if (frame.empty())
    {
        cout << "Error loading image file" << endl;
        return -1;  
    }   

    const size_t num_channels = 3;

    for (int x = 0; x < frame.cols; x++)
    {
        for (int y = 0; y < frame.rows; y++)
        {
            // Make a blue border around the image
            if (x == 0 || x == (frame.cols - 1) || y == 0 || y == (frame.rows - 1))
            {
                size_t index = y*frame.cols*num_channels + x*num_channels;

                frame.data[index + 0] = 255; // B
                frame.data[index + 1] = 127; // G
                frame.data[index + 2] = 0; // R 
            }
        }
    }

    imshow("frame", frame);

    waitKey();

    return 0;
}

Does image.at<vec3b>(x,y)[3] == image.at<vec3b>(x+1,y)[0], or something like that?

In any case, why would you do that when you know that'll cause a memory access violation / segfault?

And why not use the data member pointer, instead of the at function? See the following code that reads an OpenCV Mat by using the data member:

#include <opencv2/opencv.hpp>
using namespace cv;

#include <iostream>
using namespace std;

int main(void)
{
    Mat frame = imread("sparks.png");

    if (frame.empty())
    {
        cout << "Error loading image file" << endl;
        return -1;  
    }   

    const size_t num_channels = 3;

    for (int x = 0; x < frame.cols; x++)
    {
        for (int y = 0; y < frame.rows; y++)
        {
            // Make a blue border around the image
            if (x == 0 || x == (frame.cols - 1) || y == 0 || y == (frame.rows - 1))
            {
                size_t index = y*frame.cols*num_channels + x*num_channels;

                frame.data[index + 0] = 255; // B
                frame.data[index + 1] = 127; // G
                frame.data[index + 2] = 0; // R 
            }
        }
    }

    imshow("frame", frame);

    waitKey();

    return 0;
}

Sample image:

image description

Does image.at<vec3b>(x,y)[3] == image.at<vec3b>(x+1,y)[0], or something like that?

In any case, why would you do that when you know that'll cause a memory access violation / segfault?

And why not use the data member pointer, instead of the at function? See the following code that reads an OpenCV Mat by using the data member:

#include <opencv2/opencv.hpp>
using namespace cv;

#include <iostream>
using namespace std;

int main(void)
{
    Mat frame = imread("sparks.png");

    if (frame.empty())
    {
        cout << "Error loading image file" << endl;
        return -1;  
    }   

    const size_t num_channels = 3;

    for (int x = 0; x < frame.cols; x++)
    {
        for (int y = 0; y < frame.rows; y++)
        {
            // Make a blue border around the image
            if (x == 0 || x == (frame.cols - 1) || y == 0 || y == (frame.rows - 1))
            {
                size_t index = y*frame.cols*num_channels + x*num_channels;

                frame.data[index + 0] = 255; // B
                frame.data[index + 1] = 127; // G
                frame.data[index + 2] = 0; // R 
            }
        }
    }

    imshow("frame", frame);

    waitKey();

    return 0;
}

Sample image:image, sparks.png:

image description

Does image.at<vec3b>(x,y)[3] == image.at<vec3b>(x+1,y)[0], or something like that?

In any case, why would you do that when you know that'll cause a memory access violation / segfault?

And why not use the data member pointer, instead of the at function? See the following code that reads an OpenCV Mat by using the data member:member.

If you find my answer to be helpful, then please upvote it and mark it as correct. Thank you.

#include <opencv2/opencv.hpp>
using namespace cv;

#include <iostream>
using namespace std;

int main(void)
{
    Mat frame = imread("sparks.png");

    if (frame.empty())
    {
        cout << "Error loading image file" << endl;
        return -1;  
    }   

    const size_t num_channels = 3;

    for (int x = 0; x < frame.cols; x++)
    {
        for (int y = 0; y < frame.rows; y++)
        {
            // Make a blue border around the image
            if (x == 0 || x == (frame.cols - 1) || y == 0 || y == (frame.rows - 1))
            {
                size_t index = y*frame.cols*num_channels + x*num_channels;

                frame.data[index + 0] = 255; // B
                frame.data[index + 1] = 127; // G
                frame.data[index + 2] = 0; // R 
            }
        }
    }

    imshow("frame", frame);

    waitKey();

    return 0;
}

Sample image, sparks.png:

image description

Does image.at<vec3b>(x,y)[3] == image.at<vec3b>(x+1,y)[0], or image.at<vec3b>(x,y)[3] == image.at<vec3b>(x,y+1)[0], or something like that?

In any case, why would you do that when you know that'll cause a memory access violation / segfault?

And why not use the data member pointer, instead of the at function? See the following code that reads an OpenCV Mat by using the data member.

If you find my answer to be helpful, then please upvote it and mark it as correct. Thank you.

#include <opencv2/opencv.hpp>
using namespace cv;

#include <iostream>
using namespace std;

int main(void)
{
    Mat frame = imread("sparks.png");

    if (frame.empty())
    {
        cout << "Error loading image file" << endl;
        return -1;  
    }   

    const size_t num_channels = 3;

    for (int x = 0; x < frame.cols; x++)
    {
        for (int y = 0; y < frame.rows; y++)
        {
            // Make a blue border around the image
            if (x == 0 || x == (frame.cols - 1) || y == 0 || y == (frame.rows - 1))
            {
                size_t index = y*frame.cols*num_channels + x*num_channels;

                frame.data[index + 0] = 255; // B
                frame.data[index + 1] = 127; // G
                frame.data[index + 2] = 0; // R 
            }
        }
    }

    imshow("frame", frame);

    waitKey();

    return 0;
}

Sample image, sparks.png:

image description

Does image.at<vec3b>(x,y)[3] == image.at<vec3b>(x+1,y)[0], or image.at<vec3b>(x,y)[3] == image.at<vec3b>(x,y+1)[0], or something like that?

In any case, why would you do that when you know that'll cause a memory access violation / segfault?

And why not use the data member pointer, instead of the at function? See the following code that reads alters an OpenCV Mat by using the data member.

If you find my answer to be helpful, then please upvote it and mark it as correct. Thank you.

#include <opencv2/opencv.hpp>
using namespace cv;

#include <iostream>
using namespace std;

int main(void)
{
    Mat frame = imread("sparks.png");

    if (frame.empty())
    {
        cout << "Error loading image file" << endl;
        return -1;  
    }   

    const size_t num_channels = 3;

    for (int x = 0; x < frame.cols; x++)
    {
        for (int y = 0; y < frame.rows; y++)
        {
            // Make a blue border around the image
            if (x == 0 || x == (frame.cols - 1) || y == 0 || y == (frame.rows - 1))
            {
                size_t index = y*frame.cols*num_channels + x*num_channels;

                frame.data[index + 0] = 255; // B
                frame.data[index + 1] = 127; // G
                frame.data[index + 2] = 0; // R 
            }
        }
    }

    imshow("frame", frame);

    waitKey();

    return 0;
}

Sample image, sparks.png:

image description