Warping curved rectangle to a regular rectangle - opencv

Edit: Upon further research, I've came across similar questions. I guess the process is not as trivial as using the WarpPerspective() function. Here is a similar question and an answer.
I'm using methods like thresholding and canny to extract a rectangular shape from books. Sometimes the rectangles (contours) are deformed like this (pages are not always flat):
As you can see the bottom line is not a straight line. I need to warp it into a rectangle to do further analysis of its inside contents.
Normally, I use WarpPerspective() using the 4 points I get from ApproxPolyDP() with a contour like this and it works fine:
But I can't figure out what to do with a curved rectangle. Here is what I get using the method I use on non-curved rectangles. It's close but not quite what I want:

Related

Implementing Hough Transform

I am doing a sudoku grid detection in C using SDL. I figured out that using Hough Transform could help me detect lines so I tried to implement it. However, I don't understand what to do with the accumulator array after iterating on the image.
In fact, on the Wikipedia page, it is said that you have to apply a threshold and determine which parts of the image match the lines but I don't understand this part of the implementation.
Also, the Hough Transform implementation uses polar coordinates. However, SDL can only draw lines from two given points from what I have seen of the documentation. So how could I draw the lines with the polar expression of the lines?
PS : This is my first time using StackOverflow, I hope that I formulated my problem correctly.

Find best rectangular fit for segmented contour

After segmentation of Objects in noisy data, I need to fit the best possible retangulat fit.
currently I just use opencv findContours and minAreaRect which will give me all around. I know that those objects will always be horizontal in the image with a maximum small angle like in this image.
This can be seen as the green rectanlge in the images, however I would need something like the red drawn rectangles, or even just the middle line (blue) since thats what I do need in the end.
Further, I also do have some conjunctions, like seen in this image:
Here I want to only detect the horizontal part and maybe know that there could be a junction.
Any idea how to solve this problem? I need some fast approach and have not found anything feasable yet.
Got much better results using distance transform (as mentioned from #Micka) on the masked Image, afterwars find the Line with the biggest distance as the middle of the rectangle (using some Filters, cuting off the curve) and in the End fitting a Line on the middle estimate.

iOS detect document edges

I'm working with GPUImage to detect document edges, using GPUImageHoughTransformLineDetector without any previous filter.
I'm discarding returned lines that are similar between them, and then I'm calculating the points of intersection.
With those points I want to find possible rectangles, but I can't figure out how.
I found this post answer https://stackoverflow.com/a/26502570/3708095 that says:
The steps would be:
Edge detection using Sobel filter.
Hough transform to find all straight lines in the image.
Look at all parallel lines and then all lines 90 degrees to those parallel line pairs, to find possible rectangles.
Pick the rectangle you like best. This could be by area, or by being best aligned to the phone, or you require that all edges are
inside the visible camera image, or some other method.
Said that way seems quite easy to pick up the rectangles, but I wonder how I can find all the rectangles in an efficient way to be fast enough to do it real time, since using 4 nested loops to find all combinations of those corners is a really expensive calculation.
Besides, step 3 doesn't seem to be as easy as unapiedra describes. I think that you can find multiple parallel lines in an image... Am I misunderstanding something?

Recognize basic shapes in binarized image using OpenCV

How can I recognize some basic (usually rotated) shapes:
circle,
"empty" circle,
cross,
empty triangle,
using OpenCV? What's the most straightforward way? It would be nice if the user could "define" his own shapes somehow (load a template image, possibly).
I'd like to map each recognized shape to its type (circle, triangle etc.) and a position of its center. (And its rotation if possible).
For circles HoughCircles could be used (there's no rotation in this case, too). But what about the others? Template matching doesn't support rotation and scaling, right?...
Here's the input:
You are right that regular template matching are not rotation, scale invariant. Take a look at OpenCV's matchShapes. Internally, it uses HuMoments. You will need to use findContours to find each individual object. Now once you have done this, you will probably find matchShapes couldn't distinguish Circle from Ring. A simple way to solve this is to use the hierarchy structure from findContours. If there is a hole (large enough) inside a Circle, that's probably a Ring.

how to connect points after identifying them in cvgoodfeaturesTotrack

I want to identify an object and draw a shape around it ...
I used previously the color identification but wasn't a good option since color change dramatically from place to place .. so I though why not identifying objects by features such as edges .. and I did that using this function in openCV
cvgoodfeaturesTotrack
it returns the (x,y)-coordinates of the points .. now I want to connect those points.. well not all of them but the one who are close to each other to draw a shape around the different objects. Any ideas ?
I don't think there is a free lunch in this case. You are trying to reconstruct a polygon if you only know the corner points of the polygon. There is no unique solution to this problem: you can draw all sorts of polygons through the corners. If you are certain the shape you are after is convex, then you can construct the convex span of the corner points, but the result will be horrible if you include any corners that were not part of the original object.
It seems to me that detecting corners is not the way to segment an object that is more or less delimited by lines. You probably want to try an edge detector instead, or a proper segmentation technique such as watershed.

Resources