Python——基於區域性自適應二值化(遞迴法)的裂縫影象分割

2020-10-22 15:00:39

Python------基於區域性自適應二值化(遞迴法)的裂縫影象分割

一、區域性自適應二值化

在這裡插入圖片描述在這裡插入圖片描述

可以參考這幾篇文章:

二、遞迴法區域性自適應二值化介紹

在這裡插入圖片描述
在這裡插入圖片描述

三、Python實現

1、將影象劃分為若干個視窗

Step 1:確定視窗大小

在這裡插入圖片描述
注:因為文章中只說了按照條件,並沒有給出具體的條件,於是,我便自定義了視窗的大小,window_w和window_h,然後計算出每幅影象的視窗數量,視窗越大,視窗數量越少。

#--------劃分視窗---------
#視窗大小
window_w = 11     #11是經過多次試驗後,比較令人滿意的經驗值
window_h = 11
window_size = window_w * window_h
window_w_num = math.floor(mw/window_w)    #取整,影象的寬共有多少個
window_h_num = math.floor(mh/window_h)
print(window_w_num,window_h_num)
#視窗總數
window_num = window_w_num * window_h_num
print(window_num)

Step 2:將影象中每個視窗的值單獨取出,放入windows二維矩陣

注:此是輸入的影象的灰度影象,每一幅影象都是由不同的灰度值畫素點構成,劃分視窗是概念上的理解,真正計算的還是數值。

範例圖片:(宿舍採的裂縫)
在這裡插入圖片描述
影象大小

在做數值處理的時候,劃分視窗就好比怎麼把兩個相同面積但長寬不同的長方體相互轉換。好比:
在這裡插入圖片描述
我想破腦闊,就算用四個迴圈也沒辦法實現。後來我舍友一句話點醒了我,她說轉換不了就打破唄。哦!我又悟了!
於是。。。
在這裡插入圖片描述
那我就先把原來的影象轉化為一維陣列(藍色),再把它調整為我想要的視窗大小,這樣就避免了直接轉換的困難。完美!

windows = []    #二維陣列,存放視窗值(對應紅色矩形框)windows.shape = (window_num, window_size)

for m in range(window_h_num):   #四個迴圈,完成轉換
    for k in range(window_w_num):
        for i in range(window_h):
            for j in range(window_w):
                windows.append(median[i+window_h*m][j+window_w*k])
arr_windows = np.array(windows)    #列表轉陣列
print(arr_windows.shape)
reshape_arr = arr_windows.reshape(window_num,window_size)
print(reshape_arr.shape)

2、預先確定閾值T

在這裡插入圖片描述

F_max = np.amax(reshape_arr, axis=1)   #按行找出最大值
print(reshape_arr[0,:])
#print(F_max[0],F_max.shape)   #713

F_min = np.min(reshape_arr, axis=1)
print(F_min[0])

3、確定各個視窗的最佳閾值

在這裡插入圖片描述

#-----------確定各個視窗的最佳閾值-----------
Ts = np.zeros(window_num)
for i in range(window_num):
   Ts[i] = round((int(F_max[i]) + int(F_min[i])) / 2)
T_uint = np.array(Ts,dtype='uint8')
print(T_uint.shape,T_uint.dtype,F_max.dtype)

# temp = np.zeros(window_size,dtype='uint8')
#print(reshape_arr.shape,temp.shape)
# ground = np.empty(window_size,dtype='uint8')
# crack = np.empty(window_size,dtype='uint8')
ground = []
crack = []
for i in range(window_num):
    T = 0
    temp = reshape_arr[i,:]
    temp = np.array(temp,dtype='uint8')
    T1 = T_uint[i]    #一般T都不會是零
    #print(T1)
    while T1 != T :    #迴圈實現閾值更新
        T = T1
        for j in range(window_size):
            if temp[j]>= T1:
                ground.append(temp[j])
            else:
                crack.append(temp[j])
        R1 = int(np.mean(crack))
        R2 = int(np.mean(ground))
        T1 = int((R1 + R2) / 2)
    #print(T,T1)
    T_uint[i] = T1
print(T_uint.shape,T_uint.dtype) #713個視窗

4、利用OpenCV中的threshold函數實現二值化

#---------區域性自適應二值化-----------
binary_gray = np.zeros((window_num,window_size),dtype='uint8')
for i in range(window_num):
    temp = reshape_arr[i, :]
    temp = np.array(temp, dtype='uint8')
    #temp_reshape = np.reshape(temp,(window_h,window_w))
    ret, th = cv.threshold(temp, T_uint[i], 255, cv.THRESH_BINARY)
    thresh = np.array(th, dtype='uint8')
    thresh = np.squeeze(thresh)
    binary_gray[i, :] = thresh
print(binary_gray.shape,thresh.shape)

5、影象重構輸出

在這裡插入圖片描述

#---------影象重構輸出顯示-------------
binary_gray = np.reshape(binary_gray,window_num*window_size)
print(binary_gray.shape)
c = 0    #測試用的,可以去掉
gray_binary = np.zeros((window_h_num*window_h,window_w_num*window_w),dtype='uint8')
for m in range(window_h_num):
    for k in range(window_w_num):
        for i in range(window_h):
            for j in range(window_w):
                gray_binary[i+window_h*m][j+window_w*k] = binary_gray[c]
                c = c + 1
print(c)
cv.imshow('binary_gray',gray_binary)

到此,遞迴法區域性自適應二值化就完成啦!看一下效果。

四、測試

我們用上述裂縫影象,先用OpenCV自帶的函數看一下效果。

程式碼:

#coding = utf-8
import cv2 as cv
import numpy as np
import math
from skimage import morphology
from skimage import img_as_float
from skimage import img_as_ubyte
import  matplotlib.pyplot as plt
image = cv.imread("C:\\Users\\LENOVO\\Desktop\\004.png")  #你的圖片路徑
# 影象灰度化
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)   #加權平均法 Gray(i,j) = 0.299R(i,j) + 0.578G(i,j) + 0.114B(i,j)  可嘗試其他方法,但目前此方法最優
cv.imshow('show', gray)
ret,th1 = cv.threshold(gray,70,255,cv.THRESH_BINARY)  #全域性二值化
# 3為Block size, 5為param1值
th2 = cv.adaptiveThreshold(gray,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,11,5)  #adaptive_thresh_mean區域性二值化
th3 = cv.adaptiveThreshold(gray,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,5)   #adaptive_thresh_gaussian區域性二值化
titles = ['Gray Image', 'Global Thresholding (v = 70)',
'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [gray, th1, th2, th3]
print(ret)
for i in range(4):
   plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
   plt.title(titles[i])
   plt.xticks([]),plt.yticks([])
plt.show()
while True:
    key = cv.waitKey(10)
    if key == 27:
        cv.destroyAllWindows()    #按Esc鍵退出

效果:
在這裡插入圖片描述
在這裡插入圖片描述
emmmm,全域性的裂縫幾乎快沒了,區域性的一片混亂55555

看一下本文中的方法的結果
在這裡插入圖片描述
貌似還行,,,但這背景也是太雜了。。。
於是,看著OpenCV自帶的函數,我又悟了!-v-

在閾值那裡,把最終得到的最佳閾值都減去5(5應該是經驗值),效果8錯。
在這裡插入圖片描述

五、完整程式碼

程式碼的前面部分是影象預處理。

#coding=utf-8
import cv2 as cv
import numpy as np
import math
from skimage import morphology
from skimage import img_as_float
from skimage import img_as_ubyte
import  matplotlib.pyplot as plt

#1、載入圖片
image = cv.imread("C:\\Users\\LENOVO\\Desktop\\004.png")  #你的圖片路徑
(h, w, d) = image.shape
print("width={}, height={}, depth={}".format(w, h, d))

# 2、影象灰度化
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)   #加權平均法 Gray(i,j) = 0.299R(i,j) + 0.578G(i,j) + 0.114B(i,j)  可嘗試其他方法,但目前此方法最優
cv.imshow('show', gray)

#3、中值濾波  (論文中用的是中值濾波,但我採的圖片沒有很多椒鹽噪聲,所以換為高斯濾波器,名字懶得改了-v-)
median = cv.GaussianBlur(gray, (3, 3), 0)     #sigmaX = 0時,標準差大小由高斯核大小自動確定
cv.imshow('Gaus', median)
(mh, mw) = median.shape
# median = cv.medianBlur(gray,5)     #sigmaX = 0時,標準差大小由高斯核大小自動確定
# cv.imshow('Median', median)
# (mh, mw) = median.shape
print("medianwidth={}, medianheight={}".format(mw, mh))

