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.
main()
function: The starting point of the program.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 thecv::Scalar
constructor.std::cout << "Image dimensions: " << image.rows << " x " << image.cols << std::endl;
: Outputs the dimensions (height and width) of the image using therows
andcols
attributes of thecv::Mat
object.std::cout << "Number of channels: " << image.channels() << std::endl;
: Outputs the number of channels in the image using thechannels()
method of thecv::Mat
object.cv::namedWindow("Image", cv::WINDOW_NORMAL);
: Creates a resizable window named "Image" usingcv::namedWindow()
with the optioncv::WINDOW_NORMAL
for normal window size.cv::imshow("Image", image);
: Displays the image inside the created window usingcv::imshow()
.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.cv::destroyAllWindows();
: Closes all OpenCV windows and deallocates associated memory.return 0;
: Indicates successful program execution and exits themain()
function.