Search code examples
pythonopencvgithubcolors

OpenCV(python): how to use ccm.ColorCorrectionModel.setWeightsList(p)?


I am struggling to use the method ccm.ColorCorrectionModel.setWeightsList(p) correctly. I can not get the right format of the parameter p. First the ccm.ColorCorrectionModel is filled with source and reference values of the format/shape (24,1,3) -all double. So I tried a numpyarray for parameter p also of format (24,1,3) -all double for the weightsList. But the runtime breaks (see below). Any idea whats wrong or is there a small python app around that uses the method .setWeightsList() successfully, where I can see how the source and weight values have to be linked?

Code:

src = np.array([...]) # array of shape (24,1,3) values 0-255
ref = np.array([...]) # array of shape (24,1,3) values 0-255
model = cv2.ccm.ColorCorrectionModel(src/255, ref/255, cv2.ccm.COLOR_SPACE_sRGB);

w = np.array([...]) # array of shape (24,1,3) all values 1.0
model.setWeightsList(w)
model.run() => error

Error: "cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:214: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array' in function 'cv::binary_op' "


Solution

  • I found a C++ example in OpenCV source code file test_ccm.cpp.

    In the example, the size of the weights_list mat is (24, 1).
    Relevant part from test_ccm.cpp file:

    Mat weights_list_ = (Mat_<double>(24, 1) <<
                            1.1, 0, 0, 1.2, 0, 0,
                            1.3, 0, 0, 1.4, 0, 0,
                            0.5, 0, 0, 0.6, 0, 0,
                            0.7, 0, 0, 0.8, 0, 0);
    

    Modify the size of w to be (24, 1).

    Example:

    w = np.array(np.ones((24, 1))) # array of shape (24,1) all values 1.0
    model.setWeightsList(w)
    model.run() # Works
    

    It looks like each weight refers an RGB triplet (each weight refers [red, green, blue]).

    Note: I can't really tell what is the purpose of the Weights List (it seems to be undocumented), and following the source code is difficult.