簡體   English   中英

如何消除文本圖像上的黑噪聲?

[英]How to get rid of black noise on text images?

我正在使用此代碼來伽瑪校正圖像,當我得到足夠暗的文本時(我的背景都是白色),如何獲得伽瑪校正圖像隨附的黑色噪聲?

伽瑪校正代碼:

import cv2
import numpy as np

def adjust_gamma(image, gamma=1.0):

   invGamma = 1.0 / gamma
   table = np.array([((i / 255.0) ** invGamma) * 255
      for i in np.arange(0, 256)]).astype("uint8")

   return cv2.LUT(image, table)

x = 'page-1.png'  #location of the image
original = cv2.imread(x, 1)
#cv2.imshow('original', original)

print("Choose a value between 0-1 for gamma correction:")
print("[Choose 0.0 to exit]")

def apply_gamma():
    while True:
        gamma = float(input("\nWhat value would you like the gamma correction to be? "))
        if 1 > gamma > 0:
            adjusted = adjust_gamma(original, gamma=gamma)
            cv2.putText(adjusted, "g={}".format(gamma), (10, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
            cv2.imshow('Original', original)
            cv2.imshow("Gamma corrected image", adjusted)
            cv2.waitKey(0)
            cv2.destroyAllWindows()

            print('\n[press "y" to continue. Press "n" to readjust"]')
            while True:
                gc = input('Are you satisfied with the image? ')

                if gc == 'n':
                    apply_gamma()
                if gc == 'y':
                    print('\nDenoise is next')
                    cv2.imwrite('text1_gc.png', adjusted)
                    break
                else:
                    print('\nInvalid input')
                    gc = True
        break

apply_gamma()

噪音:

您只需要使用threshold值對圖像進行二值化處理即可。 為此,您可以使用cv2.threshold方法。 在此, threshold value = 85

使用此代碼:

import cv2
img = cv2.imread('test.png', 0)
threshold = 85
cv2.threshold(img,threshold,255,cv2.THRESH_BINARY,img)
cv2.imshow('test',img)
cv2.waitKey(0)

OUTPUT:

暫無
暫無

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

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