Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Opencv can';t检测图像上的等号_Opencv_Image Processing_Computer Vision_Detection - Fatal编程技术网

Opencv can';t检测图像上的等号

Opencv can';t检测图像上的等号,opencv,image-processing,computer-vision,detection,Opencv,Image Processing,Computer Vision,Detection,我在做一个计算机视觉项目,它能理解数学符号。除“相等”标记外,它可正确检测所有标记。但“等号”被视为两个独立的“负号” image=cv2.imread('./deneme.png') 灰色=cv2.CVT颜色(image.copy(),cv2.COLOR\u bgr2灰色) ret,thresh=cv2.threshold(grey.copy(),0,255,cv2.thresh\u BINARY\u INV) 轮廓,u=cv2.findContours(thresh.copy(),cv2.R

我在做一个计算机视觉项目,它能理解数学符号。除“相等”标记外,它可正确检测所有标记。但“等号”被视为两个独立的“负号”

image=cv2.imread('./deneme.png')
灰色=cv2.CVT颜色(image.copy(),cv2.COLOR\u bgr2灰色)
ret,thresh=cv2.threshold(grey.copy(),0,255,cv2.thresh\u BINARY\u INV)
轮廓,u=cv2.findContours(thresh.copy(),cv2.RETR\u外部,cv2.CHAIN\u近似值\u简单值)
预处理的_数字=[]
对于等高线中的c:
x、 y,w,h=cv2.boundingRect(c)
cv2.矩形(图像,(x,y),(x+w,y+h),颜色=(0,255,0),厚度=2)
数字=阈值[y:y+h,x:x+w]
数字=平方(数字)
预处理的数字。追加(数字)
plt.imshow(图像,cmap=“灰色”)
plt.show()


我不知道如何解决这个问题。如何检测等号?

这就是您想要达到的结果吗

正如@Micka所指出的,由于你的数学表达式似乎是水平书写的,你对等号的两个不同组成部分之间的关系有很强的先验知识,因此有一种直接的(但很粗糙的)方法来检测何时减号实际上是相等的:

import cv2
import matplotlib.pyplot as plt
import numpy as np

class Rect:
    def __init__(self,
            a,
            b,
            c,
            d):
        self.a=a
        self.b=b
        self.c=c
        self.d=d

        self.center=np.array([(a+c)/2, (b+d)/2])

def merge_rectangles(r_1, r_2):

    a_m= min(r_1.a, r_2.a)
    b_m= min(r_1.b, r_2.b)
    c_M= max(r_1.c, r_2.c)
    d_M= max(r_1.d, r_2.d)

    return Rect(a_m, b_m, c_M, d_M)

def they_form_an_equal_sign(rect_1,
        rect_2,
        tol_w=10,#in pixels
        tol_h=10):
    #check if the bounding boxes approximately align
    b0 = (np.abs(rect_1.a - rect_2.a) < tol_w ) and (np.abs(rect_1.c - rect_2.c) <     tol_w)

    #check if the bounding boxes have approximately the same height
    h1 = np.abs(rect_1.d - rect_1.b)
    h2 = np.abs(rect_2.d - rect_2.b)
    b1 = np.abs(h1-h2) < tol_h

    return b0 and b1

image = cv2.imread('/tmp/m.png')
grey = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(grey.copy(), 0, 255, cv2.THRESH_BINARY_INV)
contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,     cv2.CHAIN_APPROX_SIMPLE)

candidate_rectangles=[]
for c in contours:
    x,y,w,h = cv2.boundingRect(c)
    candidate_rectangles.append(Rect(x,y,x+w,y+h))

kept=np.ones(len(candidate_rectangles))
new_rectangles=[]
for i in range(len(candidate_rectangles)):
    for j in range(i+1,len(candidate_rectangles)):
        b=they_form_an_equal_sign(candidate_rectangles[i], candidate_rectangles[j])
        if b:
            new_rect=merge_rectangles(candidate_rectangles[i], candidate_rectangles[j])
            new_rectangles.append(new_rect)
            kept[i]=0
            kept[j]=0

for i in range(len(kept)):
    if kept[i]:
        rect=candidate_rectangles[i]
        cv2.rectangle(image, (rect.a, rect.b), (rect.c, rect.d), color=(0, 255, 0), thickness=2)
for rect in new_rectangles:
    cv2.rectangle(image, (rect.a, rect.b), (rect.c, rect.d), color=(0, 255, 0), thickness=2)

