C++ - Finding Maximum Value in Image using OpenCV
Last Updated :
28 Apr, 2025
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:
- Load the image into memory using an image processing library such as OpenCV or CImg.
- 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.
- 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.
Similar Reads
Maximum value of unsigned int in C++
In this article, we will discuss the maximum value of unsigned int in C++. Unsigned int data type in C++ is used to store 32-bit integers.The keyword unsigned is a data type specifier, which only represents non-negative integers i.e. positive numbers and zero. Some properties of the unsigned int dat
2 min read
Maximum value of unsigned long long int in C++
In this article, we will discuss the unsigned long long int data type in C++. It is the largest (64 bit) integer data type in C++. Some properties of the unsigned long long int data type are: An unsigned data type stores only positive values.It takes a size of 64 bits.A maximum integer value that ca
2 min read
Maximum value of unsigned char in C++
In this article, we will discuss the maximum value of unsigned char data type in C++. Some properties of the unsigned char data type are: Being an unsigned data type, it can store only positive values.Unsigned char data type in C++ is used to store 8-bit characters.A maximum value that can be stored
2 min read
Maximum value of short int in C++
In this article, we will discuss the short int data type in C++. This data type in C++ is used to store 16-bit integers. Some properties of the short int data type are: Being a signed data type, it can store positive values as well as negative values.Takes a size of 16 bits, where 1 bit is used to s
3 min read
Write on an image using openCV in C++
In this article, we will discuss how to write over an image using OpenCV C++. Function putText() from OpenCV C++ library will be used to write text on an image. Program 1: Below program shows how to write text over a blank background image: C++ // C++ program to demonstrate the // above approach #in
2 min read
Maximum value of long long int in C++
In this article, we will discuss the long long int data type in C++. long long int data type in C++ is used to store 64-bit integers. It is one of the largest data types to store integer values, unlike unsigned long long int both positive and negative. Some properties of the long long int data type
2 min read
Reading and Displaying an image in OpenCV using C++
In this article, we will discuss to open an image using OpenCV (Open Source Computer Vision) in C++. Unlike python, any additional libraries in C++ are not required. OpenCV C++ comes with this amazing image container Mat that handles everything for us. The only change seen from a standard C++ progra
3 min read
Draw a line using OpenCV in C++
In this article, we will discuss how to draw a line using OpenCV in C++. The idea is to use the line() function from OpenCV C++ library. Syntax: line(img, pt1, pt2, color, thickness, lineType, shift) Parameters: img: This is the image file.start: Start point of the line segment. The first point out
3 min read
How to Find the Maximum Element of a Vector using STL in C++?
Given a vector, find the maximum element of the vector using STL in C++. ExampleInput: v = {2, 4, 1, 5, 3}Output: 5Explanation: 5 is the largest element of vector.Input: v = {11, 23, 3, 5, 24}Output: 24Explanation: 24 is the largest element of the given range.STL provides the following different met
3 min read
Maximum OR value of a pair in an Array | Set 2
Given an array arr[] of N positive elements, the task is to find the maximum bitwise OR value of a pair from the given array.Examples: Input: arr[] = {3, 6, 8, 16} Output: 24 Explanation: The pair giving maximum OR value is (8, 16) 8|16 = 24Input: arr[] = {8, 7, 3, 12} Output: 15 Explanation: There
5 min read