Flat Field image processing

Introduction

When automatically counting particles from a micrograph, it is difficult to set the threshold value if the brightness of the original image varies. Therefore, it is necessary to reduce the variation in the brightness of the background in advance. Flat Field correction is used for this process. As a reminder, I will describe the background image processing using Pyhton and OpenCV.

Flat field correction

Astrophotography (correction at the time of shooting)

Flat Field correction is used when shooting celestial bodies. Reference 1 Starry Urban Sky Reference 2 What is flat correction processing? Reference 3 Flat frame shooting method

As you can see Light frame (LF): The image as it was taken without any processing Dark frame (DF): A shot image in which the camera is capped to prevent outside light from entering under the same shooting conditions as the light frame. Flat frame (FF): An image in which nothing is captured and only the light distribution is recorded under the same shooting conditions as a light frame. The corrected image is processed from these images as follows. Corrected image: (LF-DF)/(FF-DF)

By the way, the same processing is performed for reflectance measurement in spectroscopic measurement. Reflectance = (reflection intensity of measurement sample-dark frame)/(reflection intensity of reference (for example, Al) -dark frame)

Processing with images already obtained (after shooting)

Reference 4 Background removal In most cases, you will not be able to do the process described above because you already have an image and are asked to automatically count the number of particles from this image. You need to perform background processing from the existing image. Therefore, we will create a flat frame from the original image. For flat frames, the original image (light frame) is Mean-filtered and the average brightness of the original image is applied. (The Mean filter makes the pixel range a little larger so that there are no fine structures.)

Corrected image: Light frame (original image)/Flat frame (image after Mean filter x average brightness)

Environment I tried

Windows 10 Pro anaconda Python = 3.7 conda = 4.9 numpy = 1.18.5 opencv = 4.2.0 (installed from conda-forge)

Actually

I tried to implement Flat Field in Python using the image data posted in Reference 4.

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

#Plot of comparison before and after processing
def bef_aft_img_show(img1,img2):
    print(f'average brightness:{img1.mean():.1f}, {img2.mean():.1f} ')
    plt.figure(figsize=(10,10))
    plt.subplot(121),plt.imshow(img1),plt.title('Original')
    plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(img2),plt.title('After')
    plt.xticks([]), plt.yticks([])
    plt.show()

#Image and Histgram Plot
def image_hist_show(img):
    print(f'Shape:{img.shape},type:{img.dtype}')
    print(f'Average Brightness:{img.mean():.1f}')
    hist = cv2.calcHist([img],[0],None,[256],[0,256])

    plt.figure(figsize=(10,5))
    plt.subplot(121),plt.imshow(img),plt.title('Image')
    plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.plot(hist),plt.title('Histgram')
    plt.show()


#Image loading(I am using OpenCV)
file_path = './data/samp_Gradation1.bmp' #Please change accordingly.
img = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
image_hist_show(img)

Shape:(256, 256),type:uint8
Average Brightness:111.5

output_2_1.png

#Creating a Flat Frame
#Perform Mean filtering.
#Reference: Image smoothing
# http://whitewell.sakura.ne.jp/OpenCV/py_tutorials/py_imgproc/py_filtering/py_filtering.html

dst = cv2.blur(img, (50, 50)) 
#As a normal argument(3,3)Or (5,Enter 5), but set it to a large value to remove the overall light and darkness.
#Please adjust the value appropriately.

image_hist_show(dst)
Shape:(256, 256),type:uint8
Average Brightness:111.7

output_3_1.png

#The original image/Image after Mean filter × Average brightness
avg_hist = img.mean()
ffc = (img/dst)*avg_hist

print(ffc.dtype)
# >>> float64
#Since it is float64, it is set to unit8 to use OpenCV functions after this.
cast_ffc = ffc.astype('uint8')
image_hist_show(cast_ffc)

#If you want to save this image
# cv2.imwrite('case1_ffc.png', ffc)
float64
Shape:(256, 256),type:uint8
Average Brightness:110.3

output_4_1.png

#Comparison before and after processing
bef_aft_img_show(img,cast_ffc)
average brightness:111.5, 110.3 

output_5_1.png

#It was in Reference Material 4,
#Original image-Image after Mean filter + Average brightness
#I think that it is okay if the background processing can be done to some extent by the processing of.

l_ffc = img - dst + avg_hist
cast_l_ffc = l_ffc.astype('uint8')
image_hist_show(cast_l_ffc)
Shape:(256, 256),type:uint8
Average Brightness:110.8

output_6_1.png

There is a rolling ball algorithm implemented in ImgeJ For this, there is a Python package at the following site.

https://github.com/mbalatsko/opencv-rolling-ball

It can only be installed with pip. In some cases, opencv-python will also be installed, so if you want to try this package, you should create a new virtual environment and try it. I actually used it, but it takes some time to calculate.

from cv2_rolling_ball import subtract_background_rolling_ball

img_rg, background = subtract_background_rolling_ball(img, 30, light_background=True,
                                     use_paraboloid=False, do_presmooth=True)

image_hist_show(img_rg)   
Shape:(256, 256),type:uint8
Average Brightness:246.0

output_8_1.png

Summary

A flat image was created by averaging from an existing image, and Flat Field processing was performed.

Recommended Posts

Flat Field image processing
[Image processing] Posterization
python image processing
Image processing 100 knocks ①
Image processing with MyHDL
First Python image processing
Read digital image processing
Image processing with Python
Image Processing with PIL
Image processing with Python (Part 2)
opencv-python Introduction to image processing
Image processing with PIL (Pillow)
Digital image processing (spatial filtering)
100 image processing knocks !! (011 --020) Early game
100 image processing knocks !! (001 --010) Carefully and carefully
Image processing with Python (Part 1)
Image processing with Python (Part 3)
Image processing by python (Pillow)
Image Processing Collection in Python
Image expansion and contraction processing
[Python] Image processing with scikit-image
Personal notes for python image processing
Image processing with Python 100 knocks # 3 Binarization
Image processing 100 knocks Q9, Q10 (filter) speedup
Environmentally friendly scraping using image processing
Image processing with Python 100 knocks # 2 Grayscale
Image processing | predicting species from images
Basics of binarized image processing with Python
Image processing with Python 100 knock # 10 median filter
Image processing by Python 100 knock # 1 channel replacement
Create an image processing viewer with PySimpleGUI
Image processing with Python 100 knocks # 8 Max pooling
Grayscale by matrix-Reinventor of Python image processing-
Image processing with Python & OpenCV [Tone Curve]
Image processing with Python 100 knock # 12 motion filter
100 image processing by Python Knock # 6 Color reduction processing
Drawing with Matrix-Reinventor of Python Image Processing-
Easy image processing in Python with Pillow
Image processing engineer certification (expert) pass strategy
Image processing with Python 100 knocks # 7 Average pooling
Image processing 100 knock Q.6. Color reduction processing explanation
Light image processing with Python x OpenCV
Image processing with Lambda + OpenCV (gray image creation)
Matrix Convolution Filtering-Reinventor of Python Image Processing-
Image processing with Python 100 knocks # 9 Gaussian filter