OpenCV检测手写等号运算符错误

我正在尝试=使用 OpenCV 库识别手写数学运算符,但是,我遇到了一个问题,即它只能识别可以一笔画出的东西。


import cv2

import numpy as np

from PIL import Image, ImageOps




img = cv2.imread("sum.png")

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


#Gaussian Blurring to reduce noise

blur = cv2.GaussianBlur(gray, (5,5), 0)


#Adaptive Thresholding to account for different light/shadows

threshed = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)



morphed = cv2.morphologyEx(threshed, cv2.MORPH_OPEN, np.ones((3,3)))


conturs_lst = cv2.findContours(morphed, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2]



for cnt in conturs_lst:

    x,y,w,h = cv2.boundingRect(cnt)

    cv2.rectangle(img, (x,y), (x+w, y+h), (255, 0, 255), 1, cv2.LINE_AA)



相反,此代码会导致这种行为,其中等号的两个部分被限制在单独的框中。


输入图像:

http://img1.mukewang.com/61e66f7f0001b2ee08150699.jpg

实际输出:

http://img1.mukewang.com/61e66f890001338b04700377.jpg

有谁知道如何修改我的代码以允许整个等号位于一个边界框下?


慕码人8056858
浏览 235回答 1
1回答

慕沐林林

你可以尝试这样的事情:cv::namedWindow("result1", cv::WINDOW_FREERATIO);cv::namedWindow("result2", cv::WINDOW_FREERATIO);cv::Mat img = cv::imread(R"(FRfLi.png)");// to graycv::Mat gray;cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);cv::threshold(gray, gray, 80, 255, cv::THRESH_BINARY_INV);cv::morphologyEx(gray, gray, cv::MORPH_CLOSE, cv::getStructuringElement(cv::MORPH_CROSS, cv::Size(3, 27)));cv::imshow("result1", gray);std::vector<std::vector<cv::Point> > contours;cv::findContours(gray, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);for (int i = 0; i < contours.size(); ++i) {&nbsp; &nbsp; cv::Rect rect = cv::boundingRect(contours[i]);&nbsp; &nbsp; rect.x -= 3;&nbsp; &nbsp; rect.y -= 3;&nbsp; &nbsp; rect.width += 6;&nbsp; &nbsp; rect.height += 6;&nbsp; &nbsp; cv::rectangle(img, rect, cv::Scalar(0, 0, 255), 2);}cv::imshow("result2", img);cv::waitKey();变形后你会得到这样的结果:并检测轮廓,您将得到以下结果:注意:&nbsp;代码是用 C++ 编写的,你可以只考虑它是如何实现的步骤。我希望它可以帮助你!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python