尝试使用OpenCV分割字符-照明问题 [英] Trying to segment characters using opencv - Ilumination problem

查看:67
本文介绍了尝试使用OpenCV分割字符-照明问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码无法很好地检测二进制图像!

My code it's not detecting well binary image!

LpImg = cv2.imread('/content/drive/My Drive/TESTING/Placas_detectadas/CPVL92.png')

if (len(LpImg)): #check if there is at least one license image
    # Scales, calculates absolute values, and converts the result to 8-bit.

    plate_image = cv2.convertScaleAbs(LpImg[0], alpha=(255.0))
    plate_image = LpImg #image_cropped

    # convert to grayscale and blur the image
    gray = cv2.cvtColor(plate_image, cv2.COLOR_BGR2GRAY)

    blur = cv2.GaussianBlur(gray,(7,7),0)

    # Applied inversed thresh_binary 
    thresh_inv = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 39, 1)
    #binary = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

    kernel3 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    thre_mor = cv2.morphologyEx(thresh_inv, cv2.MORPH_DILATE, kernel3)

# visualize results    
fig = plt.figure(figsize=(12,7))
plt.rcParams.update({"font.size":18})
grid = gridspec.GridSpec(ncols=2,nrows=3,figure = fig)
plot_image = [plate_image, gray, blur, thresh_inv,thre_mor]
plot_name = ["plate_image","gray","blur","binary","dilation"]

for i in range(len(plot_image)):
    fig.add_subplot(grid[i])
    plt.axis(False)
    plt.title(plot_name[i])
    if i ==0:
        plt.imshow(plot_image[i])
    else:
        plt.imshow(plot_image[i],cmap="gray")

这是图片:

结果如下:

如果我使用自适应阈值

binary = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

此行

thresh_inv = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 39, 1)

我得到了这个结果:

为什么会这样?我该怎么解决?

Why this is happening? How can I solve it?

我当时想使用它:

LpImg = cv2.imread('/content/image.png')

# Set scaling factors and add
gamma1 = 0.3
gamma2 = 1.5
Iout = gamma1*Ioutlow[0:rows,0:cols] + gamma2*Iouthigh[0:rows,0:cols]

# Anti-log then rescale to [0,1]
Ihmf = np.expm1(Iout)
Ihmf = (Ihmf - np.min(Ihmf)) / (np.max(Ihmf) - np.min(Ihmf))
Ihmf2 = np.array(255*LpImg, dtype="uint8")

# Threshold the image - Anything below intensity 65 gets set to white
Ithresh = Ihmf2 < 65 #65
Ithresh = 255*Ithresh.astype("uint8")
Ihmf2 = np.array(255*Ihmf, dtype="uint8")

# Threshold the image - Anything below intensity 65 gets set to white
Ithresh = Ihmf2 < 65 #65

Ithresh = 255*Ithresh.astype("uint8")

结果如下:

但是我仍然想使用此过滤器:

But I still want to use this filters:

  1. 灰度
  2. 模糊
  3. 二值化
  4. 细分

推荐答案

另一种方法是在Python/OpenCV中使用除法归一化.

Another approach is to use division normalization in Python/OpenCV.

  • 阅读输入内容
  • 转换为灰色
  • 应用形态学扩张
  • 用输入的图像划分输入
  • 阈值
  • 保存结果


输入:

import cv2
import numpy as np

# read the image
img = cv2.imread('license_chile.png')

# convert to gray
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# apply morphology
kernel = cv2.getStructuringElement(cv2.MORPH_RECT , (75,75))
smooth = cv2.morphologyEx(gray, cv2.MORPH_DILATE, kernel)

# divide gray by morphology image
division = cv2.divide(gray, smooth, scale=255)

# threshold
result = cv2.threshold(division, 0, 255, cv2.THRESH_OTSU )[1] 

# save results
cv2.imwrite('license_chile_thresh.jpg',result)

# show results
cv2.imshow('smooth', smooth)  
cv2.imshow('division', division)  
cv2.imshow('result', result)  
cv2.waitKey(0)
cv2.destroyAllWindows()


结果:

这篇关于尝试使用OpenCV分割字符-照明问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