3

I tried to convert color correction module example in https://docs.opencv.org/master/d1/dc1/tutorial_ccm_color_correction_model.html but I faced some difficulty because I do not know c++,

    //compte color correction matrix
    ColorCorrectionModel model1(src, COLORCHECKER_Vinyl);
    model1.run();
    Mat ccm = model1.getCCM();
    std::cout<<"ccm "<<ccm<<std::endl;
    double loss = model1.getLoss();
    std::cout<<"loss "<<loss<<std::endl;

I wrote in python as

   colorCorrectionModel = cv2.ccm_ColorCorrectionModel(src, cv2.ccm.COLORCHECKER_Macbeth)

   colorCorrectionModel.run()

   ccmat = colorCorrectionModel.getCCM()
   print(ccmat)
   weigths = colorCorrectionModel.getWeights()
   print(weigths)

but give me this error

colorCorrectionModel.run()

cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-sljz46fi\opencv\modules\core\src\arithm.cpp:234: 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'

How I could correct this error

2
  • More people have this problem: github.com/opencv/opencv_contrib/issues/2901 No solution has been reported yet. Commented Mar 30, 2021 at 5:37
  • the C++ code (tutorial code on docs.opencv.org) has a column matrix of 3-channel data (Vec3d) because the API expects that kind of data. this shape maps to a numpy shape of (-1, 1, 3) (auto rows, 1 column, 3 channels) due to the cv::Mat/np.array conversion logic of OpenCV. Commented May 28, 2021 at 19:47

1 Answer 1

6

Example:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import cv2
import numpy as np

image = cv2.imread('input.jpg')
detector = cv2.mcc.CCheckerDetector_create()
detector.process(image, cv2.mcc.MCC24, 1)

checkers = detector.getListColorChecker()
for checker in checkers:
    cdraw = cv2.mcc.CCheckerDraw_create(checker)
    img_draw = image.copy()
    cdraw.draw(img_draw)
    
    chartsRGB = checker.getChartsRGB()
    width, height = chartsRGB.shape[:2]
    roi = chartsRGB[0:width,1]
    print (roi)
    rows = int(roi.shape[:1][0])
    src = chartsRGB[:,1].copy().reshape(int(rows/3), 1, 3)
    src /= 255
    #print(src.shape)

    model = cv2.ccm_ColorCorrectionModel(src, cv2.ccm.COLORCHECKER_Macbeth)
    model.setColorSpace(cv2.ccm.COLOR_SPACE_sRGB)
    model.setCCM_TYPE(cv2.ccm.CCM_3x3)
    model.setDistance(cv2.ccm.DISTANCE_CIE2000)
    model.setLinear(cv2.ccm.LINEARIZATION_GAMMA)
    model.setLinearGamma(2.2)
    model.setLinearDegree(3)
    model.setSaturatedThreshold(0, 0.98)
    model.run()

    ccm = model.getCCM()
    print ('ccm:\n{}\n'.format(ccm))
    loss = model.getLoss()
    print ('loss:\n{}\n'.format(loss))

    img_ = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    img_ = img_.astype(np.float64)
    img_ = img_/255
    calibratedImage = model.infer(img_)
    out_ = calibratedImage * 255
    out_[out_ < 0] = 0
    out_[out_ > 255] = 255
    out_ = out_.astype(np.uint8)
     
    out_img = cv2.cvtColor(out_, cv2.COLOR_RGB2BGR)
    cv2.imwrite('output.jpg', out_img);

    width, height = image.shape[:2]
    image = cv2.resize(image, (int(height/4),int(width/4)), interpolation=cv2.INTER_CUBIC)
    img_draw = cv2.resize(img_draw, (int(height/4),int(width/4)), interpolation=cv2.INTER_CUBIC)
    out_img = cv2.resize(out_img, (int(height/4),int(width/4)), interpolation=cv2.INTER_CUBIC)
    cv2.namedWindow('Image')
    cv2.imshow("Image",image)
    cv2.imshow('img_draw', img_draw)
    cv2.imshow('Out Image', out_img)
    cv2.waitKey(0)

cv2.destroyAllWindows()
Sign up to request clarification or add additional context in comments.

1 Comment

Please provide further context like: How does this code work? How would another user adapt this code to their needs? Which lines are critical for changing the correction algorithm?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.