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

相关推荐
liulilittle12 小时前
Unix/Linux 平台通过 IP 地址获取接口名的 C++ 实现
linux·开发语言·c++·tcp/ip·unix·编程语言
hqyjzsb15 小时前
2025职场进阶:B端产品经理必备的计算机专业技能精要
大数据·开发语言·人工智能·产品经理·编程语言·caie
科技树支点1 天前
无GC的Java创新设计思路:作用域引用式自动内存管理
java·python·go·web·编程语言·编译器
大熊猫侯佩4 天前
韦爵爷闯荡 Swift 6 江湖:单例秘籍新解(上)
swift·编程语言·apple
一支鱼5 天前
前端使用次数最多的工具封装
前端·typescript·编程语言
Jooolin5 天前
【C++】C++11都有什么新特性?
c++·ai编程·编程语言
秋难降7 天前
深入解析快速排序:原理、波动根源与优化之道
算法·排序算法·编程语言
咕白m6259 天前
Java 开发:用 Spire.PDF 高效压缩 PDF 文件
java·编程语言
藤椒鱼不爱编程10 天前
核心类库_常用类
java·编程语言
NekoCNN14 天前
流式处理-与迭代相对的现代编程范式
编程语言