plt.imshow(image, cmap="gray") 
plt.show()
导入cv2
将matplotlib.pyplot作为plt导入
将numpy作为np导入
类矩形:
定义初始化(自我,
A.
B
C
d) :
self.a=a
self.b=b
self.c=c
self.d=d
self.center=np.array([(a+c)/2,(b+d)/2])
def合并矩形(r_1,r_2):
a_m=min(r_1.a,r_2.a)
b_m=min(r_1.b,r_2.b)
c_M=最大值(r_1.c,r_2.c)
d_M=最大值(r_1.d,r_2.d)
return Rect(a_m,b_m,c_m,d_m)
如果它们形成等号(矩形1,
rect_2,
tol_w=10,以像素为单位
tol_h=10):
#检查边界框是否大致对齐
b0=(np.abs(rect_1.a-rect_2.a)
基本上,这采用了蛮力方法,即比较代码中检测到的每两个边界框,如果它们符合先前的要求,则将它们合并为一个更大的边界框:即,如果它们在水平方向上被褐变(负号的顶部和底部应该如此),并且它们的高度大致相同


然而,这显然不是健壮的:您需要调整阈值,如果您的表达式没有水平且清晰地分开,那么整个过程将崩溃。如果您想要一个更健壮/有用的系统,那么字符识别的基本ML方法是一个更好的起点。

这就是您想要实现的结果吗

正如@Micka所指出的,由于你的数学表达式似乎是水平书写的,你对等号的两个不同组成部分之间的关系有很强的先验知识,因此有一种直接的(但很粗糙的)方法来检测何时减号实际上是相等的:

import cv2
import matplotlib.pyplot as plt
import numpy as np

class Rect:
    def __init__(self,
            a,
            b,
            c,
            d):
        self.a=a
        self.b=b
        self.c=c
        self.d=d

        self.center=np.array([(a+c)/2, (b+d)/2])

def merge_rectangles(r_1, r_2):

    a_m= min(r_1.a, r_2.a)
    b_m= min(r_1.b, r_2.b)
    c_M= max(r_1.c, r_2.c)
    d_M= max(r_1.d, r_2.d)

    return Rect(a_m, b_m, c_M, d_M)

def they_form_an_equal_sign(rect_1,
        rect_2,
        tol_w=10,#in pixels
        tol_h=10):
    #check if the bounding boxes approximately align
    b0 = (np.abs(rect_1.a - rect_2.a) < tol_w ) and (np.abs(rect_1.c - rect_2.c) <     tol_w)

    #check if the bounding boxes have approximately the same height
    h1 = np.abs(rect_1.d - rect_1.b)
    h2 = np.abs(rect_2.d - rect_2.b)
    b1 = np.abs(h1-h2) < tol_h

    return b0 and b1

image = cv2.imread('/tmp/m.png')
grey = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(grey.copy(), 0, 255, cv2.THRESH_BINARY_INV)
contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,     cv2.CHAIN_APPROX_SIMPLE)

candidate_rectangles=[]
for c in contours:
    x,y,w,h = cv2.boundingRect(c)
    candidate_rectangles.append(Rect(x,y,x+w,y+h))

kept=np.ones(len(candidate_rectangles))
new_rectangles=[]
for i in range(len(candidate_rectangles)):
    for j in range(i+1,len(candidate_rectangles)):
        b=they_form_an_equal_sign(candidate_rectangles[i], candidate_rectangles[j])
        if b:
            new_rect=merge_rectangles(candidate_rectangles[i], candidate_rectangles[j])
            new_rectangles.append(new_rect)
            kept[i]=0
            kept[j]=0

for i in range(len(kept)):
    if kept[i]:
        rect=candidate_rectangles[i]
        cv2.rectangle(image, (rect.a, rect.b), (rect.c, rect.d), color=(0, 255, 0), thickness=2)
for rect in new_rectangles:
    cv2.rectangle(image, (rect.a, rect.b), (rect.c, rect.d), color=(0, 255, 0), thickness=2)

plt.imshow(image, cmap="gray") 
plt.show()
导入cv2
将matplotlib.pyplot作为plt导入
将numpy作为np导入
类矩形:
定义初始化(自我,
A.
B
C
d) :
self.a=a
self.b=b
self.c=c
self.d=d
self.center=np.array([(a+c)/2,(b+d)/2])
def合并矩形(r_1,r_2):
a_m=min(r_1.a,r_2.a)
b_m=min(r_1.b,r_2.b)
c_M=最大值(r_1.c,r_2.c)
d_M=最大值(r_1.d,r_2.d)
return Rect(a_m,b_m,c_m,d_m)
如果它们形成等号(矩形1,
rect_2,
tol_w=10,以像素为单位
tol_h=10):
#检查边界框是否大致对齐
b0=(np.abs(rect_1.a-rect_2.a)