OpenCV Image Reading, Display, and Core Matrix Operations

1. Image Reading and Display

The following code demonstrates reading an image from disk and displaying it in a window.

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main(int argc, char** argv) {
    // Read the image in grayscale mode
    Mat imageData = imread("path/to/image.jpg", IMREAD_GRAYSCALE);

    if (imageData.empty()) {
        cerr << "Failed to load the image file." << endl;
        return -1;
    }

    namedWindow("Image Viewer", WINDOW_NORMAL);
    imshow("Image Viewer", imageData);
    waitKey(0);
    return 0;
}

Key functions: imread, imshow.


2. Color Space Conversion and Image Saving

This example reads a color image, converts it to grayscale, displays the result, and saves the new grayscale image to a file.

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main(int argc, char** argv) {
    // Read the image in BGR color format
    Mat colorImage = imread("path/to/image.jpg");
    if (colorImage.empty()) {
        cerr << "Image loading failed." << endl;
        return -1;
    }
    namedWindow("Color Input", WINDOW_NORMAL);
    imshow("Color Input", colorImage);
    waitKey(0);

    // Convert BGR to Grayscale
    Mat grayImage;
    cvtColor(colorImage, grayImage, COLOR_BGR2GRAY);
    imshow("Grayscale Output", grayImage);
    waitKey(0);

    // Save the grayscale image
    imwrite("path/to/gray_output.jpg", grayImage);
    return 0;
}

Key functions: cvtColor, imwrite.


3. The Mat Object and Core Operations

The Mat object is OpenCV's primary container for image data. It consists of a header (metadata like width, height, type) and a data pointer to the pixel array.

Cloning vs. Copying vs. Assignment:

  • Cloning (clone()): Creates a deep copy with a new, independent data allocatino.
  • Copying (copyTo()): Similar to cloning, performs a deep copy.
  • Assignment (=): Creates a shallow copy; the new Mat header points to the same underlying data. Modifications to one affect the other.

Creating New Mat Objects:

#include <opencv2/opencv.hpp>
using namespace cv;

int main() {
    Mat src = imread("path/to/image.jpg", IMREAD_GRAYSCALE);

    // Deep copy via cloning
    Mat clonedMat = src.clone();

    // Deep copy via copyTo
    Mat copiedMat;
    src.copyTo(copiedMat);

    // Shallow copy via assignment
    Mat assignedMat = src;

    // Create a blank black image with the same size and type as src
    Mat blankBlack = Mat::zeros(src.size(), src.type());

    // Create a 512x1024 black image with 3 channels of 8-bit unsigned int
    Mat customSizeMat = Mat::zeros(Size(512, 1024), CV_8UC3);

    // Create a 512x512 white image
    Mat whiteImage = Mat::ones(Size(512, 512), CV_8UC3);

    // Define a small 3x3 kernel matrix manually
    Mat kernelMatrix = (Mat_<char>(3, 3) << 0, -1, 4,
                                              2, 4, 9,
                                              1, -3, 3);
    return 0;
}

Key concepts: Mat::clone, Mat::copyTo, assignment, Mat::zeros, Mat::ones, manual matrix initialization.

Tags: OpenCV C++ Image Processing Computer Vision Mat Object

Posted on Sun, 10 May 2026 11:36:29 +0000 by theresandy