Josip Mrđen
Josip Mrđen

Reputation: 31

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. payment slip 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. 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. I only need the red rectangle that is the biggest(the one just below UNIVERZALNI NALOG ZA PLAĆ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

desired This is the result I would really like to get, but haven't managed until now.

Upvotes: 0

Views: 1974

Answers (1)

eshirima
eshirima

Reputation: 3867


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. 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 :)

Upvotes: 1

Related Questions