Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here is a sample image, and C++ code along with it:

image description

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world340.lib")

#include <iostream>
#include <vector>
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 (unsigned short int x = 0; x < frame.cols; x++)
    {
        for (unsigned short int y = 0; y < frame.rows; y++)
        {
            if (x == 0 || x == (frame.cols - 1) || y == 0 || y == (frame.rows - 1))
            {
                // Make a blue border around the image
                size_t index = y*frame.cols*num_channels + x*num_channels;
                frame.data[index] = 255; // B
                frame.data[index + 1] = 127; // G
                frame.data[index + 2] = 0; // R 
            }
        }
    }

    imshow("frame", frame);

    waitKey();

    return 0;
}

Here is a sample image, and some C++ code to go along with it:

image description

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world340.lib")

#include <iostream>
#include <vector>
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 (unsigned short int x = 0; x < frame.cols; x++)
    {
        for (unsigned short int y = 0; y < frame.rows; y++)
        {
            if (x == 0 || x == (frame.cols - 1) || y == 0 || y == (frame.rows - 1))
            {
                // Make a blue border around the image
                size_t index = y*frame.cols*num_channels + x*num_channels;
                frame.data[index] frame.data[index + 0] = 255; // B
                frame.data[index + 1] = 127; // G
                frame.data[index + 2] = 0; // R 
            }
        }
    }

    imshow("frame", frame);

    waitKey();

    return 0;
}

Here is a sample image, and some C++ code to go along with it:

image description

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

#include <iostream>
#include <vector>
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 (unsigned short int x = 0; x < frame.cols; x++)
    {
        for (unsigned short int y = 0; y < frame.rows; y++)
        {
            if (x == 0 || x == (frame.cols - 1) || y == 0 || y == (frame.rows - 1))
            {
                // Make a blue border around the image
                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;
}

Here is a sample image, and some C++ code to go along with it:

image description

#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 (unsigned short int (int x = 0; x < frame.cols; x++)
    {
        for (unsigned short int (int y = 0; y < frame.rows; y++)
        {
            if (x == 0 || x == (frame.cols - 1) || y == 0 || y == (frame.rows - 1))
            {
                // Make a blue border around the image
                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;
}

Here is a sample image, and some C++ code to go along with it:

image description

#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))
            {
                // Make a blue border around the image
                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;
}

Here Below is a sample image, and image. The image is followed by some C++ code to go along with it:code. If you find that my answer is correct, then please upvote my answer and mark it as correct. Have fun!

image description

#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;
}

Below is a sample image. The image is followed by some C++ code. If you find that my answer is correct, then please upvote my answer and mark it as correct. Have fun!

image description

#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;
}

Since you seem to have problems with the above code, here is a simpler code. Note that I switched the columns and rows variables around, so that it makes more sense.

#include <iostream>
#include <vector>
using namespace std;

int main(void)
{
    size_t rows = 4;
    size_t columns = 3;

    vector<int> int_array(rows*columns);

    int count = 0;

    for(size_t x = 0; x < rows; x++)
    {
        for(size_t y = 0; y < columns; y++)
        {
            size_t index = y*rows + x;

            int_array[index] = count;
            count++;
        }
    }

    for(size_t x = 0; x < rows; x++)
    {
        for(size_t y = 0; y < columns; y++)
        {
            size_t index = y*rows + x;

            cout << int_array[index] << ' ';
        }

        cout << endl;
    }

    return 0;
}

Below is a sample image. The image is followed by some C++ code. If you find that my answer is correct, then please upvote my answer and mark it as correct. Have fun!

image description

#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;
}

Since you seem to have problems with the above code, here is a simpler code. Note that I switched the columns and rows variables around, so that it makes more sense.sense. Here we treat a vector of ints as a 2D array.

#include <iostream>
#include <vector>
using namespace std;

int main(void)
{
    size_t rows = 4;
    size_t columns = 3;

    vector<int> int_array(rows*columns);

    int count = 0;

    for(size_t x = 0; x < rows; x++)
    {
        for(size_t y = 0; y < columns; y++)
        {
            size_t index = y*rows + x;

            int_array[index] = count;
            count++;
        }
    }

    for(size_t x = 0; x < rows; x++)
    {
        for(size_t y = 0; y < columns; y++)
        {
            size_t index = y*rows + x;

            cout << int_array[index] << ' ';
        }

        cout << endl;
    }

    return 0;
}

Below is a sample image. The image is followed by some C++ code. If you find that my answer is correct, then please upvote my answer and mark it as correct. Have fun!

image description

#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;
}

Since you seem to have problems with the above code, here is a simpler code. Note that I switched the columns and rows variables around, so that it makes more sense. around. Here we treat a vector of ints as a 2D array.array. Once you understand this code, then go back to the code given above... note that the rows and columns are swapped around.

#include <iostream>
#include <vector>
using namespace std;

int main(void)
{
    size_t rows = 4;
    size_t columns = 3;

    vector<int> int_array(rows*columns);

    int count = 0;

    for(size_t x = 0; x < rows; x++)
    {
        for(size_t y = 0; y < columns; y++)
        {
            size_t index = y*rows + x;

            int_array[index] = count;
            count++;
        }
    }

    for(size_t x = 0; x < rows; x++)
    {
        for(size_t y = 0; y < columns; y++)
        {
            size_t index = y*rows + x;

            cout << int_array[index] << ' ';
        }

        cout << endl;
    }

    return 0;
}

Below is a sample image. The image is followed by some C++ code. If you find that my answer is correct, then please upvote my answer and mark it as correct. Have fun!

image description

#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;
}

Since you seem to have problems with the above code, here is a simpler code. Note that I switched the columns and rows variables around. Here we treat a vector of ints as a 2D array. Once you understand this the following code, then go back to the code given above... above, and note that the rows and columns are swapped around.around although the same indexing system is used.

#include <iostream>
#include <vector>
using namespace std;

int main(void)
{
    size_t rows = 4;
    size_t columns = 3;

    vector<int> int_array(rows*columns);

    int count = 0;

    for(size_t x = 0; x < rows; x++)
    {
        for(size_t y = 0; y < columns; y++)
        {
            size_t index = y*rows + x;

            int_array[index] = count;
            count++;
        }
    }

    for(size_t x = 0; x < rows; x++)
    {
        for(size_t y = 0; y < columns; y++)
        {
            size_t index = y*rows + x;

            cout << int_array[index] << ' ';
        }

        cout << endl;
    }

    return 0;
}