Python 数字识别彩色屏幕

如何解决Python 数字识别彩色屏幕

我使用python搜索图像识别。似乎没有关于从彩色背景中提取数字的教程,所以我跟着 THIS TUTORIAL

import cv2
import matplotlib.pyplot as plt 

def detect_edge(image):
''' function Detecting Edges '''

    image_with_edges = cv2.Canny(image,100,200)

    images = [image,image_with_edges]

    location = [121,122]

    for loc,img in zip(location,images):
        plt.subplot(loc)
        plt.imshow(img,cmap='gray')

    plt.savefig('edge.png')
    plt.show()

image = cv2.imread('myscreenshot.png',0)
detect_edge(image)

这是我的图片:

enter link description here

结果如下:

edge.png

有什么办法可以打印出这些数字?

解决方法

这里有一些代码可以让这张图片的边缘干净利落。

enter image description here

import cv2
import numpy as np

# load image
img = cv2.imread("numbers.png");

# change to hue colorspace
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV);
h,s,v = cv2.split(hsv);

# use clahe to improve contrast 
# (the contrast is pretty good already,so not much change,but good habit to have here)
clahe = cv2.createCLAHE(clipLimit = 10) 
contrast = clahe.apply(v);

# use canny
canny = cv2.Canny(contrast,20,110);

# show
cv2.imshow('i',img);
cv2.imshow('v',v);
cv2.imshow('c',contrast);
cv2.imshow("canny",canny);
cv2.waitKey(0);

# save
cv2.imwrite("edges.png",canny);

不使用任何 OCR(如 pytesseract 或其他东西),我看不出有什么明显的方法可以始终如一地将此图像转换为“文本”数字。我会把它留给其他人,他们可能知道如何在没有任何模式识别的情况下解决这个问题,因为没有它我什至不知道从哪里开始。如果您愿意放弃该限制,那么 pytessaract 应该没有问题;甚至可能不做这样的处理。

好的,我填写了图片的数字。 OpenCV 的 findContours 的层次结构由于某种原因没有合作,所以我不得不手动执行它,这使得这段代码非常笨拙。老实说,如果我要从头开始再试一次,我会尝试找到有助于少量总像素和每个阈值的颜色,然后组合蒙版。

enter image description here

import cv2
import numpy as np

# check if small box is in big box
def contained(big,small):
    # big corners
    x,y,w,h = big;
    big_tl = [x,y];
    big_br = [x+w,y+h];

    # small corners
    x,h = small;
    small_tl = [x,y];
    small_br = [x+w,y+h];

    # check
    if small_tl[0] > big_tl[0] and small_br[0] < big_br[0]:
        if small_tl[1] > big_tl[1] and small_br[1] < big_br[1]:
            return True;
    return False;

# load image
img = cv2.imread("numbers.png");

# change to hue colorspace
hsv = cv2.cvtColor(img,but good habit to have here)
clahe = cv2.createCLAHE(clipLimit = 10) 
contrast = clahe.apply(v);

# rescale
scale = 2.0;
h,w = img.shape[:2];
h = int(h * scale);
w = int(w * scale);
contrast = cv2.resize(contrast,(w,h),cv2.INTER_LINEAR);
img = cv2.resize(img,cv2.INTER_LINEAR);

# use canny
canny = cv2.Canny(contrast,10,60);

# show
cv2.imshow('i',canny);
cv2.waitKey(0);

# try to fill in contours
# contours
_,contours,hierarchy = cv2.findContours(canny,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE);

# filter contours by size
# filter out noisy bits and the big grid boxes
filtered = [];
for contour in contours:
    perimeter = cv2.arcLength(contour,True);
    if 50 < perimeter and perimeter < 750:
        filtered.append(contour);

# draw contours again
# create a mask of the contoured image
mask = np.zeros_like(contrast);
mask = cv2.drawContours(mask,filtered,-1,255,-1);

# close to get rid of annoying little gaps
kernel = np.ones((3,3),np.uint8)
mask = cv2.dilate(mask,kernel,iterations = 1);
mask = cv2.erode(mask,iterations = 1);

# contours
_,hierarchy = cv2.findContours(mask,cv2.CHAIN_APPROX_NONE);

# alright,hierarchy is being stupid,plan B
# SUUUUUPEEERRR JAAAANK
outer_cntrs = [a for a in range(len(contours))];
children = [];
for a in range(len(contours)):
    if a in outer_cntrs:
        # get current box
        big_box = cv2.boundingRect(contours[a]);
        # check against all other boxes
        for b in range(0,len(contours)):
            if b in outer_cntrs:
                small_box = cv2.boundingRect(contours[b]);
                # remove any children
                if contained(big_box,small_box):
                    outer_cntrs.remove(b);
                    children.append(contours[b]);

# # select by hierarchy
top_cntrs = [];
for a in range(len(contours)):
    if a in outer_cntrs:
        top_cntrs.append(contours[a]);

# create a mask of the contoured image
mask = np.zeros_like(contrast);
mask = cv2.drawContours(mask,top_cntrs,-1);
mask = cv2.drawContours(mask,children,-1);

# close
kernel = np.ones((3,iterations = 1);

# do contours agains because opencv is being super difficult
# honestly,at this point,a fill method would've been better
# contours
_,cv2.CHAIN_APPROX_NONE);

# fill in
for con in contours:
    cv2.fillPoly(mask,pts = [con],color=(255));
for con in children:
    cv2.fillPoly(mask,color=(0));

# resize back down
h,w = mask.shape;
h = int(h / scale);
w = int(w / scale);
mask = cv2.resize(mask,h));

# show
cv2.imshow("mask",mask);
cv2.waitKey(0);

# save
cv2.imwrite("filled.png",mask);
,

您可以分三步找到数字



  1. 自适应阈值结果:

    • enter image description here

    • 这里我们看到 90 与其他数字不同。我们需要移除 9 的边界。

  2. 侵蚀结果:

    • enter image description here
  3. Pytesseract 结果:

    • 8 | 1
      
      5 9
      4 @
      3 | 3
      6 | 1
      
    • 有多种页面分割模式可用于 pytesseract

    • 如果您想从输出中删除 |,您可以使用 re.sub

    • text = re.sub('[^A-Za-z0-9]+',',text)
      
    • 结果将是:

      • 8
        1
        5
        9
        4
        3
        3
        6
        1
        

代码:

import cv2
import pytesseract
import re
import numpy as np

image = cv2.imread("7UUGYHw.png")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,13,2)
erode = cv2.erode(thresh,np.array((7,7)),iterations=1)
text = pytesseract.image_to_string(erode,config="--psm 6")
text = re.sub('[^A-Za-z0-9]+','\n',text)
print(text)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res