This Chapter is all about image segmentation.
I still not finished whole chapter, but here try to publish some for reference.
這里寫目錄標題
- 基礎知識
import sys
import numpy as np
import cv2
import matplotlib
import matplotlib.pyplot as plt
import PIL
from PIL import Imageprint(f"Python version: {sys.version}")
print(f"Numpy version: {np.__version__}")
print(f"Opencv version: {cv2.__version__}")
print(f"Matplotlib version: {matplotlib.__version__}")
print(f"Pillow version: {PIL.__version__}")
def normalize(x, ymin=0, ymax=1):"""Description:- Map x into desired range[ymin, ymax], according to the math function $$y = \frac{(y_{\text{max}} - y_{\text{min}}) (x - x_{\text{min}})}{(x_{\text{max}} - x_{\text{min}})} + y_{\text{min}}$$Parameters:- x: input array- ymin: desired min value, such as -1, or whatever you want- ymax: desired max value, such as 1, or other you need"""result = (ymax - ymin) * (x - x.min()) / (x.max()-x.min()) + ymin#################### old one ####################### result = (x - x.min()) / (x.max() - x.min())return result
基礎知識
令RRR表示一幅圖像占據的整個空間區域。我們可以將圖像分割視為把RRR分為nnn個子區域R1,R2,?,RnR_1,R_2,\cdots,R_nR1?,R2?,?,Rn?的過程,滿足:
- (a) ?i=1nRi=R\bigcup_{i=1}^{n} R_{i} = R?i=1n?Ri?=R。
- (b) Ri,i=0,1,2,?,nR_{i}, i=0, 1, 2, \cdots, nRi?,i=0,1,2,?,n 是一個連通集。
- (c) 對所有iii和jjj, i≠j,Ri?Rj=?i\neq j, R_{i}\bigcap R_{j} = \emptyseti?=j,Ri??Rj?=?。
- (d) Q(Ri)=TRUE,i=0,1,2,?,nQ(R_{i})=TRUE, i=0, 1, 2, \cdots, nQ(Ri?)=TRUE,i=0,1,2,?,n。
- (e) 對于任何鄰接區域RiR_{i}Ri?和RjR_{j}Rj?, Q(Ri?Rj)=FALSEQ(R_{i} \bigcup R_{j}) = FALSEQ(Ri??Rj?)=FALSE。
其中Q(Ri)Q(R_{i})Q(Ri?)是定義在集合RkR_{k}Rk?中的點上的一個謂詞邏輯。
def std_seg(image, thred=0, stride=4):"""Description:Segment image caculating standard deviationParemeters:image: input grayscale imagethred: thredshold of the standard diviationstride: control the neighborhoodReturn:Binary segment image"""h, w = image.shape[:2]result = image.copy()# here we use stride the create non-overlap region, if we not need stride here, we still can get very good result# or we set stride smaller than 8, then we can get better resultfor i in range(0, h, stride):for j in range(0, w, stride):temp = image[i:i+stride, j:j+stride]if np.std(temp) <= thred:result[i:i+stride, j:j+stride] = 0else:result[i:i+stride, j:j+stride] = 255return result
# standard deviation segment, according the result below, it seems the img_f is not 8x8 region, is about 4-5
img_d = cv2.imread("DIP_Figures/DIP3E_Original_Images_CH10/Fig1001(d)(noisy_region).tif", -1)
img_seg = std_seg(img_d, thred=0, stride=5)fig = plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1), plt.imshow(img_d, 'gray'), plt.xticks([]), plt.yticks([])
plt.subplot(1, 2, 2), plt.imshow(img_seg, 'gray'), plt.xticks([]), plt.yticks([])
plt.tight_layout()
plt.show()