OpenCV C++ Program to create a single colored blank image
Last Updated :
19 Dec, 2022
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
of 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.
Similar Reads
OpenCV C++ Program to blur an image The following is the explanation to the C++ code to blur an Image in C++ using the tool OpenCV. Things to know: (1) The code will only compile in Linux environment. (2) Compile command: g++ -w article.cpp -o article `pkg-config --libs opencv` (3) Run command: ./article (4) The image bat.jpg has to b
5 min read
Image Processing in Java - Colored to Red Green Blue Image Conversion Prerequisites:Â Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image Conversion In this set, we will be converting a colored image to an i
5 min read
OpenCV C++ Program to blur a Video The following is the explanation to the C++ code to blur a video 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: 'blur_video.o' and run it in cmd. However if it does not run(problem in system architecture)
3 min read
Image Processing in Java - Colored Image to Sepia Image Conversion Prerequisites:Â Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image
3 min read
Color Identification in Images using Python - OpenCV An open-source library in Python, OpenCV is basically used for image and video processing. Not only supported by any system, such as Windows, Linux, Mac, etc. but also it can be run in any programming language like Python, C++, Java, etc. OpenCV also allows you to identify color in images. Donât you
3 min read