Article directory
-
- summary
- Load sample image
- Statistical data analysis
- White Patch Algorithm
- summary
Summary
White balance technology plays a vital role in photography and image processing. Under different lighting conditions, the camera may not accurately capture the true color of an object, resulting in images that appear dull, unnatural in tone, or washed out. In order to solve this problem, we need to understand and apply white balance technology.
The importance of white balance
In daily life, we often encounter photos taken under different light sources, such as using incandescent lights, fluorescent lights indoors, or taking photos outdoors in the sun. Different types of light sources produce light with different color temperatures, and the camera may not automatically adapt to these light differences. This causes the colors in the photos to look unrealistic and inconsistent with our visual perception.
The principle of white balance
The basic principle of white balance technology is to make the grayscale areas in the image appear neutral gray by adjusting the gain of each color channel in the image. Simply put, it makes white look like white and black looks like black. In this way, images taken under different light sources can more accurately restore the true color of the object.
How to adjust white balance
Preset white balance modes: Cameras usually provide some preset white balance modes, such as daylight, cloudy, fluorescent, incandescent, etc. Choosing a suitable preset mode can improve the color deviation of the image to a certain extent. Manual white balance: In some cameras, we can set the white balance manually. This usually requires placing a white card in the shooting scene and letting the camera adjust the white balance through this reference object. Post-processing: In image processing software, we can also adjust the white balance. By adjusting parameters such as color temperature, hue, and saturation of the image, we can more finely control the color effect of the image.
Application of white balance technology
White balance technology is not only widely used in photography, but also plays an important role in image processing, advertising design, artistic creation and other fields. In various scenarios such as product photography, portrait photography, landscape photography, etc., appropriate white balance adjustment can improve the quality of photos and make them more attractive and realistic.
White balance is a technique used to correct color deviations in images caused by different lighting conditions. It adjusts the color contrast of an image to make white look like white and black look like black. This process is important because it ensures that the colors in the image are accurate while also making the image look more natural to the human eye.
Load sample image
# Import necessary Python libraries import numpy as np import pandas as pd import matplotlib.pyplot as plt from skimage import io, img_as_ubyte from skimage.io import imread, imshow from matplotlib.patches import Rectangle #Load sample image from skimage import io import matplotlib.pyplot as plt #Read image file image = io.imread(r'E:\yolo project\Opencv-project-main\Opencv-project-main\CVZone\img.png') # show original image plt.figure(figsize=(10,10)) plt.title('Original Image') # Set image title plt.imshow(image) # Display image plt.show() # Display image
result:
Statistical data analysis
# Import necessary Python libraries import numpy as np import pandas as pd import matplotlib.pyplot as plt from skimage import io, img_as_ubyte from skimage.io import imread, imshow from matplotlib.patches import Rectangle #Load sample image from skimage import io import matplotlib.pyplot as plt #Read image file image = io.imread('qmark.png') # show original image plt.figure(figsize=(10,10)) plt.title('Original Image') # Set image title plt.imshow(image) # Display image plt.show() # Display image # Analyze statistical information in the image def calc_color_overcast(image): # Calculate the color deviation of each channel red_channel = image[:, :, 0] # red channel green_channel = image[:, :, 1] # Green channel blue_channel = image[:, :, 2] # blue channel #Create a DataFrame to store the results channel_stats = pd.DataFrame(columns=['Mean', 'Std', 'Min', 'Median', 'P_80', 'P_90', 'P_99' , 'Max']) # Calculate and store statistics for each color channel for channel, name in zip([red_channel, green_channel, blue_channel], ['Red', 'Green', 'Blue']): mean = np.mean(channel) # average std = np.std(channel) # standard deviation minimum = np.min(channel) # minimum value median = np.median(channel) # Median p_80 = np.percentile(channel, 80) # 80th percentile p_90 = np.percentile(channel, 90) # 90th percentile p_99 = np.percentile(channel, 99) # 99th percentile maximum = np.max(channel) # Maximum value # Store statistical information into DataFrame channel_stats.loc[name] = [mean, std, minimum, median, p_80, p_90, p_99, maximum] return channel_stats # Calculate statistics of color channels channel_stats = calc_color_overcast(image) #Print statistics print(channel_stats)
A function calc_color_overcast(image) is defined, which is used to calculate the statistical information of each color channel (red, green, blue) in the image, including mean, standard deviation, minimum value, median, 80th, 90th, 99th percentiles and maximum values. This information is useful for analyzing the color properties of an image.
result:
White Patch Algorithm
The white patch algorithm is a color balance method commonly used in image processing. The goal is to scale the color channels of the image so that the brightest pixels in each channel become white. This method is based on the assumption that the brightest pixels in the image should represent white. By adjusting the brightness of each channel, the algorithm corrects the color projection of the image and achieves the white balance of the image.
# Import necessary Python libraries import numpy as np import pandas as pd import matplotlib.pyplot as plt from skimage import io, img_as_ubyte from skimage.io import imread, imshow from matplotlib.patches import Rectangle #Load sample image from skimage import io import matplotlib.pyplot as plt #Read image file def white_patch(image, percentile=100): """ Returns a plot comparison of original and corrected/white balanced image using the White Patch algorithm. Parameters ---------- image : numpy array Image to process using white patch algorithm percentile: integer, optional Percentile value to consider as channel maximum """ white_patch_image = img_as_ubyte( (image * 1.0 / np.percentile(image, percentile, axis=(0, 1))).clip(0, 1)) # Plot the comparison between the original and white patch corrected images fig, ax = plt.subplots(1, 2, figsize=(10, 10)) ax[0].imshow(image) ax[0].set_title('Original Image') ax[0].axis('off') ax[1].imshow(white_patch_image, cmap='gray') ax[1].set_title('White Patch Corrected Image') ax[1].axis('off') plt.show() # Read the input image image = imread(r'E:\yolo project\Opencv-project-main\Opencv-project-main\CVZone\img.png') # Call the function to implement white patch algorithm white_patch(image, 100)
Using the default parameter percentile=100 does not significantly improve the image because the maximum value of the RGB channels in the image is already [255, 255, 255]. By observing the statistics from the previous section, we can see that the maximum value and 99th percentile of the RGB channels are both 255.
To solve this problem, we can consider considering the lower percentile of the pixel value as the maximum value, rather than the absolute maximum value.
white_patch(image, 85)
result:
Summary
advantage:
Simple and easy to use: The implementation of the white patch algorithm is relatively simple and easy to understand and operate. This makes it a convenient option for fixing image white balance issues, especially for scenes that don’t require complex manipulation. Effective for specific scenarios: This algorithm is very effective when processing images with predominantly white areas or neutral gray areas. Especially when there are obvious bright areas in the image, the white patch algorithm can significantly improve the color balance problem of the image, making the image clearer and more natural. Wide applicability: The white patch algorithm can be widely used in various scenarios, including photography, image processing and other fields. It is not only suitable for professional photographers, but can also be used by ordinary users for simple image repair work.
shortcoming:
Assumption limitations: The core assumption of the algorithm is that the brightest color in the image is white. However, in actual scenes, the brightest color in the image may be other colors. When this assumption does not hold, the effectiveness of the white patch algorithm may be limited and it cannot completely fix the white balance problem of the image. Risk of over-correction: If the assumptions of the algorithm do not hold true, it may lead to over-correction, causing unnatural colors or artifacts in the image. Over-correction can introduce new problems that affect the quality and authenticity of the image. Color shifts and artifacts: Due to the underlying assumption of the algorithm, that the brightest areas in the image are white, this can cause color shifts or artifacts in certain areas of the image. This phenomenon may be more obvious at the edges or highlight areas of the image, affecting the overall visual effect. In some special scenarios, this color shift and artifacts may have a negative impact on the authenticity of the image.
When using the white patch algorithm, users need to weigh its advantages and disadvantages based on the specific situation and ensure that the appropriate scene and image are selected to obtain the best repair results.