OpenCV C++ Program to create a single colored blank image

The following is the explanation to the C++ code to create a single colored blank image in C++ using the tool OpenCV.

Things to know: 

(1) The code will only compile in Linux environment.

(2) To run in windows, please use the file: ‘blank.o’ and run it in cmd. However if it does not run (problem in system architecture) then compile it in windows by making suitable and obvious changes to the code like: Use <iostream.h> in place of <iostream>.

(3) Compile command: g++ -w blank.cpp -o blank `pkg-config –libs opencv`



(4) Run command: ./article

Before you run the code, please make sure that you have OpenCV installed on your system.

Code Snippet:

// Title: Create a coloured image in C++ using OpenCV.

// highgui - an easy-to-use interface to 
// video capturing, image and video codecs,
// as well as simple UI capabilities.
#include "opencv2/highgui/highgui.hpp"

// Namespace where all the C++ OpenCV 
// functionality resides. 
using namespace cv;

// For basic input / output operations. 
// Else use macro 'std::' everywhere.
using namespace std;

int main()
{
    // To create an image
    // CV_8UC3 depicts : (3 channels,8 bit image depth
    // Height  = 500 pixels, Width = 1000 pixels
    // (0, 0, 100) assigned for Blue, Green and Red 
    //             plane respectively. 
    // So the image will appear red as the red 
    // component is set to 100.
    Mat img(500, 1000, CV_8UC3, Scalar(0,0, 100));
    
    // check whether the image is loaded or not
    if (img.empty()) 
    {
        cout << "\n Image not created. You"
                     " have done something wrong. \n";
        return -1;    // Unsuccessful.
    }

    // first argument: name of the window
    // second argument: flag- types: 
    // WINDOW_NORMAL If this is set, the user can 
    //               resize the window.
    // WINDOW_AUTOSIZE If this is set, the window size
    //                is automatically adjusted to fit 
    //                the displayed image, and you cannot
    //                change the window size manually.
    // WINDOW_OPENGL  If this is set, the window will be
    //                created with OpenGL support.
    namedWindow("A_good_name", CV_WINDOW_AUTOSIZE);

    // first argument: name of the window
    // second argument: image to be shown(Mat object)
    imshow("A_good_name", img);
    
    waitKey(0); //wait infinite time for a keypress

    // destroy the window with the name, "MyWindow"
    destroyWindow("A_good_name");            

    return 0;
}
// END OF PROGRAM

 

Previous Post:

https://www.geeksforgeeks.org/opencv-c-program-to-blur-an-image/

About the Author: 

Aditya Prakash is an undergraduate student at Indian Institute Adityaof Information Technology, Vadodara. He primarily codes in C++. The motto for him is: So far so good. He plays cricket, watches superhero movies, football and is a big fan of answering questions.

If you also wish to showcase your blog here, please see GBlog for guest blog writing on GeeksforGeeks.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.