[西门子] Halcon转OpenCV实例--OCR字符识别(附源码)

[复制链接]
查看38167 | 回复0 | 2024-2-3 10:37:10 | 显示全部楼层 |阅读模式
点击下方卡片,关注“OpenCV与AI深度学习”

视觉/图像重磅干货,第一时间送达!

导  读

    本文主要介绍Halcon转OpenCV实例--OCR字符识别(附源码)。

     

实例来源

    实例来源于51Halcon论坛的帖子,原贴地址:

    https://www.51halcon.com/forum.php?mod=viewthread&tid=889  


     

Halcon实现

    测试图:



    实现代码与效果:
read_image (Image, 'ET.png')decompose3 (Image, Image1, Image2, Image3)trans_from_rgb(Image1, Image2, Image3, ImageResult1, ImageResult2, ImageResult3, 'hsv')dev_get_window (WindowHandle)draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)reduce_domain (ImageResult3, Rectangle, ImageReduced)threshold(ImageReduced, Region, 180, 255)connection (Region, ConnectedRegions)select_shape (ConnectedRegions, SelectedRegions, ['width','height'], 'and', [10,20.24], [50,32.994])
count_obj(SelectedRegions,Number)sort_region (SelectedRegions, SortedRegions1, 'character', 'true', 'column')
invert_image (ImageReduced, ImageInvert)read_ocr_class_mlp ('Industrial_0-9_Rej.omc', OCRHandle)
dev_display (Image)
for Index:=1 to Number by 1    select_obj (SortedRegions1, ObjectSelected, Index)    do_ocr_multi_class_mlp (ObjectSelected, ImageInvert, OCRHandle , Class, Confidence)    area_center (ObjectSelected, Area, Row, Column)    disp_message (WindowHandle, Class, 'image', Row+50, Column, 'black', 'true')
endfor


     

OpenCV实现步骤与代码

    测试图:



    说明:由Halcon实现可见,字符识别只做了一部分,这一部分由手动设定ROI来选取,如下图所示:



    OTSU阈值分割:
gray = cv2.cvtColor(ROI, cv2.COLOR_BGR2GRAY)_,thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)cv2.imshow("Otsu", thresh)

    轮廓提取与筛选:
cnts,_= cv2.findContours(thresh, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for c in cnts:  (x, y, w, h) = cv2.boundingRect(c)  if w >= 10 and w <= 50 and h >= 20 and h <= 33:


    OCR识别(基于EasyOCR):
result = reader.readtext(char_roi)strText = result[0][1][0]


    完整源码:
#公众号:OpenCV与AI深度学习import cv2import numpy as npimport imutilsimport pytesseract #识别失败import easyocrreader = easyocr.Reader(['en'])
image = cv2.imread('ET.png')ROI = image[253:318,86:1440]cv2.imshow('ROI',ROI)
gray = cv2.cvtColor(ROI, cv2.COLOR_BGR2GRAY)_,thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)cv2.imshow("Otsu", thresh)cv2.imwrite('thres.jpg', thresh)
cnts,_= cv2.findContours(thresh, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for c in cnts:  (x, y, w, h) = cv2.boundingRect(c)  if w >= 10 and w <= 50 and h >= 20 and h <= 33:    char_roi = thresh[y-4:y+h+8,x-4:x+w+8]    cv2.imshow('char_roi',char_roi)    cv2.waitKey()    #text = pytesseract.image_to_string(char_roi, lang='chi_sim',  \    #    config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')    # 打印识别后的文本    #print(text)    result = reader.readtext(char_roi)    strText = result[0][1][0]     #print(strText)    #cv2.drawContours(ROI,c,-1,(0,255,0),3)    cv2.rectangle(ROI,(x,y),(x+w,y+h),(0,0,255),1)    cv2.putText(image,strText,(86+x,253+y+50), 0, 0.8, (0,255,0), 2)  cv2.imshow("chars", ROI)cv2.imshow('result',image)cv2.imwrite('result.jpg',image)
cv2.waitKey()cv2.destroyAllWindows()
相关文章推荐:

Halcon缺陷检测实例转OpenCV实现(一)

Halcon缺陷检测实例转OpenCV实现(二)

Halcon缺陷检测实例转OpenCV实现(三)

Halcon缺陷检测实例转OpenCV实现(四)

Halcon缺陷检测实例转OpenCV实现(五) 混合颜色药片缺陷检测

Halcon缺陷检测实例转OpenCV实现(六) 瓶口缺陷检测

Halcon转OpenCV实例--复杂背景下缺陷检测(附源码)

Halcon转OpenCV实例--去除纸张中的颜色笔迹/墨迹(附源码)

Halcon实例转OpenCV--低对比度图像中提取圆形轮廓(附源码)

实战 | Halcon实例转OpenCV实现-复杂背景下OCR数字识别(附源码)

—THE END—
下载1:Pytorch常用函数手册

在「OpenCV与AI深度学习」公众号后台回复:Pytorch函数手册,即可下载学习全网第一份Pytorch函数常用手册,包括Tensors介绍、基础函数介绍、数据处理函数、优化函数、CUDA编程、多处理等十四章内容。
下载2:145个OpenCV实例应用代码
在「OpenCV与AI深度学习」公众号后台回复:OpenCV145,即可下载学习145个OpenCV实例应用代码(Python和C++双语言实现)。



欢迎加入CV学习交流微信群!


觉得有用,记得点个赞和在看

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册哦

x
您需要登录后才可以回帖 登录 | 注册哦

本版积分规则