Delgan
Delgan

Reputation: 19677

How to use "xphoto_WhiteBalancer.balanceWhite" with Python and OpenCV?

I am looking for a straightforward way of applying automatic white balance to an image.

I found some official documentation about a balanceWhite() method: cv::xphoto::WhiteBalancer Class Reference

However, I have an obscure error when I try to call the function as shown in the example.

image = cv2.xphoto_WhiteBalancer.balanceWhite(image)

Raises:

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'

If then I try to use a cv2.xphoto_WhiteBalancer object as required:

balancer = cv2.xphoto_WhiteBalancer()
cv2.xphoto_WhiteBalancer.balanceWhite(balancer, image)

It raises:

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)

Did anyone succeeded to use this feature with Python 3.6 and OpenCV 3.4?

I also tried with derived classes GrayworldWB, LearningBasedWB and SimpleWB but errors are the same.

Upvotes: 2

Views: 6206

Answers (1)

Delgan
Delgan

Reputation: 19677

The answer can be found in the xphoto documentation.

The appropriate methods to create the WB algorithms are createSimpleWB(), createLearningBasedWB() and createGrayworldWB().

Example:

wb = cv2.xphoto.createGrayworldWB()
wb.setSaturationThreshold(0.99)
image = wb.balanceWhite(image)

Here is a sample file in the official OpenCV repository: modules/xphoto/samples/color_balance_benchmark.py

Upvotes: 5

Related Questions