Python error: (-215:Assertion failed) images.size() == times.total() in function ‘cv::CalibrateDebe

文章讲述了在使用OpenCV进行多曝光图像处理时遇到的错误,具体是由于在调用cameraresponsefunction的算法时,图像数组和曝光时间列表长度不匹配。作者通过检查代码,发现了问题在于将多曝光图像合并成一张图像后,与曝光时间数组不对应。修正方法是保持原始图像列表和曝光时间列表,然后正确地传给process方法,从而成功计算出相机响应函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

运行源代码,

import numpy as np
import cv2
import matplotlib.pyplot as plt

# 读取多曝光图像
img1 = cv2.imread('6-250.bmp')
img2 = cv2.imread('6-500.bmp')
img3 = cv2.imread('6-800.bmp')

# 将多曝光图像转换为灰度图像
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
gray3 = cv2.cvtColor(img3, cv2.COLOR_BGR2GRAY)

# 提取每个像素的曝光时间
exp_times = np.array([0.1, 0.55, 2.5], dtype=np.float32)

# 将图像像素值转换为对数域
log_exposure_times = np.log(exp_times)

# 将多曝光图像合并为一张图像
stacked = np.stack([gray1, gray2, gray3], axis=2)

# 估计相机响应函数
calibrateDebevec = cv2.createCalibrateDebevec()
responseDebevec = calibrateDebevec.process(stacked, times=exp_times)

# 将相机响应函数曲线写入文件
np.savetxt('response_curve.txt', responseDebevec)

出现错误:

responseDebevec = calibrateDebevec.process(stacked, times=exp_times)
cv2.error: OpenCV(3.4.10) C:\projects\opencv-python\opencv\modules\photo\src\calibrate.cpp:72: error: (-215:Assertion failed) images.size() == times.total() in function 'cv::CalibrateDebevecImpl::process'

    这个错误是由于 cv2.createCalibrateDebevec() 方法返回一个用于执行Debevec曲线拟合的对象,而 process() 方法用于计算相机响应函数。在调用 process() 方法时,需要传递一个与图像数量相同的时间列表作为参数 times,用于指定每张图像的曝光时间。

    添加代码去检测错误:

#检测图像列表和曝光时间列表都具有相同长度
if len(stacked) != len(exp_times):
    raise ValueError("The length of stacked and exp_times must be the same.")

#检测输入图像时没有重复
unique_images = set(stacked)
if len(unique_images) < len(stacked):
    raise ValueError("The images in stacked must be unique.")

#检测输入的exp_times中的时间值是单调递增的(即按时间排序)
if not all(x < y for x, y in zip(exp_times, exp_times[1:])):
    raise ValueError("The values in exp_times must be in increasing order.")

运行后显示出错误地方:

Traceback (most recent call last):
  File "D:\HApp\Deep\Pycharm-Community\PycharmProjects\pythonProject17\RANDOM_test1111\hhhhdr.py", line 132, in <module>
    raise ValueError("The length of stacked and exp_times must be the same.")
ValueError: The length of stacked and exp_times must be the same.

发现错误,是我将多曝光图像合并为一张图像stacked,再进行stacked和 times拟合相机响应曲线,两个数量不对等,所以一直显示error。

改为如下代码,就可以成功运行了

# stacked = np.stack([gray1, gray2, gray3], axis=2)
img_list =[gray1, gray2, gray3]

# 估计相机响应函数
calibrateDebevec = cv2.createCalibrateDebevec()
responseDebevec = calibrateDebevec.process(img_list, times=exp_times)

这是一个非常简单的错误,竟然没有检查出来……记录下来以进行改正。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值