gpt4 book ai didi

python - 基于边缘的二值化

转载 作者:太空宇宙 更新时间:2023-11-03 22:45:33 24 4
gpt4 key购买 nike

我正在尝试实现研究论文 "Automatic License Plate Recognition Using Deep Learning Technique" 中所写的基于边缘的二值化算法但是我在实现它时得到的最终图像全黑并且找不到问题。

import cv2
import numpy as np

def edge_based_binarization(image):
edge_image = cv2.Canny(image, 0, 255)
binary_image = np.zeros(shape=image.shape)
Hblocks = 10
Vblocks = 5

blockH = np.floor(image.shape[0] * 1.0 / Vblocks)
blockH = np.array(blockH, dtype=np.uint8)
blockW = np.floor(image.shape[1] * 1.0 / Hblocks)
blockW = np.array(blockW, dtype=np.uint8)

for r in range(Vblocks):
for c in range(Hblocks):
r0 = r * blockH + 1
c0 = c * blockW + 1

imgblock = image[r0 : r0 + blockH, c0 : c0 + blockW]
edgeblock = edge_image[r0 : r0 + blockH, c0 : c0 + blockW]

t = find_threshold(imgblock, edgeblock)
if(np.all(imgblock < t)):
binary_image[r0 : r0 + blockH, c0 : c0 + blockW] = 1
else:
binary_image[r0 : r0 + blockH, c0 : c0 + blockW] = 0

binary_image = np.array(binary_image, dtype=np.uint8)
return binary_image

def find_threshold(imgblock,edgeblock):
t1 = [ ]
for r in range(3,imgblock.shape[0] - 2):
for c in range(3,imgblock.shape[1] - 2):
if(edgeblock[r,c] == 255 and edgeblock[r,c-1] == 0 and edgeblock[r,c+1] == 0):
m = min(imgblock[r,c-2], imgblock[r,c+2])
if m < 128:
t2 = np.mean(imgblock[r,c-2 : c+2]) * 1.0
t1.append(t2)
if(edgeblock[r,c] == 255 and edgeblock[r-1,c] == 0 and edgeblock[r+1,c] == 0):
m = min(imgblock[r-2,c], imgblock[r+2,c])
if m < 128:
t2 = np.mean(imgblock[r-2 : r+2,c]) * 1.0
t1.append(t2)

if len(t1) == 0:
t = 0
else:
t = np.mean(t1) - np.std(t1)

return t

img = cv2.imread("test1.jpg")
cv2.imshow("Orig", img)
img = edge_based_binarization(img)

cv2.imshow("Edge", img)
cv2.waitKey(0)

论文中给出的代码 find threshold function edge based binarization function

最佳答案

将给定的代码与论文中的内容进行比较后,我必须说论文中附加的实现是错误的。

我没有搜索其他证明,但根据论文中的说法,该算法通过估计 10x5 像素区域内边缘像素的局部阈值并随后从这些相当小的像素组装整个二值图像来对图像进行二值化补丁。

给定的实现将图像分成 10 个垂直 block 和 5 个水平 block ,从而产生更大的区域。

Hblocks = 10
Vblocks = 5

blockH = np.floor(image.shape[0] * 1.0 / Vblocks)
blockH = np.array(blockH, dtype=np.uint8)
blockW = np.floor(image.shape[1] * 1.0 / Hblocks)
blockW = np.array(blockW, dtype=np.uint8)

由于该算法总体上是正确的,因此在估计阈值时,实际上只需要对 block 大小进行小的修正,并对范围大小进行小的调整。

#!/usr/bin/env python

import cv2
import numpy as np
from sys import argv
from math import floor


def edge_based_binarization(image):
edge_image = cv2.Canny(image, 0, 255)
cv2.imwrite("edge.png", edge_image)
binary_image = np.zeros(shape=image.shape)

# Fix block sizes
blockWidth = 10
blockHeight = 5

verticalBlocks = floor(image.shape[0] / blockHeight)
horizontalBlocks = floor(image.shape[1] / blockWidth)

for r in range(verticalBlocks):
for c in range(horizontalBlocks):
# Fix 1 pixel border
r0 = r * blockHeight
c0 = c * blockWidth

imgblock = image[r0: r0 + blockHeight, c0: c0 + blockWidth]
edgeblock = edge_image[r0: r0 + blockHeight, c0: c0 + blockWidth]

t = find_threshold(imgblock, edgeblock)
if(np.all(imgblock > t)):
binary_image[r0: r0 + blockHeight, c0: c0 + blockWidth] = 0
else:
binary_image[r0: r0 + blockHeight, c0: c0 + blockWidth] = 1

return binary_image * 255.0


def find_threshold(imgblock, edgeblock):
t1 = []
# Adjust range indices
# Otherwise it would result in an empty range for the 5px height
for r in range(2, imgblock.shape[0] - 2):
for c in range(2, imgblock.shape[1] - 2):
if(edgeblock[r, c] == 255 and edgeblock[r, c - 1] == 0 and edgeblock[r, c + 1] == 0):
m = min(imgblock[r, c - 2], imgblock[r, c + 2])
if m < 128:
t2 = np.mean(imgblock[r, c - 2: c + 2])
t1.append(t2)
if(edgeblock[r, c] == 255 and edgeblock[r - 1, c] == 0 and edgeblock[r + 1, c] == 0):
m = min(imgblock[r - 2, c], imgblock[r + 2, c])
if m < 128:
t2 = np.mean(imgblock[r - 2: r + 2, c])
t1.append(t2)

if len(t1) == 0:
t = 0
else:
t = np.mean(t1) - np.std(t1)

return t

img = cv2.imread(argv[1])
cv2.imshow("Orig", img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
bin_img = edge_based_binarization(img)

cv2.imshow("Edge", bin_img)
cv2.waitKey(0)

关于python - 基于边缘的二值化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48020440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com