[英]C# how to use OpenCV to remove noise of a captcha?
這是一個非常簡單的方法:
獲取二值圖像。 加載圖像,轉換為灰度,自適應閾值。
隔離所需的字符。 執行形態學開運算以去除椒鹽噪聲。
去除小噪音。 使用輪廓區域查找輪廓並過濾。
反轉圖像。 我們反轉圖像的原因是因為在執行 OCR 時,我們希望所需的文本為黑色,背景為白色。
這是每個步驟的可視化:
二進制圖像
變形開孔+輪廓區域過濾
反轉圖像以獲得結果
這是其他圖像的輸出
我在 python 中實現了這個方法,但你可以將相同的策略應用到 C#
import cv2
# Load image, grayscale, adaptive threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,3)
# Morph open
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
# Remove noise by filtering using contour area
cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
area = cv2.contourArea(c)
if area < 10:
cv2.drawContours(opening, [c], -1, (0,0,0), -1)
# Invert image for result
result = 255 - opening
cv2.imshow('thresh', thresh)
cv2.imshow('opening', opening)
cv2.imshow('result', result)
cv2.waitKey()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.