转载请注明出处
import sys
import numpy as np
import cv2
import matplotlib
import matplotlib.pyplot as plt
import PIL
from PIL import Image
print(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
像素间的一些基本关系
像素的相信像素
- 坐标处的像素有2个水平的相信像素和2个垂直的相邻像素,它们的坐标是这组像素称为的4邻域,用表示
- 的4个对角相邻像素的坐标是用表示。
- 这些相邻像素和4邻域全称为8邻域
如题一个邻域包含,那么称该邻域为闭邻域,否则称该邻域为开邻域。
def n4p(x_0):
x = np.zeros(5)
y_4 = np.zeros_like(x)
x[0] = x_0
y_4[0] = x_0
x[1] = x[0] + 1
y_4[1] = y_4[0]
x[2] = x[0] - 1
y_4[2] = y_4[0]
x[3] = x[0]
y_4[3] = y_4[0] + 1
x[4] = x[0]
y_4[4] = y_4[0] - 1
return x, y_4
def ndp(x_0):
x = np.zeros(5)
y_D = np.zeros_like(x)
x[0] = x_0
y_D[0] = x_0
x[1] = x[0] + 1
y_D[1] = y_D[0] + 1
x[2] = x[0] + 1
y_D[2] = y_D[0] - 1
x[3] = x[0] - 1
y_D[3] = y_D[0] + 1
x[4] = x[0] - 1
y_D[4] = y_D[0] - 1
return 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.0
img_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.0
img_8 = (img_d + img_4)
img_8[1, 1] = 0.5
plt.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()
距离测试
对于坐标分别为,如果
,
,
则D是一个距离函数或距离测度。之间的区几里得(欧氏)距离定义为:
对于这个距离测度,到点(x, y)的距离小于等于r的像素,是中心在(x, y)、半径为r的圆盘。
(称为街区距离-曼哈顿距离)
(称为棋盘距离)