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能看得更清楚。

相关推荐
有意义10 小时前
栈数据结构全解析:从实现原理到 LeetCode 实战
javascript·算法·编程语言
HyperAI超神经5 天前
【TVM 教程】优化大语言模型
人工智能·语言模型·自然语言处理·cpu·gpu·编程语言·tvm
吴名氏.12 天前
电子书《ASP.NET MVC企业级实战》
后端·asp.net·mvc·编程语言
Mr_Dwj12 天前
【Python】Python 基本概念
开发语言·人工智能·python·大模型·编程语言
加个鸡腿儿13 天前
React项目实战 | 修复Table可展开行,点击一个全部展开
前端·react.js·编程语言
Moonbit13 天前
MoonBit 再获美国知名科技媒体关注:The New Stack 推出 MoonBit Wasm 组件教程
编程语言·webassembly·web components
Moonbit14 天前
MoonBit Pearls Vol.14:哈希表避坑指南
后端·算法·编程语言
Moonbit16 天前
MGPIC 初赛提交倒计时 4 天!
后端·算法·编程语言
今天没有盐18 天前
Pandas缺失值处理完全指南:从基础操作到高级技巧
python·pycharm·编程语言
今天没有盐19 天前
Pandas完全指南:从Series到DataFrame,掌握数据分析核心技能
python·pycharm·编程语言