首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检测图片上的符号并保存?

如何检测图片上的符号并保存?
EN

Stack Overflow用户
提问于 2018-05-20 12:24:42
回答 1查看 3.6K关注 0票数 1

我已经创建了简单的神经网络,它可以识别不同的数字和字符。我想让神经网络来识别汽车的车牌。为了做到这一点,我必须将图像上的符号分开。例如,我必须在图像上找到符号并将每个符号保存到文件(png或jpg):

源图:

创建的符号:

文件中的分隔符号:

如何使用python找到符号并将绿色矩形保存为简单的png (或jpg)文件?

EN

回答 1

Stack Overflow用户

发布于 2018-05-21 01:59:57

如果您希望使用OpenCV执行操作,可以查看此解决方案:

您可以通过查找某个区域上方的等高线来执行符号检测。它们对应的边界框可以绘制在相同形状的空白图像上。

代码语言:javascript
复制
import cv2

img = cv2.imread(r'C:\Users\Desktop\pic.png') 
cv2.imshow('Image', img)

#--- create a blank image of the same size for storing the green rectangles (boundaries) ---
black = np.zeros_like(img)

#--- convert your image to grayscale and apply a threshold ---
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
ret2, th2 = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

#--- perform morphological operation to ensure smaller portions are part of a single character ---
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
threshed = cv2.morphologyEx(th2, cv2.MORPH_CLOSE, kernel)

#--- find contours ---
imgContours, Contours, Hierarchy = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for contour in Contours:

    #--- select contours above a certain area ---
    if cv2.contourArea(contour) > 200:

        #--- store the coordinates of the bounding boxes ---
        [X, Y, W, H] = cv2.boundingRect(contour)

        #--- draw those bounding boxes in the actual image as well as the plain blank image ---
        cv2.rectangle(img2, (X, Y), (X + W, Y + H), (0,0,255), 2)
        cv2.rectangle(black, (X, Y), (X + W, Y + H), (0,255,0), 2)

cv2.imshow('contour', img2)
cv2.imshow('black', black)

结果如下:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50431647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档