You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenCV Python颜色校正遇属性错误:getChartsRGB返回numpy.ndarray而非cv2.Map

解决OpenCV Python颜色校正中getChartsRGB()的属性错误

问题核心在于OpenCV的Python绑定与C++ API的差异:C中getChartsRGB()返回cv::Mat,但Python里会自动转换为numpy.ndarray,因此不存在rows这类C Mat专属属性,必须用numpy数组的维度方法替代。

错误代码修正

将出错的代码行:

src = chartsRGB[:,1].copy().reshape(3,chartsRGB.rows/3)

替换为:

# 用numpy shape获取行数,整数除法//保证维度为整数
src = chartsRGB[:, 1].copy().reshape(3, chartsRGB.shape[0] // 3)

关键说明

  • Python版OpenCV会把所有C++的cv::Mat转为numpy数组,对应关系:
    • chartsRGB.shape[0] 等价于C++的chartsRGB.rows
    • chartsRGB.shape[1] 等价于C++的chartsRGB.cols
  • 用//而非/是因为Python3中/返回浮点数,而reshape要求维度为整数

完整修正代码

import cv2

chartype = cv2.mcc.MCC24
image = cv2.imread("image.jpg", cv2.IMREAD_COLOR)

if image is None:
    print("Invalid Image ! \n")

imageCopy = image.copy()
detector = cv2.mcc.CCheckerDetector.create()
# Marker type to detect
if not detector.process(image, chartype, 1):
    print("ChartColor not detected \n")

checkers = detector.getListColorChecker()
for checker in checkers:
    cdraw = cv2.mcc.CCheckerDraw.create(checker)
    cdraw.draw(image)
    chartsRGB = checker.getChartsRGB()
    # 修正后的维度处理
    src = chartsRGB[:, 1].copy().reshape(3, chartsRGB.shape[0] // 3)

内容的提问来源于stack exchange,提问作者Rorschy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 18:48:24