光照不均匀处理——图像的光补偿


对侧方向光源导致的光照不均匀进行光补偿


注:使用的例子为左侧方向打光

待处理图像:

      

 

 

 目标:实现图像二值化,不能缺少图像信息

一、拟合光照轮廓

  1.对图像整体进行大津阈值分割   

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

plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False

S = cv2.imread('rice3.bmp',0)

position = np.array([],dtype = [('y',int),('x',int)])
thres, Out1 = cv2.threshold(S, 0, 255, cv2.THRESH_OTSU) #大津阈值分割
plt.imshow(Out1,'gray'), plt.title(f'阈值 = {thres}'), plt.show()

      可以看到右侧亮度不够,且轮廓接近二次曲线

 

    2.拟合光照轮廓    

#拟合光照轮廓
for i in range(S.shape[0]):   #行扫描
    flag = 0
    for j in range( S.shape[1]-1, 350, -1 ):  #从右往左进行列扫描
        if Out1[i,j] == 255 and flag == 0 :
            flag = 1
            position = np.insert(position,len(position),(i,j))
            
plt.plot(position['x'],position['y'],'*')            
coef = np.polyfit(position['y'],position['x'],2)
x_fit = np.polyval(coef, position['y'])
plt.plot(x_fit,position['y'] )

      

 

 二、生成光补偿数组  

#生成光补偿数组
def posi(a1,a2):
    for i,j in zip( a1, a2 ):
        yield (i,j)

comp = np.zeros(S.shape,np.float)
for i,j in posi( x_fit, position['y'] ):
    for col in range( int(i)-200, S.shape[1] ):
        comp[j, col] =  min(comp[j, col-1] + 0.09, 255)
plt.imshow(comp, 'gray'), plt.title('补偿数组图像'), plt.show()

      

 

 三、对原图像进行光补偿,并重新阈值分割

S_new = np.uint8( S + comp )  #补偿原图像
cv2.imwrite('rice_new.bmp',S_new)

thres2, Out2 = cv2.threshold(S_new, 0, 255, cv2.THRESH_OTSU) #对补偿后的图像大津阈值分割
plt.imshow(Out2,'gray'), plt.title(f'阈值 = {thres2}')
cv2.imwrite(f'rice_binary({thres2}).bmp',Out2)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM