image shapes in opencv c++

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

int main() {
    cv::Mat image(300, 400, CV_8UC3, cv::Scalar(0, 255, 255));
    std::cout << "Image dimensions: " << image.rows << " x " << image.cols << std::endl;
    std::cout << "Number of channels: " << image.channels() << std::endl;

    cv::namedWindow("Image", cv::WINDOW_NORMAL);
    cv::imshow("Image", image);

    cv::waitKey(0);
    cv::destroyAllWindows();

    return 0;
}

This code demonstrates how to create an OpenCV image, determine its dimensions, and display it in a window.

Explanation for each step: 1. #include statements: Includes necessary libraries for input/output operations, matrix operations, image processing, and GUI functionalities provided by OpenCV.

  1. main() function: The starting point of the program.

  2. cv::Mat image(300, 400, CV_8UC3, cv::Scalar(0, 255, 255));: Creates a 3-channel (CV_8UC3) image with a size of 300x400 pixels and fills it with a yellow color (0, 255, 255) using the cv::Scalar constructor.

  3. std::cout << "Image dimensions: " << image.rows << " x " << image.cols << std::endl;: Outputs the dimensions (height and width) of the image using the rows and cols attributes of the cv::Mat object.

  4. std::cout << "Number of channels: " << image.channels() << std::endl;: Outputs the number of channels in the image using the channels() method of the cv::Mat object.

  5. cv::namedWindow("Image", cv::WINDOW_NORMAL);: Creates a resizable window named "Image" using cv::namedWindow() with the option cv::WINDOW_NORMAL for normal window size.

  6. cv::imshow("Image", image);: Displays the image inside the created window using cv::imshow().

  7. cv::waitKey(0);: Waits indefinitely (0 indicates waiting for any keypress) for a keyboard event. This keeps the window open until a key is pressed.

  8. cv::destroyAllWindows();: Closes all OpenCV windows and deallocates associated memory.

  9. return 0;: Indicates successful program execution and exits the main() function.