Skip to content

Add support to identify what colorspace a cvMat is in #18792

@jgbradley1

Description

@jgbradley1

This is a feature request for opencv core.

Using opencv C++, is there a way to automatically detect what colorspace a cvMat is in? In my experience with opencv, I believe the answer is no. The responsibility of knowing what colorspace a cvMat is in has always been application-specific and relied on a dev to know how data was read in via imread.

This becomes problematic when interfacing with other libraries that use opencv and pass cvMat's around where it's not always clear how the cvMat was constructed. Either the colorspace must be documented or requires digging into the source code.

An easy fix to this would be to add an enum (maybe call it COLORSPACE) that tracks what colorspace the data is in. Functions like imread and cvtColor would set/update the enum where appropriate. Here is a short example of how I imagine someone would use the enum solution.

#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char **argv) {
    string image_file("some-image.jpg");
    Mat img = imread(image_file); // or possibly retrieve a cvMat from an external library
    if (img.empty()) {
        cout << "Can't read image from file: " <<  image_file << endl;
        return -1;
    }

    // what colorspace are we in? Does the Mat need converting with cv::cvtColor?
    if (img.COLORSPACE == cv::RGB) {
        cout << "this image is in RGB space" << endl
    }
    if (img.COLORSPACE == cv::BGR) {
        cout << "this image is in BGR space" << endl;
    }
}

Thoughts or suggestions?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions