Python——基于區域自適應二值化(遞回法)的裂縫影像分割
- Python------基于區域自適應二值化(遞回法)的裂縫影像分割
- 一、區域自適應二值化
- 二、遞回法區域自適應二值化介紹
- 三、Python實作
- 1、將影像劃分為若干個視窗
- 2、預先確定閾值T
- 3、確定各個視窗的最佳閾值
- 4、利用OpenCV中的threshold函式實作二值化
- 5、影像重構輸出
- 四、測驗
- 五、完整代碼
- 六、不足
Python------基于區域自適應二值化(遞回法)的裂縫影像分割
一、區域自適應二值化


可以參考這幾篇文章:
- 影像的自適應二值化
- OpenCV全域/區域閥值二值化
- OpenCV_基于區域自適應閾值的影像二值化
二、遞回法區域自適應二值化介紹


三、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自帶的,
*** 第一次寫博客,記錄更容易成長,知道自己很菜,但慢慢來,若有錯誤之處,歡迎指正交流,***
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/188243.html
標籤:其他
