python - 将颜色校正 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'二进制操作'
我该如何纠正这个错误
解决方案
例子:
#!/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()
推荐阅读
- html - 如何在不影响 HTML 和 CSS 中的背景图像的情况下调整图像
- java - 在JavaFX中按下按钮以重复(无限时间)启动方法的最简单方法是什么?
- powershell - 运行 powershell 脚本,以便检查并邮寄故障
- python - 在 pycharm 中获取权限被拒绝错误(错误号 13)
- c# - C# 登录脚本:RDP 远程应用程序获取应用程序名称
- c++ - 如何处理参数名称阴影方法?
- angular - 输入更改时指令不会触发
- html - 如何访问 API 调用中的所有数据
- java - 如何只拥有一个可用作 Webdriver 或 Appiumdriver 的驱动程序
- python-3.x - Django:如何在基于类的视图中获取 id?