aardio 图像处理

今天终于学会了编程中的 OCR 技术!原来计算机真的能识别图片里的文字,这种让程序 "看懂" 图像的能力太神奇了,赶紧把学习过程记录下来。

一、初识OCR:让程序读懂图片文字

(一)简单识别实验

OCR就像给程序装上"火眼金睛",我先试了试识别本地图片:

arduino 复制代码
import console;
import string.ocrLite
import string.ocrLite.defaultModels;

var imgpath = "图片路径";
var ocr = string.ocrLite ();

var bit = gdip.bitmap(imgpath);
var text = ocr.detectBitmap(bit);

for(i, value in table.eachIndex(text.blocks)){
console.log(value.text)
}
console.pause();

运行时就像看着程序用"放大镜"逐字扫描图片,当控制台输出文字的那一刻,一种满满的自豪感由然而出!

接着试了网络图片识别,原来只要用inet.http()模块获取图片数据,后面的步骤和本地识别差不多:

arduino 复制代码
import inet.http;
import console;
import string.ocrLite
import string.ocrLite.defaultModels;
var http = inet.http(); 
var imgurl = "图片的url"; 
var ingData = http.get(imgurl)
var ocr = string.ocrLite();
var bit = gdip.bitmap(ingData);
var text = ocr.detectBitmap(bit);
for(i, value in table.eachIndex(text.blocks)){
    console.log(value.text)
}
console.pause();

(二)提高识别准确率的秘诀

我发现图片质量不好时,识别会出错。还能用"图片美容术":

  1. 灰度化处理:把彩色图片变成黑白照片,文字会更清晰
arduino 复制代码
import soImage
import console;
import string.ocrLite
import string.ocrLite.defaultModels;

img = soImage()
img.load("需要转换为灰度图的图片路径") 
// 转换图片为灰度图
img.grayScale()
img.save("保存路径") 

imgpath = "保存路径" 
var ocr = string.ocrLite(); 
var bit = gdip.bitmap(imgpath); 
var text = ocr.detectBitmap(bit); 
for(i, value in table.eachIndex(text.blocks)){
    console.log(value.text)
}
console.pause();

试了下,原本有点模糊的图片处理后,文字边缘果然更清楚了!

二、挑战

今天的挑战是处理这张网络图片:aardio.online/upload/file...

我把学到的知识组合起来,写出了完整代码:

arduino 复制代码
import soImage;
import console;
import inet.http;
import string.ocrLite;
import string.ocrLite.defaultModels;

var img = soImage();
var http = inet.http();

var imageUrl = "https://aardio.online/upload/files/20250423/1745395458.png";
string.save("D:/aaaaaaac.jpg", http.get(imageUrl));

img.load("D:/aaaaaaac.jpg");
//图片灰度化处理
img.grayScale();
img.save("D:/aaabbb.jpg");

var ocr = string.ocrLite();
var bit = gdip.bitmap("D:/aaabbb.jpg");
var imgtext = ocr.detectBitmap(bit);

for(i, value in table.eachIndex(imgtext.blocks)) {
    console.log(value.text)
}
console.pause();

运行时看着程序一步步下载、处理、识别,最后正确输出文字时,成就感爆棚!原来复杂任务都是由一个个小步骤组成的。

三、总结

今天最大的收获是明白OCR不是魔法,而是通过"图片预处理+识别算法"实现的。当图片质量差时,预处理就像给眼睛戴上眼镜,让OCR能看得更清楚。

相关推荐
神奇啊龙1 小时前
Java和Go各方面对比:现代编程语言的深度分析
编程语言
木叶丸7 小时前
编程开发中,那些你必须掌握的基本概念
前端·数据结构·编程语言
楽码4 天前
终于说清楚!希腊字符如何进入数学或科学场景
openai·编程语言·trae
用户05956611920914 天前
Java 入门之循环结构基础详细讲解
java·性能优化·编程语言
Moonbit15 天前
MoonBit Pearls Vol.02:MoonBit 中的面向对象编程
编程语言
Codebee15 天前
OneCode基础组件介绍——树形组件(Tree)
前端·编程语言
Mirageef15 天前
aardio 事件驱动
编程语言
大熊猫侯佩16 天前
Swift 隐藏宝藏:“逆天改命”调整方法重载(function overloading)优先级
swift·编程语言·apple
大熊猫侯佩16 天前
Swift 入门之自定义类型的模式匹配(Pattern Matching)
swift·编程语言·apple
Mirageef17 天前
aardio 并行任务处理
编程语言