3
votes

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?

2
get the Length of Val in a Scalar? stackoverflow.com/a/67907842/4721626 - jrosq

2 Answers

16
votes

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;
}
3
votes

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