尝试使用opencv分割字符照明问题

2024-09-30 10:36:21 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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")

这是图像:

enter image description here

根据这一结果:

enter image description here

如果我使用自适应阈值

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)

我得到了这个结果:

enter image description here

为什么会这样?我怎样才能解决它

我想用这个:

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")

结果是:

enter image description here

但我仍然想使用这个过滤器:

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

Tags: thetoimageplotnppltcv2plate
1条回答
网友
1楼 · 发布于 2024-09-30 10:36:21

另一种方法是在Python/OpenCV中使用除法规范化

  • 读取输入
  • 变灰
  • 应用形态学膨胀
  • 将输入除以放大的图像
  • 门槛
  • 保存结果


输入:

enter image description here

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()


结果:

enter image description here

相关问题 更多 >

    热门问题