C++ – Finding Maximum Value in Image using OpenCV
Last Updated :
26 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[])
{
cv::Mat image
= cv::imread( "image.jpg" , cv::IMREAD_GRAYSCALE);
int maxValue = std::numeric_limits<uchar>::min();
for ( int y = 0; y < image.rows; y++) {
for ( int x = 0; x < image.cols; x++) {
int pixelValue = image.at<uchar>(y, x);
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 signed char in C++
In this article, we will discuss the signed char data type in C++. Some properties of the signed char data type are: It is generally used to store 8-bit characters.Being a signed data type, it can store positive values as well as negative values.Size of 8 bits is occupied where 1 bit is used to stor
3 min read
Maximum value of int in C++
In this article, we will discuss the int data type in C++. It is used to store a 32-bit integer. Some properties of the int data type are: Being a signed data type, it can store positive values as well as negative values.Takes a size of 32 bits where 1 bit is used to store the sign of the integer.A
3 min read
Maximum value of unsigned short int in C++
In this article, we will discuss the unsigned short int data type in C++. It is the smallest (16 bit) integer data type in C++. Some properties of the unsigned short int data type are: Being an unsigned data type, it can store only positive values.Takes a size of 16 bits.A maximum integer value that
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
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
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++ Code // C++ program to demonstrate the // above appro
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