Create a blank image and Display

The following code is used to create the image of specific colour. Mat img(500, 1000, CV_8UC3, Scalar(0,0, 100)) is used to create the image of size 500 x 1000, data type of array is CV_8UC3 means 8 bit unsigned int and there are 3 channels for Red, Green and Blue colour. Here, the value assigned to Blue is 0, Green is 0 and Red is 100. So, the image will be displayed in red colour here. It follows the sequence BGR in C++. Mat is a data structure which is used to store image matrix.

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main()
       //create an image ( 3 channels, 8 bit image depth, 500 high, 1000 wide, (0, 0, 100) assigned for Blue, Green and Red plane respectively. )      
       Mat img(500, 1000, CV_8UC3, Scalar(0,0, 100)); 

       //create a window with the name "MyWindow"       
       namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); 
       //display the image which is stored in the 'img' in the "MyWindow" window
       imshow("MyWindow", img); 

       //wait infinite time for a keypress

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

       return 0;

To compile use

$ sudo g++ -o nameofFile nameofFile.cpp `pkg-config opencv --cflags --libs`

To run use

$ ./nameofFile


Red image will be displayed.


Published by


I am the one who discover myself daily through reading, writing, interacting with people and expressing my thoughts to inspire them if I would be able to bring a little change to the society through my work. I write blogs, participate in various social activities and want to be a Professional Speaker. Currently, I am pursuing my post graduation in Computer Applications from Guru Nanak Dev Engineering College, Ludhiana and I’m very much active in many technical & social communities.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s