首页 > 解决方案 > 将颜色校正 opencv 模块示例从 C++ 转换为 python

问题描述

我尝试在https://docs.opencv.org/master/d1/dc1/tutorial_ccm_color_correction_model.html中转换颜色校正模块示例, 但我遇到了一些困难,因为我不懂 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;

我用python写成

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

   colorCorrectionModel.run()

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

但是给我这个错误

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: 错误:( -209:输入参数的大小不匹配)该操作既不是'array op array'(其中数组具有相同的大小和类型),也不是'array op scalar',也不是函数'cv::中的'scalar op array'二进制操作'

我该如何纠正这个错误

标签: pythonc++opencvcolors

解决方案


例子:

#!/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()

推荐阅读