目录
- 像素间的一些基本关系
- 像素的相邻像素
- 距离测试
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__}")
Python version: 3.6.12 |Anaconda, Inc.| (default, Sep 9 2020, 00:29:25) [MSC v.1916 64 bit (AMD64)]
Numpy version: 1.16.6
Opencv version: 3.4.1
Matplotlib version: 3.3.2
Pillow version: 8.0.1
像素间的一些基本关系
像素的相邻像素
- N4(p)N_4(p)N4(p)
- 坐标(x,y)(x, y)(x,y)处的像素ppp有2个水平的相信像素和2个垂直的相邻像素,它们的坐标是(x+1,y),(x−1,y),(x,y+1),(x,y−1)(x+1, y),(x-1, y),(x, y+1),(x, y-1)(x+1,y),(x−1,y),(x,y+1),(x,y−1)这组像素称为ppp的4邻域,用N4(p)N_4(p)N4(p)表示
- ND(p)N_D(p)ND(p)
- ppp的4个对角相邻像素的坐标是(x+1,y+1),(x+1,y−1),(x−1,y+1),(x−1,y−1)(x+1, y+1),(x+1,y-1),(x-1,y+1),(x-1, y-1)(x+1,y+1),(x+1,y−1),(x−1,y+1),(x−1,y−1)用ND(p)N_D(p)ND(p)表示。
- N8(p)N_8(p)N8(p)
- 这些相邻像素和4邻域全称为8邻域
如题一个邻域包含ppp,那么称该邻域为闭邻域,否则称该邻域为开邻域。
def n4p(x_0):x = np.zeros(5)y_4 = np.zeros_like(x)x[0] = x_0y_4[0] = x_0x[1] = x[0] + 1y_4[1] = y_4[0]x[2] = x[0] - 1y_4[2] = y_4[0]x[3] = x[0]y_4[3] = y_4[0] + 1x[4] = x[0]y_4[4] = y_4[0] - 1return x, y_4def ndp(x_0):x = np.zeros(5)y_D = np.zeros_like(x)x[0] = x_0y_D[0] = x_0x[1] = x[0] + 1y_D[1] = y_D[0] + 1x[2] = x[0] + 1y_D[2] = y_D[0] - 1x[3] = x[0] - 1y_D[3] = y_D[0] + 1x[4] = x[0] - 1y_D[4] = y_D[0] - 1return x, y_D
x_4, y_4 = n4p(0)
x_d, y_d = ndp(0)
x_8 = np.concatenate((x_4, x_d))
y_8 = np.concatenate((y_4, y_d))plt.figure(figsize=(15, 5))
plt.subplot(1,3,1), plt.scatter(x_4, y_4), plt.title("N_4"),# plt.xticks([]), plt.yticks([])
plt.subplot(1,3,2), plt.scatter(x_d, y_d), plt.title("N_D"),# plt.xticks([]), plt.yticks([])
plt.subplot(1,3,3), plt.scatter(x_8, y_8), plt.title("N_8"),# plt.xticks([]), plt.yticks([])
plt.tight_layout()
plt.show()
# 图像的邻域
height, width = 3, 3
img_ori = np.zeros([height, width], dtype=np.float)img_4 = img_ori.copy()
img_4[0, 1] = 1.0
img_4[1, 0] = 1.0
img_4[1, 1] = 1.0
img_4[1, 2] = 1.0
img_4[2, 1] = 1.0img_d = img_ori.copy()
img_d[0, 0] = 1.0
img_d[0, 2] = 1.0
img_d[1, 1] = 1.0
img_d[2, 0] = 1.0
img_d[2, 2] = 1.0img_8 = (img_d + img_4)
img_8[1, 1] = 0.5plt.figure(figsize=(15, 5))
plt.subplot(1,3,1), plt.imshow(img_4, 'gray'), plt.title("N_4"),# plt.xticks([]), plt.yticks([])
plt.subplot(1,3,2), plt.imshow(img_d, 'gray'), plt.title("N_D"),# plt.xticks([]), plt.yticks([])
plt.subplot(1,3,3), plt.imshow(img_8, 'gray'), plt.title("N_8"),# plt.xticks([]), plt.yticks([])
plt.tight_layout()
plt.show()
距离测试
对于坐标分别为(x,y),(u,v)和(w,z)的像素p,q和s(x, y),(u,v)和(w, z)的像素p, q和s(x,y),(u,v)和(w,z)的像素p,q和s,如果
(a)D(p,q)≥0[D(p,q)=0,当且仅当p=q](a)\; D(p, q) \ge 0 \; [D(p,q) = 0, 当且仅当p=q](a)D(p,q)≥0[D(p,q)=0,当且仅当p=q],
(b)D(p,q)==D(q,p)且(b)\; D(p, q) == D(q, p) 且(b)D(p,q)==D(q,p)且
(c)D(p,s)≤D(p,q)+D(q,s)(c)\; D(p, s) \leq D(p, q) + D(q, s)(c)D(p,s)≤D(p,q)+D(q,s),
则D是一个距离函数或距离测度。p和qp 和 qp和q之间的区几里得(欧氏)距离定义为:
De(p,q)=[(x−u)2+(y−v)2]12(2.19)D_{e}(p, q) = \Big[(x - u)^2 + (y -v)^2 \Big]^{\frac{1}{2}} \tag{2.19}De(p,q)=[(x−u)2+(y−v)2]21(2.19)
对于这个距离测度,到点(x, y)的距离小于等于r的像素,是中心在(x, y)、半径为r的圆盘。
p和q之间的距离D4p和q之间的距离D_4p和q之间的距离D4(称为街区距离-曼哈顿距离)
D4(p,q)=∣x−u∣+∣y−v∣(2.20)D_4(p, q) = |x - u| + |y - v| \tag{2.20}D4(p,q)=∣x−u∣+∣y−v∣(2.20)
p和q之间的距离D8p和q之间的距离D_8p和q之间的距离D8(称为棋盘距离)
D4(p,q)=max(∣x−u∣,∣y−v∣)(2.21)D_4(p, q) = max(|x - u|, |y - v|) \tag{2.21}D4(p,q)=max(∣x−u∣,∣y−v∣)(2.21)