Open In App

C++ – Finding Maximum Value in Image using OpenCV

Last Updated : 31 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Suppose you have an image stored in a matrix in C++ and you want to find the maximum value among all the pixels in the image. The matrix may be of any type, such as ‘uchar’ (for 8-bit unsigned integers, which are commonly used to represent pixels in an image), ‘int’, or ‘float’. Your task is to write a C++ function that takes an image matrix as input and returns the maximum value among all the pixels in the image. We use OpenCV for solving this task.

What is OpenCV?

OpenCV (Open Source Computer Vision) is a free and open-source library of computer vision and machine learning algorithms that can be used to process and analyze images and video. It was developed by Intel in the late 1990s and is now maintained by a team of developers at Willow Garage and Itseez. OpenCV is written in C++ and has interfaces for Python, Java, and other programming languages. It provides a wide range of functionality, including image and video capture and analysis, basic image processing operations (such as resizing, cropping, and color space conversions), object detection and recognition, and machine learning algorithms. OpenCV is widely used in computer vision applications, including facial recognition, object tracking, and augmented reality, as well as in a variety of other fields, such as robotics and medical imaging. It is a powerful tool for working with images and video and is widely used in the field of computer vision.

To find the maximum value in an image in C++, you can follow these steps:

  1. Load the image into memory using an image processing library such as OpenCV or CImg.
  2. Iterate through each pixel in the image and compare the value of the pixel to the current maximum value. If the pixel value is greater than the current maximum value, update the maximum value.
  3. Continue iterating through all the pixels in the image until you have found the maximum value. 

Here is some example code that demonstrates how to do this using the OpenCV library:

C++




#include <iostream>
#include <opencv2/opencv.hpp>
  
int main(int argc, char* argv[])
{
    // Load the image
    cv::Mat image
        = cv::imread("image.jpg", cv::IMREAD_GRAYSCALE);
  
    // Initialize the maximum value
    // to the minimum possible
    // value for the image type
    int maxValue = std::numeric_limits<uchar>::min();
  
    // Iterate through each pixel in the image
    for (int y = 0; y < image.rows; y++) {
        for (int x = 0; x < image.cols; x++) {
            // Get the pixel value
            int pixelValue = image.at<uchar>(y, x);
  
            // Update the maximum value if the pixel value
            // is greater than the current maximum
            if (pixelValue > maxValue) {
                maxValue = pixelValue;
            }
        }
    }
  
    std::cout << "The maximum value in the image is: "
              << maxValue << std::endl;
  
    return 0;
}


Input:

image[rows][cols]=           {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
                           {9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
                           {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
                           {9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
                           {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
                           {9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
                           {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
                           {9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
                           {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
                           {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}}

Output:

The maximum value in the image is: 9

This code loads an image in grayscale format, iterates through each pixel in the image, and keeps track of the maximum pixel value. The maximum value is then printed to the console.

Note: This code assumes that the image is in grayscale format and that the pixel values are unsigned 8-bit integers (‘uchar’). If the image is in a different format or if the pixel values are of a different type, you will need to adjust the code accordingly.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads