|
| 1 | +// Create a coloured image in C++ using OpenCV. |
| 2 | + |
| 3 | +#include "opencv2/highgui/highgui.hpp" |
| 4 | +using namespace cv; |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +int main() |
| 8 | +{ |
| 9 | + // To create an image |
| 10 | + // CV_8UC3 depicts : (3 channels,8 bit image depth |
| 11 | + // Height = 500 pixels, Width = 1000 pixels |
| 12 | + // (0, 0, 100) assigned for Blue, Green and Red |
| 13 | + // plane respectively. |
| 14 | + // So the image will appear red as the red |
| 15 | + // component is set to 100. |
| 16 | + Mat img(500, 1000, CV_8UC3, Scalar(0,0, 100)); |
| 17 | + |
| 18 | + // check whether the image is loaded or not |
| 19 | + if (img.empty()) |
| 20 | + { |
| 21 | + cout << "\n Image not created. You" |
| 22 | + " have done something wrong. \n"; |
| 23 | + return -1; // Unsuccessful. |
| 24 | + } |
| 25 | + |
| 26 | + // first argument: name of the window |
| 27 | + // second argument: flag- types: |
| 28 | + // WINDOW_NORMAL If this is set, the user can |
| 29 | + // resize the window. |
| 30 | + // WINDOW_AUTOSIZE If this is set, the window size |
| 31 | + // is automatically adjusted to fit |
| 32 | + // the displayed image, and you cannot |
| 33 | + // change the window size manually. |
| 34 | + // WINDOW_OPENGL If this is set, the window will be |
| 35 | + // created with OpenGL support. |
| 36 | + namedWindow("A_good_name", CV_WINDOW_AUTOSIZE); |
| 37 | + |
| 38 | + // first argument: name of the window |
| 39 | + // second argument: image to be shown(Mat object) |
| 40 | + imshow("A_good_name", img); |
| 41 | + |
| 42 | + waitKey(0); //wait infinite time for a keypress |
| 43 | + |
| 44 | + // destroy the window with the name, "MyWindow" |
| 45 | + destroyWindow("A_good_name"); |
| 46 | + |
| 47 | + return 0; |
| 48 | +} |
0 commit comments