Converting Color Correction opencv module example from C++ to python

Viewed 1066

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

1 Answers

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()
Related