简体   繁体   English

如何在Python中使用OpenCV查找图片上的最大矩形?

[英]How to find the biggest rectangle on picture using OpenCV in Python?

I am having a school project which is based on classifying letters and numbers and for that I have been using Convolutional Neural Networks. 我有一个学校项目,该项目基于对字母和数字进行分类,为此我一直在使用卷积神经网络。 An application would be recognition of some data on payment slips. 一个应用将是识别付款单上的一些数据。 However, in order to extract the data from the payment slip I need to recognize it first. 但是,为了从付款单中提取数据,我需要首先识别它。 It is a rectangular object and I need to get the exact bounding box of it. 这是一个矩形对象,我需要获取它的确切边界框。 I will be scanning those payment slips and the background will always be white. 我将扫描这些付款单,背景将始终为白色。 付款单 So my problem is that I know how to detect letters, numbers with CNN-s but I don't have a clue about working with OpenCV in Python. 所以我的问题是我知道如何使用CNN-s检测字母,数字,但是我不了解在Python中使用OpenCV的任何线索。 I heard it is really easy to detect rectangles using OpenCV, I have tried some codes from StackOverflow but they mostly gave me the whole paper as the bounding box which I don't need. 我听说使用OpenCV检测矩形真的很容易,我尝试了StackOverflow的一些代码,但是它们大多数都给了我整个纸,作为我不需要的边框。 I only need the red rectangle that is the biggest(the one just below UNIVERZALNI NALOG ZA PLAĆANJE). 我只需要最大的红色矩形(UNIVERZALNI NALOG ZAPLAĆANJE下方的红色矩形)。 I'm really sorry that I do not have any code that I have written for this problem, but I really don't know what the methods do in OpenCV and if I started learning OpenCV it would take some time which unfortunately I don't have.... If anyone has at least some help i would really appreciate it 非常抱歉,我没有针对该问题编写的任何代码,但我真的不知道该方法在OpenCV中的作用,如果我开始学习OpenCV,将花费一些时间,但是不幸的是,我没有有...。如果有人至少有一些帮助,我将非常感谢

期望 This is the result I would really like to get, but haven't managed until now. 这是我真正想要得到的结果,但是直到现在还没有得到解决。


Disclaimer 放弃

I will provide you with the easiest (at least what I think is) solution to this. 我将为您提供最简单(至少我认为是)的解决方案。

After you have fully understood it, you may go ahead and deploy fancier stuff. 完全理解它之后,您可以继续部署更高级的东西。 My answer is meant to be a guide and you are responsible for the code. 我的回答是作为指南,您应对代码负责。



Edit I: 编辑我:

Sorry I missed a point. 对不起,我错过了一点。 You want to grayscale your image first. 您想先对图像进行灰度处理。


The most common initial stage to this problem is cleaning out the noise . 这个问题最常见的初始阶段是清除噪声 Depending on your application though, this can be an optional initial stage. 但是,根据您的应用程序,这可以是可选的初始阶段。 There are different operations you can perform to help you get rid of the unnecessary stuff. 您可以执行各种操作来帮助您摆脱不必要的东西。

Thereafter, you can find the edges of the less noisy image. 之后,您可以找到噪点较小的图像的边缘 This helps us see an outline of the image. 这有助于我们看到图像的轮廓。

From here, you start looking for contours inside our edges image. 从这里开始,您开始在边缘图像中寻找轮廓

At this point now, we have attained contours and it is time to run this and/or this analysis on them to see if we can pinpoint our desired one. 至此,我们已经获得了轮廓,现在对它们进行和/或分析,以查看是否可以精确定位所需的轮廓。

The simplest initial analysis can be removing/ignoring contours whose area is smaller than a certain threshold. 最简单的初始分析可以是删除/忽略面积小于某个阈值的轮廓。 Once you have observed the results, go ahead and maybe combine the area with something else like the aspect ratio and see what you get. 观察结果后,继续进行操作,然后将区域与长宽比之类的东西结合起来,看看会得到什么。

After some trial and error, we have managed to isolate the desired contour, you can go ahead and draw a bounding rect around it. 经过反复试验,我们设法隔离了所需的轮廓,您可以继续在其周围绘制边界矩形。

Two Cents: 两分钱:

Just like CNN model creation, image processing is a trial and error task. 就像CNN模型创建一样,图像处理也是一个反复试验的任务。 For each of the different methods I have listed above, you have to try out different parameters and see which one makes sense. 对于上面列出的每种方法,您都必须尝试不同的参数,然后看看哪个参数有意义。

There are other alternatives you can use instead. 您还可以使用其他替代方法。 Say instead of finding edges, maybe you can try thresholding the image , or maybe smooth out the image right after gray scaling it. 说而不是寻找边缘,也许您可​​以尝试对图像进行阈值处理 ,或者可以在对图像进行灰度缩放后立即对其进行平滑处理

Cheers, happy coding :) 干杯,快乐的编码:)

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

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