这个项目主要关注图像处理技术,包括但不限于边缘检测、图像二值化,分辨率,灰度量化在线处理等。项目采用Python语言和OpenCV库,为图像处理提供了高效和灵活的解决方案。
这个函数负责将输入图像转换为灰度图像(如果还不是),然后使用阈值127进行二值化。
def binarize_image(image):
if len(image.shape) == 3:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
threshold = 127
_, binary_image = cv2.threshold(image, threshold, 255, cv2.THRESH_BINARY)
return binary_image
这两个函数分别返回一个点的4连通和8连通邻居。
def four_connected(x, y):
return [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]
def eight_connected(x, y):
return [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1),
(x - 1, y - 1), (x + 1, y + 1), (x - 1, y + 1), (x + 1, y - 1)]
这个函数实现了基于不同度量标准的边缘检测。
l1: 使用Canny边缘检测
l2: 使用Laplacian边缘检测
基于连通性的度量(4连通和8连通)
def detect_edges(image, metric):
binary_image = binarize_image(image)
edges = np.zeros_like(binary_image, dtype=np.uint8)
height, width = binary_image.shape
inner_func = eight_connected if metric == '内部点8连通,轮廓点4连通' else four_connected
edge_func = four_connected if metric == '内部点8连通,轮廓点4连通' else eight_connected
for x in range(1, height - 1):
for y in range(1, width - 1):
pixel = binary_image[x, y]
inner_neighbors = inner_func(x, y)
is_inner = all(
0 <= i < height and 0 <= j < width and binary_image[i, j] == pixel for i, j in inner_neighbors)
if is_inner:
continue
edge_neighbors = edge_func(x, y)
is_edge = any(0 <= i < height and 0 <= j < width and binary_image[i, j] != pixel for i, j in edge_neighbors)
if is_edge:
edges[x, y] = 255 # Set the edge pixel to white
return edges
Flask== 2.3.3
opencv-python== 4.6.0
numpy== 1.23.4
确保安装了Python和OpenCV。
导入项目代码。
使用detect_edges函数进行边缘检测。
GitHub