我在Matlab中开发了一个脚本,它分析了一幅彩色画上的雕刻文本。我正在使用一系列的形态学技术来提取文本并用OCR阅读。我需要在Raspberry Pi上实现它,因此我决定将我的Matlab代码转换成OpenCV(python语言)。我试着转移一些方法,它们的工作原理是相似的,但是我如何实现imreconstruct和imbinalize(如下所示)到OpenCV?(这里的挑战是适当区分前景和背景)。在
也许我应该尝试添加grabCut
或getStructuringElement
或{
我将把整个脚本两者如果有人能给我建议,一般如何提高这种提取和OCR过程的准确性,我将不胜感激。在
Based on bin values of grey-scale image. I change some parameters in those functions:
Matlab:
se = strel('disk', 300);
img = imtophat(img, se);
maker = imerode(img, strel('line',100,0)); %for whiter ones
maker = imerode(img, strel('line',85,0)); %for medium
maker = imerode(img, strel('line',5,0));
imgClear = imreconstruct(maker, img);
imgBlur = imgaussfilt(imgClear,1); %less blur for whiter frames
BW = imbinarize(imgBlur,'adaptive','ForegroundPolarity','Bright',...
'Sensitivity',0.7); %process for medium
BW = imbinarize(imgBlur, 'adaptive', 'ForegroundPolarity',...
'Dark', 'Sensitivity', 0.4); % process for black and white
res = ocr(BW, 'CharacterSet', '0123456789', 'TextLayout', 'Block');
res.Text;
^{pr2}$OpenCv
以下是输入图像:
当matlab和opencv似乎都使用相同的算法时,它们之间的差别有多大,我感到惊讶。为什么要运行
imbinarize
两次?sensitivity关键字的实际作用是什么(从数学上讲,在后台)。因为它们显然比光秃秃的大冢还要多出好几步。在显示的图像包括:
经过一番清理:
所以总的来说不一样,当然也没有matlab好。但基本原理似乎是一样的,只是数字需要玩玩。在
一个更好的方法可能是用图像的平均值来做一个阈值,然后用它的输出作为掩模来自适应阈值化原始图像。希望结果会比opencv和matlab都好。在
试着用自适应的THRESH做这个,你可以得到一些非常好的结果,但是周围有更多的垃圾。同样,如果你可以用它作为一个遮罩来隔离文本,然后再做一次tresholding,它可能会更好。同时,侵蚀和膨胀的核的形状也会造成很大的不同。在
我能从你的代码中看到的是你在Matlab代码中使用了tophat过滤作为第一步。但是,我在python OpenCV代码中看不到相同的情况。 Python内置了tophat过滤器,尝试应用它来获得类似的结果
kernel = np.ones((5,5),np.uint8) tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
另外,试着用CLAHE,它能给你的图像提供更好的对比度,然后用blackhat过滤掉小细节。在
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) cl1 = clahe.apply(img)
通过应用这些转换,我得到了更好的结果。在我根据你雕刻的文本样本计算出了一个肯定的结果。在
处理后的图像和OCR输出:
如果你能用更多的样本图像反馈你的测试结果,那就太好了。在
opencvpythontesseractocr
相关问题 更多 >
编程相关推荐