Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the type of data in a cv::Mat converted to grayscale

Tags:

c++

opencv

I'm not sure where to find this information.

I loaded in a .jpg and converted it to grayscale with cv::cvtColor(*input_image_grayscale, *input_image_grayscale, CV_BGR2GRAY);

I then try to reference a pixel with input_image_grayscale->at<float>(row, col) but get an assertion error. How do I determine the right type of data (it's clearly not float) to dereference this? Thanks

For reference, I ran input_image_grayscale->type() and got 0.

like image 957
zebra Avatar asked Oct 08 '22 00:10

zebra


1 Answers

The value returned by type is just an integer that OpenCV declares with a preprocessor define. You can check it in a switch statement like this:

switch( matrixType )
{
    case CV_8UC1:

    .... check for other types
}

The 8U, in that example refers to an unsigned char, and C1 refers to a single channel image. CV_8UC1 is defined as 0, so that is your Mat's type and you should use unsigned char for your reference type.

You can also use the function Mat::depth, to return the type of the size of a single matrix element, because you already know it is a single channel image since it is grayscale.

like image 52
kappamaki Avatar answered Oct 12 '22 22:10

kappamaki