how to change colour image to grey in opencv c++

#include <opencv2/opencv.hpp>

int main() {
    // Read the input color image
    cv::Mat colorImage = cv::imread("input_image.jpg");

    // Check if the image is successfully loaded
    if (colorImage.empty()) {
        std::cerr << "Error: Unable to read the input image." << std::endl;
        return -1;
    }

    // Convert the color image to grayscale
    cv::Mat grayImage;
    cv::cvtColor(colorImage, grayImage, cv::COLOR_BGR2GRAY);

    // Save the grayscale image
    cv::imwrite("output_image_gray.jpg", grayImage);

    return 0;
}