簡體   English   中英

我應該如何從 python 中的 7 段顯示器中正確提取數字

[英]How should I properly extract the digital from 7 segment display in python

我正在進行一個關於從 7 段顯示器中提取數字的項目,我正在遵循本指南: https://pyimagesearch.com/2017/02/13/recognizing-digits-with-opencv-and-python/

首先,我已經成功提取了 LED 顯示屏的 ROI,但在生成灰黑色圖像以使用 `cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) 查找數字時遇到了一些困難

我應該怎么做才能在陰影下生成灰度圖像?

原始照片:

原始照片

提取的黑白照片:

黑白照片

代碼:


    img_name = 'test2.jpeg'
    image = cv2.imread(img_name)

    image = imutils.resize(image, height=1000)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(blurred, 50, 200, 255)


    #cv2.imshow("test", edged)
    #cv2.waitKey(0)

    cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)

    displayCnt = None
    # loop over the contours
    for c in cnts:
        # approximate the contour
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)
        # if the contour has four vertices, then we have found
        # the thermostat display
        if len(approx) == 4:
            displayCnt = approx
            break
    warped = four_point_transform(gray, displayCnt.reshape(4, 2))
    output = four_point_transform(image, displayCnt.reshape(4, 2))

    thresh = cv2.threshold(warped, 222, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1]
    cv2.imwrite("black.png", thresh)

由於圖像的不同部分具有不同的整體亮度級別,全局閾值將導致圖像的某些部分的閾值過低和過高。 這可以通過在圖像上使用中值濾波器來確定整個圖像的局部閾值來補救。 以下是描述的步驟(並使用 Paint.NET 進行了演示)。

  1. 對圖像應用中值濾波器

中值濾波器

  1. 取原始圖像和過濾圖像之間的差異並將其轉換為灰度

原始圖像和模糊圖像之間的差異(灰度)

  1. 在此新圖像上使用全局閾值

臨界點

關於亮度差異,可以應用划分歸一化和銳化:

smooth = cv2.GaussianBlur(warped, (95,95), 0)
division = cv2.divide(warped, smooth, scale=255)
sharp = filters.unsharp_mask(division, radius=1.5, amount=1.5, 
        multichannel=False, preserve_range=False)
sharp = (255*sharp).clip(0,255).astype(np.uint8)

Output:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM