【发布时间】:2018-06-09 18:24:12
【问题描述】:
我正在尝试实现在研究论文"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
【问题讨论】:
标签: python numpy opencv image-processing deep-learning