用opencv-python检测W2中的单个框
我已经做了大量的研究,但找不到一种能够满足我需求的技术组合 我的情况是,我需要在数百个W2上执行OCR,以提取数据进行对账。W2的质量非常差,因为它们被打印并随后扫描回计算机。上述过程超出我的控制范围;不幸的是,我不得不用我所拥有的来工作 去年,我成功地执行了这个过程,但我不得不强行执行,因为时效性是一个主要问题。我这样做是通过手动指示要提取数据的坐标,然后一次只对这些段执行一个OCR。今年,我想提出一个更具动态性的情况,预计坐标可能会改变,格式可能会改变,等等 我已包括一个样本,擦洗W2以下。其思想是将W2上的每个框作为自己的矩形,并通过遍历所有矩形来提取数据。我尝试了几种边缘检测技术,但没有一种能够提供所需的准确信息。我相信我还没有找到所需的正确的预处理组合。我试着镜像一些数独谜题检测脚本 以下是我到目前为止尝试的结果,以及python代码,无论OpenCV 2还是OpenCV 3都可以使用:用opencv-python检测W2中的单个框,python,python-2.7,opencv,computer-vision,edge-detection,Python,Python 2.7,Opencv,Computer Vision,Edge Detection,我已经做了大量的研究,但找不到一种能够满足我需求的技术组合 我的情况是,我需要在数百个W2上执行OCR,以提取数据进行对账。W2的质量非常差,因为它们被打印并随后扫描回计算机。上述过程超出我的控制范围;不幸的是,我不得不用我所拥有的来工作 去年,我成功地执行了这个过程,但我不得不强行执行,因为时效性是一个主要问题。我这样做是通过手动指示要提取数据的坐标,然后一次只对这些段执行一个OCR。今年,我想提出一个更具动态性的情况,预计坐标可能会改变,格式可能会改变,等等 我已包括一个样本,擦洗W2以下。
import cv2
import numpy as np
img = cv2.imread(image_path_here)
newx,newy = img.shape[1]/2,img.shape[0]/2
img = cv2.resize(img,(newx,newy))
blur = cv2.GaussianBlur(img, (3,3),5)
ret,thresh1 = cv2.threshold(blur,225,255,cv2.THRESH_BINARY)
gray = cv2.cvtColor(thresh1,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,220,apertureSize = 3)
minLineLength = 20
maxLineGap = 50
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)
for x1,y1,x2,y2 in lines[0]:
cv2.line(img,(x1,y1),(x2,y2),(255,0,255),2)
cv2.imshow('hough',img)
cv2.waitKey(0)
如果您不遵守我的代码中的任何内容,请告诉我。这个概念的最大缺点是 1:(如果主框行中有嘈杂的中断,会将其拆分为单独的斑点) 2:idk如果这是一个可以手写文本的东西,但是让字母重叠在盒子的边缘可能是不好的 3:它绝对没有方向检查(你可能真的想改进它,因为我认为这不会太糟糕,会给你更准确的手柄)。我的意思是,这取决于长方体与xy轴大致对齐,如果它们足够倾斜,它将为所有长方体角点提供总偏移量(尽管它仍然可以找到所有长方体角点) 我稍微调整了一下阈值设定点,以便将所有文本与边缘分开,如果需要,在开始打断主线之前,可以将其拉得更低。此外,如果担心换行,可以将足够大的斑点添加到最终图像中。 基本上,第一步是调整阈值,使其处于最稳定(可能是保持连接框的最低值)的截止值,以便从框中分离文本和噪声 第二,找到最大的正blob(应该是boxgrid)。如果你的盒子不能一直放在一起,你可能想要取几个最高的斑点。。。虽然这会变得很粘,所以尝试获取阈值,以便可以将其作为单个水滴获取 最后一步是得到矩形,要做到这一点,我只需要寻找负斑点(忽略第一个外部区域)
这里是代码(对不起,它是在C++中,但希望你理解这个概念,无论如何都会自己写):
#包括“opencv2/imgproc/imgproc.hpp”
#包括“opencv2/highgui/highgui.hpp”
#包括
#包括
#包括
使用名称空间cv;
//尝试查找最大的连接点组(假定为textbox网格的互连边界)
Mat biggestComponent(Mat targetImage,int connectivity=8)
{
Mat输入图像;
inputImage=targetImage.clone();
Mat finalImage;/=输入图像;
int greatestBlobSize=0;
std::coutHe-He,边缘检测不是唯一的方法。由于边缘足够厚(每个地方至少有一个像素),二值化允许您对框内的区域进行单选
通过简单的标准,您可以消除混乱,而仅仅是边界框就可以提供相当好的分割效果
问题在于,使用此参数几乎无法检测到垂直线,请尝试使用lines\u v=cv2.HoughLinesP(边,1,np.pi,100,minLineLength,maxLineGap)查找垂直线
并为此进行另一个循环。尝试为HoughLinesP
函数设置不同的参数值,可能为水平线和垂直线设置不同的参数值。类似于偷偷摸摸的北极熊的方法。
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
//Attempts to find the largest connected group of points (assumed to be the interconnected boundaries of the textbox grid)
Mat biggestComponent(Mat targetImage, int connectivity=8)
{
Mat inputImage;
inputImage = targetImage.clone();
Mat finalImage;// = inputImage;
int greatestBlobSize=0;
std::cout<<"Top"<<std::endl;
std::cout<<inputImage.rows<<std::endl;
std::cout<<inputImage.cols<<std::endl;
for(int i=0;i<inputImage.cols;i++)
{
for(int ii=0;ii<inputImage.rows;ii++)
{
if(inputImage.at<uchar>(ii,i)!=0)
{
Mat lastImage;
lastImage = inputImage.clone();
Rect* boundbox;
int blobSize = floodFill(inputImage, cv::Point(i,ii), Scalar(0),boundbox,Scalar(200),Scalar(255),connectivity);
if(greatestBlobSize<blobSize)
{
greatestBlobSize=blobSize;
std::cout<<blobSize<<std::endl;
Mat tempDif = lastImage-inputImage;
finalImage = tempDif.clone();
}
//std::cout<<"Loop"<<std::endl;
}
}
}
return finalImage;
}
//Takes an image that only has outlines of boxes and gets handles for each textbox.
//Returns a vector of points which represent the top left corners of the text boxes.
std::vector<Rect> boxCorners(Mat processedImage, int connectivity=4)
{
std::vector<Rect> boxHandles;
Mat inputImage;
bool outerRegionFlag=true;
inputImage = processedImage.clone();
std::cout<<inputImage.rows<<std::endl;
std::cout<<inputImage.cols<<std::endl;
for(int i=0;i<inputImage.cols;i++)
{
for(int ii=0;ii<inputImage.rows;ii++)
{
if(inputImage.at<uchar>(ii,i)==0)
{
Mat lastImage;
lastImage = inputImage.clone();
Rect boundBox;
if(outerRegionFlag) //This is to floodfill the outer zone of the page
{
outerRegionFlag=false;
floodFill(inputImage, cv::Point(i,ii), Scalar(255),&boundBox,Scalar(0),Scalar(50),connectivity);
}
else
{
floodFill(inputImage, cv::Point(i,ii), Scalar(255),&boundBox,Scalar(0),Scalar(50),connectivity);
boxHandles.push_back(boundBox);
}
}
}
}
return boxHandles;
}
Mat drawTestBoxes(Mat originalImage, std::vector<Rect> boxes)
{
Mat outImage;
outImage = originalImage.clone();
outImage = outImage*0; //really I am just being lazy, this should just be initialized with dimensions
for(int i=0;i<boxes.size();i++)
{
rectangle(outImage,boxes[i],Scalar(255));
}
return outImage;
}
int main() {
Mat image;
Mat thresholded;
Mat processed;
image = imread( "Images/W2.png", 1 );
Mat channel[3];
split(image, channel);
threshold(channel[0],thresholded,150,255,1);
std::cout<<"Coputing biggest object"<<std::endl;
processed = biggestComponent(thresholded);
std::vector<Rect> textBoxes = boxCorners(processed);
Mat finalBoxes = drawTestBoxes(image,textBoxes);
namedWindow("Original", WINDOW_AUTOSIZE );
imshow("Original", channel[0]);
namedWindow("Thresholded", WINDOW_AUTOSIZE );
imshow("Thresholded", thresholded);
namedWindow("Processed", WINDOW_AUTOSIZE );
imshow("Processed", processed);
namedWindow("Boxes", WINDOW_AUTOSIZE );
imshow("Boxes", finalBoxes);
std::cout<<"waiting for user input"<<std::endl;
waitKey(0);
return 0;
}