Average values of a single channel

12,752

Solution 1

Image.channels() will give you the number of channels in any image. Please refer to the OpenCV documentation.

multiple channels can be accessed as follows:

        img.at<Vec3b>(i,j)[0] //Y
        img.at<Vec3b>(i,j)[1] //U
        img.at<Vec3b>(i,j)[2] //V

Solution 2

You can do this:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>

using namespace cv;

int main(void)
{
    vector<Mat> channels;
    Mat img = imread("Documents/forest.jpg");
    split(img, channels);
    Scalar m = mean(channels[0]);
    printf("%f\n", m[0]);

    return 0;
}
Share:
12,752
Lakshmi Narayanan
Author by

Lakshmi Narayanan

Roboticist, Interested in android. New to Android

Updated on June 04, 2022

Comments

  • Lakshmi Narayanan
    Lakshmi Narayanan almost 2 years

    I converted my image from rgb to YUV. Now I want to find the average values of luma channel alone. Could you please help me by telling how I can achieve this? Moreover, is there a way to determine how many channels an image consists of?