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 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)
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)
Windows 10 Pro anaconda Python = 3.7 conda = 4.9 numpy = 1.18.5 opencv = 4.2.0 (installed from conda-forge)
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
#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
#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
#Comparison before and after processing
bef_aft_img_show(img,cast_ffc)
average brightness:111.5, 110.3
#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
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
A flat image was created by averaging from an existing image, and Flat Field processing was performed.
Recommended Posts