简体   繁体   English

如何使用python OpenCV缩放图像中字符的粗细?

[英]How can i scale a thickness of a character in image using python OpenCV?

I created one task, where I have white background and black digits. 我创建了一个任务,其中有白色背景和黑色数字。

I need to take the largest by thickness digit. 我需要采用最大的厚度数字。 I have made my picture bw, recognized all symbols, but I don't understand, how to scale thickness. 我已经使图片变白了,可以识别所有符号,但是我不知道如何缩放厚度。 I have tried arcLength(contours), but it gave me the largest by size. 我已经尝试过arcLength(contours),但是它给了我最大的尺寸。 I have tried morphological operations, but as I undestood, it helps to remove noises and another mistakes in picture, right? 我已经尝试过形态学运算,但是由于我不了解,因此有助于消除噪声和图片中的其他错误,对吗? And I had a thought to check the distance between neighbour points of contours, but then I thought that it would be hard because of not exact and clear form of symbols(I draw tnem on paint). 我曾经想过要检查轮廓的相邻点之间的距离,但是后来我觉得这很困难,因为符号的形式不准确也不清晰(我在画上画了些淡点)。 So, that's all Ideas, that I had. 所以,这就是我所有的想法。 Can you help me in this question by telling names of themes in Comp. 您可以通过在Comp中告诉主题名称来帮助我解决这个问题。 vision and OpenCV, that could help me to solve this task? 愿景和OpenCV,这可以帮助我解决此任务吗? I don't need exact algorithm of solution, only themes. 我不需要确切的解决方案算法,只需主题即可。 And if that's not OpenCV task, so which is? 如果这不是OpenCV任务,那是什么? What library? 什么图书馆? Should I learn some pack of themes and basics before the solution of my task? 在解决任务之前,我是否应该学习一些主题和基础知识?

One possible solution that I can think of is to alternate erosion and find contours till you have only one contour left (that should be the thicker). 我可以想到的一种可能的解决方案是交替腐蚀并找到轮廓,直到只剩下一个轮廓(应该更粗)为止。 This could work if the difference in thickness is enough, but I can also foresee many particular cases that can prevent a correct identification, so it depends very much on how is your original image. 如果厚度上的差异足够大,这可能行得通,但是我还可以预见到很多特殊情况会妨碍正确识别,因此这在很大程度上取决于您的原始图像。

Have you thought about drawing a line from a certain point of the contour and look for points where the line intersects your contour? 您是否考虑过从轮廓的某个点绘制一条线并寻找该线与轮廓相交的点? I mean if you get the coordinates from two points you can measure the distance. 我的意思是,如果您从两点获得坐标,则可以测量距离。 I have made a sample to demonstrate what I mean. 我做了一个样本来说明我的意思。 Note that this script is meant just for the demonstration of solution and it will not work with other pictures except my sample one. 请注意,该脚本仅用于解决方案的演示,不适用于除我的示例以外的其他图片。 I would give a better one but I have only encountered with programming a few months back. 我会给一个更好的选择,但几个月前我才遇到编程问题。

在此处输入图片说明

So the first thing is to extract the contours which you said you have already done (mind that cv2.findContours finds white values). 因此,第一件事是提取您已经说过的轮廓(请注意cv2.findContours找到白色值)。 then you can get referential coordinates with cv2.boundingRect() - it returns x,y coordinate, width and height of an bounding rectangle for your contour (you can of course do something similar by extracting a little fracture of your contour on a mask and work from there). 那么您可以使用cv2.boundingRect()获得参考坐标-它返回轮廓的边界矩形的x,y坐标,宽度和高度(您当然可以通过在蒙版上提取轮廓的一点断裂来做类似的操作,从那里工作)。 In my example I defined the center of the box and moved the line slightly downwards then made a line to the left (I have done it by appending to lists and converting it to arrays and there are probably a million better solutions). 在我的示例中,我定义了框的中心,然后将线稍微向下移动,然后向左划了一条线(我通过将其附加到列表并将其转换为数组来完成,可能有上百万种更好的解决方案)。 Then you look for points that are in your contour and in your line (those points are the points of intersection). 然后,寻找轮廓和直线中的点(这些点是交点)。 I have calculated simply by difference of two x coordinates because it works for this demonstration but better approach would be sqrt(x2-x1)^2+(y2-y1)^2 . 我仅通过两个x坐标的差来计算,因为它适用于本演示,但更好的方法是sqrt(x2-x1)^2+(y2-y1)^2 Maybe it will give you an idea. 也许会给你一个主意。 Cheers! 干杯!

Sample code: 样例代码:

import cv2
import numpy as np
import numpy

img = cv2.imread('Thickness2.png')
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray_image,10,255,cv2.THRESH_BINARY_INV)
im2, cnts, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
font = cv2.FONT_HERSHEY_TRIPLEX

for c in cnts:
    two_points = []
    coord_x = []
    coord_y = []
    area = cv2.contourArea(c)
    perimeter = cv2.arcLength(c, False)
    if area > 1 and perimeter > 1:
        x,y,w,h = cv2.boundingRect(c)
        cx = int((x+(w/2))) -5
        cy = int((y+(h/2))) +15
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
        for a in range(cx, cx+70):
            coord_x.append(a)
            coord_y.append(cy)
        coord = list(zip(coord_x, coord_y))
        arrayxy = np.array(coord)
        arraycnt = np.array(c)
        for a in arraycnt:
            for b in arrayxy:
                if a[:,0] == b[0] and a[:,1] == b[1]:
                    cv2.circle(img,(b[0],b[1]), 2, (0,255,255), -1)
                    two_points.append(b)
        pointsarray = np.array(two_points)
        thickness = int(pointsarray[1,0]) - int(pointsarray[0,0])
        print(thickness)              
        cv2.line(img, (cx, cy), (cx+50, cy), (0,0,255), 1)
        cv2.putText(img, 'Thickness : '+str(thickness),(x-20, y-10), font, 0.4,(0,0,0),1,cv2.LINE_AA)

cv2.imshow('img', img)

Output: 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM