如何在 Python 和 OpenCV 中使用“xphoto_WhiteBalancer.balanceWhite”?

Del*_*gan 2 python opencv image-processing python-3.x opencv3.0

我正在寻找一种将自动白平衡应用于图像的简单方法。

我找到了一些关于balanceWhite()方法的官方文档:cv::xphoto::WhiteBalancer Class Reference

但是,当我尝试调用该函数时,出现一个模糊错误,如示例所示。

image = cv2.xphoto_WhiteBalancer.balanceWhite(image)
Run Code Online (Sandbox Code Playgroud)

加薪:

Traceback (most recent call last):
  File "C:\Users\Delgan\main.py", line 80, in load
    image = cv2.xphoto_WhiteBalancer.balanceWhite(image)
TypeError: descriptor 'balanceWhite' requires a 'cv2.xphoto_WhiteBalancer' object but received a 'numpy.ndarray'
Run Code Online (Sandbox Code Playgroud)

cv2.xphoto_WhiteBalancer如果然后我尝试根据需要使用对象:

balancer = cv2.xphoto_WhiteBalancer()
cv2.xphoto_WhiteBalancer.balanceWhite(balancer, image)
Run Code Online (Sandbox Code Playgroud)

它提出:

Traceback (most recent call last):
  File "C:\Users\Delgan\main.py", line 81, in load
    cv2.xphoto_WhiteBalancer.balanceWhite(balancer, image)
TypeError: Incorrect type of self (must be 'xphoto_WhiteBalancer' or its derivative)
Run Code Online (Sandbox Code Playgroud)

有人成功地在 Python 3.6 和 OpenCV 3.4 中使用此功能吗?

我也尝试过派生类GrayworldWBLearningBasedWBSimpleWB错误是相同的。

Del*_*gan 5

答案可以在xphoto文档中找到。

创建 WB 算法的适当方法是createSimpleWB()createLearningBasedWB()createGrayworldWB()

例子:

wb = cv2.xphoto.createGrayworldWB()
wb.setSaturationThreshold(0.99)
image = wb.balanceWhite(image)
Run Code Online (Sandbox Code Playgroud)

以下是官方 OpenCV 存储库中的示例文件:modules/xphoto/samples/color_balance_benchmark.py