我一直在尝试清除 OCR 图像:(线条)
我需要删除这些行以有时进一步处理图像并且我已经非常接近但很多时间阈值从文本中带走了太多:
copy = img.copy()
blur = cv2.GaussianBlur(copy, (9,9), 0)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,11,30)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
dilate = cv2.dilate(thresh, kernel, iterations=2)
cnts = cv2.findContours(dilate, 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 > 300:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(copy, (x, y), (x + w, y + h), (36,255,12), 3)
Run Code Online (Sandbox Code Playgroud)
编辑:此外,如果字体发生变化,使用常量数字将不起作用。有没有通用的方法来做到这一点?
我想从这样的图像中提取文本(主要是数字)
我试过这个代码
import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img = Image.open('1.jpg')
text = pytesseract.image_to_string(img, lang='eng')
print(text)
Run Code Online (Sandbox Code Playgroud)
但我得到的只是这个(hE PPAR)
通过tesseract-OCR,我试图从以下带有红色背景的图像中提取文本。
我在提取框 B 和 D 中的文本时遇到问题,因为有垂直线。我怎样才能像这样清理背景:
输入:
输出:
我有一张可爱的魔方的照片:
我想把它分成几个正方形并确定每个正方形的颜色。我可以对其运行高斯模糊,然后运行“Canny”,最后运行“Dilate”以获得以下结果:
这看起来看起来不错,但我无法将其变成正方形。我尝试的任何类型的“findContours”都只能显示一两个方块。距离我的目标九还差得很远。除此之外,人们对我还能做什么有什么想法吗?
目前最佳解决方案:
代码如下,需要 numpy + opencv2。它需要一个名为“./sides/rubiks-side-F.png”的文件,并将多个文件输出到“steps”文件夹。
import numpy as np
import cv2 as cv
def save_image(name, file):
return cv.imwrite('./steps/' + name + '.png', file)
def angle_cos(p0, p1, p2):
d1, d2 = (p0-p1).astype('float'), (p2-p1).astype('float')
return abs(np.dot(d1, d2) / np.sqrt(np.dot(d1, d1)*np.dot(d2, d2)))
def find_squares(img):
img = cv.GaussianBlur(img, (5, 5), 0)
squares = []
for gray in cv.split(img):
bin = cv.Canny(gray, 500, 700, apertureSize=5)
save_image('post_canny', bin)
bin = cv.dilate(bin, None)
save_image('post_dilation', bin)
for thrs in range(0, 255, 26):
if thrs != …Run Code Online (Sandbox Code Playgroud)