首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将色彩校正opencv模块示例从C++转换为python

将色彩校正opencv模块示例从C++转换为python
EN

Stack Overflow用户
提问于 2021-02-21 21:25:12
回答 1查看 460关注 0票数 2

我试着在https://docs.opencv.org/master/d1/dc1/tutorial_ccm_color_correction_model.html中转换色彩校正模块的例子,但我遇到了一些困难,因为我不知道c++,

代码语言:javascript
复制
    //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写成

代码语言:javascript
复制
   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: C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-sljz46fi\opencv\modules\core\src\arithm.cpp:234:(4.5.1)数组错误:(-209:输入参数的大小不匹配)该操作既不是‘OpenCV op array’(其中数组具有相同的大小和类型),也不是'array op scalar',也不是函数'cv::binary_op‘中的'scalar op array’

我如何纠正这个错误

EN

回答 1

Stack Overflow用户

发布于 2021-05-09 12:57:32

示例:

代码语言:javascript
复制
#!/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()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66302777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档