#4、區域性自適應二值化(遞迴法)
(gh, gw) = gray.shape
print("graywidth={}, grayheight={}".format(gw, gh))
# for i in range(gh):
#     for j in range(gw):
#         print(gray[i][j])

#--------劃分視窗---------
#視窗大小
window_w = 11
window_h = 11
window_size = window_w * window_h
window_w_num = math.floor(mw/window_w)
window_h_num = math.floor(mh/window_h)
print(window_w_num,window_h_num)
#視窗總數
window_num = window_w_num * window_h_num
print(window_num)

windows = []
#print(windows,windows.shape)

for m in range(window_h_num):
    for k in range(window_w_num):
        for i in range(window_h):
            for j in range(window_w):
                windows.append(median[i+window_h*m][j+window_w*k])
arr_windows = np.array(windows)
print(arr_windows.shape)
reshape_arr = arr_windows.reshape(window_num,window_size)
print(reshape_arr.shape)
F_max = np.amax(reshape_arr, axis=1)
print(reshape_arr[0,:])
#print(F_max[0],F_max.shape)   #713

F_min = np.min(reshape_arr, axis=1)
print(F_min[0])

#-----------確定各個視窗的最佳閾值-----------
Ts = np.zeros(window_num)
for i in range(window_num):
   Ts[i] = round((int(F_max[i]) + int(F_min[i])) / 2)
T_uint = np.array(Ts,dtype='uint8')
print(T_uint.shape,T_uint.dtype,F_max.dtype)

# temp = np.zeros(window_size,dtype='uint8')
#print(reshape_arr.shape,temp.shape)
# ground = np.empty(window_size,dtype='uint8')
# crack = np.empty(window_size,dtype='uint8')
ground = []
crack = []
for i in range(window_num):
    T = 0
    temp = reshape_arr[i,:]
    temp = np.array(temp,dtype='uint8')
    T1 = T_uint[i]    #一般T都不會是零
    #print(T1)
    while T1 != T :
        T = T1
        for j in range(window_size):
            if temp[j]>= T1:
                ground.append(temp[j])
            else:
                crack.append(temp[j])
        R1 = int(np.mean(crack))
        R2 = int(np.mean(ground))
        T1 = int((R1 + R2) / 2)
    #print(T,T1)
    T_uint[i] = T1 - 5
print(T_uint.shape,T_uint.dtype) #713個視窗

#---------區域性自適應二值化-----------
binary_gray = np.zeros((window_num,window_size),dtype='uint8')
for i in range(window_num):
    temp = reshape_arr[i, :]
    temp = np.array(temp, dtype='uint8')
    #temp_reshape = np.reshape(temp,(window_h,window_w))
    ret, th = cv.threshold(temp, T_uint[i], 255, cv.THRESH_BINARY)
    thresh = np.array(th, dtype='uint8')
    thresh = np.squeeze(thresh)
    binary_gray[i, :] = thresh
print(binary_gray.shape,thresh.shape)

#---------影象重構輸出顯示-------------
binary_gray = np.reshape(binary_gray,window_num*window_size)
print(binary_gray.shape)
c = 0
gray_binary = np.zeros((window_h_num*window_h,window_w_num*window_w),dtype='uint8')
for m in range(window_h_num):
    for k in range(window_w_num):
        for i in range(window_h):
            for j in range(window_w):
                gray_binary[i+window_h*m][j+window_w*k] = binary_gray[c]
                c = c + 1
print(c)
cv.imshow('binary_gray',gray_binary)
plt.imsave('C:\\Users\\LENOVO\\Desktop\\binary_gray.png',gray_binary)

while True:
    key = cv.waitKey(10)
    if key == 27:
        cv.destroyAllWindows()    #按Esc鍵退出

六、不足

1、演演算法時間較長,比如上面那張圖987x742,需要跑大概十幾分鍾,OpenCV自帶的函數就幾秒鐘
2、特定場景表現得較好,有些場景表現不好

例如下面這張圖:
在這裡插入圖片描述
OpenCV自帶函數的二值化效果
在這裡插入圖片描述
在這裡插入圖片描述
本文中的遞迴法(不減去5):
在這裡插入圖片描述
本文中的遞迴法(減去5):
在這裡插入圖片描述
結論:減去5應用比較廣,且細節清楚,但整體效果還是不如OpenCV自帶的。

*** 第一次寫部落格,記錄更容易成長。知道自己很菜,但慢慢來。若有錯誤之處,歡迎指正交流。***